Last active
February 7, 2017 21:25
-
-
Save newlawrence/832e60a3d39fa9a97aa90b6ee9690455 to your computer and use it in GitHub Desktop.
A proof of concept of a small plural sentences creator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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