Skip to content

Instantly share code, notes, and snippets.

@esoergel
Created June 26, 2014 22:03
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 esoergel/aac92698f6859e03d9cf to your computer and use it in GitHub Desktop.
Save esoergel/aac92698f6859e03d9cf to your computer and use it in GitHub Desktop.
import Data.List
main :: IO ()
main = do
putStrLn "Enter a number"
number <- getLine
putStrLn $ longForm number
longForm n =
let ints = map makeInt n
strings = toStrings ints 0
in intercalate " " strings
longForm n = intercalate " " $ toStrings (map makeInt n) 0
makeInt :: Char -> Int
makeInt n = read (n:[])
toStrings :: [Int] -> Int -> [String]
toStrings [] _ = []
toStrings xs depth = leading ++ trailing ++ place
where trailing = convertTriple (last3 xs)
leading = toStrings (dropLast3 xs) (depth + 1)
place
| trailing == [] = []
| depth == 0 = []
| depth == 1 = ["thousand"]
| depth == 2 = ["million"]
| depth == 3 = ["billion"]
| depth == 4 = ["trillion"]
| depth == 5 = ["quadrillion"]
| otherwise = ["bajillion"]
last3 :: [Int] -> (Int, Int, Int)
last3 xs = (a, b, c)
where list | length xs < 3 = take (3 - length xs) [0,0..] ++ xs
| length xs >= 3 = drop (length xs - 3) xs
(a:b:c:[]) = list
dropLast3 :: [a] -> [a]
dropLast3 xs = reverse . (drop 3) $ reverse xs
convertTriple :: (Int, Int, Int) -> [String]
convertTriple (h,t,o)
| t == 1 && o /= 0 = h' ++ to'
| otherwise = h' ++ t' ++ o'
where h' = if h/=0 then getFromList ones h ++ ["hundred"] else []
t' = getFromList tens t
o' = getFromList ones o
to' = getFromList teens o
getFromList :: [String] -> Int -> [String]
getFromList _ 0 = []
getFromList list n = [list !! (n - 1)]
ones = ["one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine"]
tens = ["ten",
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety"]
teens = ["eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment