--------------------------------------------------
--------------------------------------------------

--    CS_221 Functional Programming I
--
--      Solutions to Coursework 1
--
--           7 March 2011

-- Question 1                  
--                               

alfredo :: Int -> Int -> Float
alfredo diam top = 
  let { 
        area = (fromIntegral diam/2)^2 * pi 
      }
  in if diam >= 10 && diam <= 100 && top >= 1 && top <= 10
     then area * (0.001 + fromIntegral top * 0.002) * 1.5  
     else 
       error 
       "Required: 10<=diameter<=100 and 1<=toppings<=10"

{- 
Main> alfredo 32 4
10.85735
Main> alfredo 32 11

Program error: Required: 10 <= diameter <= 100 and 1 <= toppings <= 10
-}
--------------------------------------------------
--------------------------------------------------
-- Question 2
--                               

menu :: String -> Int -> Int -> String
menu name dia top =
  let { 
        pennies = round ((alfredo dia top) * 100) ;
        pounds  = pennies `div` 100 ;
        pence   = pennies `mod` 100 ;
        poundstring = if pounds == 0 then "" else
                      if pounds == 1 then "1 pound" 
                                     else show pounds ++ " pounds" ;
        andstring   = if pounds > 0 && pence > 0 then " and " else ""  ;
        pencestring = if pence == 0 then "" else
                      if pence == 1 then "1 penny" 
                                    else show pence ++ " pence" ;
      }
  in "Pizza " ++ name ++ " costs " ++ poundstring ++ andstring ++ pencestring 

{-
"Pizza Diavolo costs 1 pound and 77 pence"
*Main> menu "Diavolo" 10 8
"Pizza Diavolo costs 2 pounds"
*Main> menu "Diavolo" 10 9
"Pizza Diavolo costs 2 pounds and 24 pence"
*Main> menu "Diavolo" 32 4
"Pizza Diavolo costs 10 pounds and 86 pence"
-}
--------------------------------------------------
--------------------------------------------------
-- END OF COURSEWORK
--------------------------------------------------
--------------------------------------------------
                                                  










