module TFwH.Chap01
(
Text
, Word
, commonWords
, sortWords
, countRuns
, sortRuns
, showRun
, convert
, units
, teens
, tens
, convert1
, digits2
, convert2
, combine2
, convert3
, convert6
, link
) where
import Data.Char (toLower)
import Prelude hiding (Word)
type Text = [Char]
type Word = [Char]
commonWords :: Int -> Text -> String
commonWords n = concat
. map showRun
. take n
. sortRuns
. countRuns
. sortWords
. words
. map toLower
sortWords :: [Word] -> [Word]
sortWords = undefined
countRuns :: [Word] -> [(Int, Word)]
countRuns = undefined
sortRuns :: [(Int, Word)] -> [(Int, Word)]
sortRuns = undefined
showRun :: (Int, Word) -> String
showRun = undefined
units :: [String]
units = [ "zero", "one", "two", "three", "four", "five"
, "six", "seven", "eight", "nine"
]
teens :: [String]
teens = [ "ten", "eleven", "twelve", "thirteen", "fourteen"
, "fifteen", "sixteen", "seventeen", "eighteen"
, "nineteen"
]
tens :: [String]
tens = [ "twenty", "thirty", "forty", "fifty", "sixty"
, "seventy", "eighty", "ninety"
]
convert1 :: Int -> String
convert1 n = units !! n
digits2 :: Int -> (Int, Int)
digits2 n = n `divMod` 10
convert2 :: Int -> String
convert2 = combine2 . digits2
combine2 :: (Int, Int) -> String
combine2 (t, u)
| t == 0 = units !! u
| t == 1 = teens !! u
| 2 <= t && u == 0 = tens !! (t - 2)
| 2 <= t && u /= 0 = tens !! (t - 2) ++ "-" ++ units !! u
convert3 :: Int -> String
convert3 n
| h == 0 = convert2 t
| t == 0 = units !! h ++ " hundred"
| otherwise = units !! h ++ " hundreds and " ++ convert2 t
where
(h, t) = n `divMod` 100
convert6 :: Int -> String
convert6 n
| m == 0 = convert3 h
| h == 0 = convert3 m ++ " thousand"
| otherwise = convert3 m ++ " thousand" ++ link h ++ convert3 h
where
(m, h) = n `divMod` 1000
link :: Int -> String
link h = if h < 100 then " and " else " "
convert :: Int -> String
convert = convert6