module SayNum3 where

-- Another version of the NumWords script (see week 2 lab exercises)
-- to convert positive integers into their English phrase equivalent.

-- A version without lists as an in-class problem-solving live demo.

-- Clem Baker-Finch February 2006
------------------------------------------------------------------
-- Step 3: Three digits max.

-- Now we're on a roll.  There are less special cases from now on
-- and only a few extra words and punctuation.
------------------------------------------------------------------

-- The main function
convert :: Int -> String
convert n = convert3 (hundredsSplit n)

-- A function to split an integer less than 1000 into the hundreds
-- digit and the other 2 digits
hundredsSplit :: Int -> (Int,Int)
hundredsSplit n = (n `div` 100, n `mod` 100)

-- What are the special cases for convert3?
-- (1) hundreds digit is 0 , so don't say "hundred"
-- (2) tens,units is 0, so don't say "and"

convert3 :: (Int,Int) -> String
convert3 (h,tu)
    | h == 0    = convert2 (tensSplit tu)
    | tu == 0   = units h ++ " hundred"
    | otherwise = units h ++ " hundred and " ++ convert2 (tensSplit tu)

------------------------------------------------------------------
-- All the same as SayNum2.hs from here on.
------------------------------------------------------------------

-- A function to split an integer less than 100 into separate digits
tensSplit :: Int -> (Int, Int)
tensSplit n = (n `div` 10, n `mod` 10)

-- A function returning digit names
units :: Int -> String
units 0 = "zero"
units 1 = "one"
units 2 = "two"
units 3 = "three"
units 4 = "four"
units 5 = "five"
units 6 = "six"
units 7 = "seven"
units 8 = "eight"
units 9 = "nine"

-- A function returning tens names
tens :: Int -> String
tens 2 = "twenty"
tens 3 = "thirty"
tens 4 = "forty"
tens 5 = "fifty"
tens 6 = "sixty"
tens 7 = "seventy"
tens 8 = "eighty"
tens 9 = "ninety"

-- A function returning teen names
teens :: Int -> String
teens 0 = "ten"
teens 1 = "eleven"
teens 2 = "twelve"
teens 3 = "thirteen"
teens 4 = "fourteen"
teens 5 = "fifteen"
teens 6 = "sixteen"
teens 7 = "seventeen"
teens 8 = "eighteen"
teens 9 = "nineteen"

-- A function to convert a pair of (digit) Ints into words 
convert2 :: (Int,Int) -> String
convert2 (t,u)
    | t == 0    = units u
    | t == 1    = teens u
    | u == 0    = tens  t
    | otherwise = tens  t ++ "-" ++ units u



