module SayNum2 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 2: Two digits max.
------------------------------------------------------------------

-- The main function
convert :: Int -> String
convert n = convert2 (tensSplit n)

-- 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"

-- In most cases we split the number (say 42) into its two digits
-- (4,2), get the two words (tens 4 and units 2) and link them with a
-- dash.  The tensSplit function gives the separate digits, so our
-- convert2 function would be something like:

-- convert2 :: (Int,Int) -> String
-- convert2 (t,u) = tens t ++ "-" ++ units u

-- But there are three special cases:
-- (1) The tens digit is 0, so no tens and no dash
-- (2) The tens digit is 1, so use the teens mapping
-- (3) The units digit is 0, so no dash and no units

convert2 :: (Int,Int) -> String
convert2 (t,u)
    | t == 0    = units u
    | t == 1    = teens u
    | u == 0    = tens  t
    | otherwise = tens  t ++ "-" ++ units u

-- Note that the ordering of guards in the convert2 definition is
-- quite delicate here.  Try reordering them to discover what happens.




