Skip to content

Instantly share code, notes, and snippets.

@sordina
Last active June 19, 2018 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sordina/a4567fc765a483b3306bdfafe62a3b59 to your computer and use it in GitHub Desktop.
Save sordina/a4567fc765a483b3306bdfafe62a3b59 to your computer and use it in GitHub Desktop.
module Main where
main :: IO ()
main = interact (unlines . map (go . read) . lines)
go :: Int -> String
go n = case term n
of [] -> "nothing"
[(x,"")] -> numb x
xs -> unwords $ map item xs
item (0,"") = ""
item (0,y) = y
item (x,"") = "and " ++ numb x
item (x,y) = numb x ++ " " ++ y
term :: Int -> [(Int, String)]
term n
| n > quadrilion = let (d,m) = divMod n quadrilion in (d,"quadrilion") : term m
| n > trilion = let (d,m) = divMod n trilion in (d,"trilion") : term m
| n > billion = let (d,m) = divMod n billion in (d,"billion") : term m
| n > million = let (d,m) = divMod n million in (d,"million") : term m
| n > thousand = let (d,m) = divMod n thousand in (d,"thousand") : term m
| n > hundred = let (d,m) = divMod n hundred in (d,"hundred") : term m
| otherwise = [(n,"")]
hundred = 100
thousand = 1000
million = thousand * thousand
billion = thousand * million
trilion = thousand * billion
quadrilion = thousand * trilion
prop_test1 :: Bool
prop_test1 = go 1000000 == "one million"
numb :: Int -> String
numb 0 = ""
numb 1 = "one"
numb 2 = "two"
numb 3 = "three"
numb 4 = "four"
numb 5 = "five"
numb 6 = "six"
numb 7 = "seven"
numb 8 = "eight"
numb 9 = "nine"
numb 10 = "ten"
numb 11 = "eleven"
numb 12 = "twelve"
numb 13 = "thirteen"
numb 14 = "fourteen"
numb 15 = "fifteen"
numb 16 = "sixteen"
numb 17 = "seventeen"
numb 18 = "eighteen"
numb 19 = "nineteen"
numb 20 = "twenty"
numb x | x < 100 = let (d,m) = divMod x 10 in tees d ++ " " ++ numb m
| x < 1000 = let (d,m) = divMod x 100 in hundreds d ++ " " ++ numb m
numb x = error $ "numb: " ++ show x
tees :: Int -> String
tees 1 = "ten"
tees 2 = "twenty"
tees 3 = "thirty"
tees 4 = "fourty"
tees 5 = "fifty"
tees 6 = "sixty"
tees 7 = "seventy"
tees 8 = "eighty"
tees 9 = "ninety"
tees x = error $ "tees: " ++ (show x)
hundreds n = numb n ++ " hundred"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment