Skip to content

Instantly share code, notes, and snippets.

@newlawrence
Last active February 7, 2017 21:25
Show Gist options
  • Save newlawrence/832e60a3d39fa9a97aa90b6ee9690455 to your computer and use it in GitHub Desktop.
Save newlawrence/832e60a3d39fa9a97aa90b6ee9690455 to your computer and use it in GitHub Desktop.
A proof of concept of a small plural sentences creator
import qualified Data.Char as Char
data Things t = Things {thing :: t, quantity :: Int}
instance (Show t) => Show (Things t)
where
show t = if quantity t == 1 then get t else get t ++ "s"
where
get = filter (not . (`elem` "\"")) . show . thing
makeSentence :: (Show t) => [Things t -> String] -> t -> Int -> String
makeSentence ps t n = makeup . unwords . map ($ Things t n) $ ps
where
makeup (s:ss) = (Char.toUpper s : ss) ++ "."
things :: (Show t) => Things t -> String
things = show
thereBe :: (Show t) => Things t -> String
thereBe t
| quantity t == 1 = "there is"
| otherwise = "there are"
howMany :: (Show t) => Things t -> String
howMany t
| quantity t == 0 = "no"
| otherwise = show $ quantity t
tellHowMany :: (Show t) => t -> Int -> String
tellHowMany = makeSentence [thereBe, howMany, things]
main = do
putStrLn $ tellHowMany "apple" 0
putStrLn $ tellHowMany "banana" 1
putStrLn $ tellHowMany "orange" 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment