---------- CSE P505 Final Exam, Autumn 2016 -------- -------------- provided type definitions (do not change) ----------- data Coin = Quarter | Dime | Nickel | Penny -- Money q d n p is q quarters, d dimes, n nickels, and p pennies data Money = Money Int Int Int Int data MoneyTree = Leaf Coin | Node Coin MoneyTree MoneyTree ------------------- Problem 7 ---------------------------- -- (no provided code) ------------------- Problem 8 -------------------- {- main8 = -- some tests; see also the commented out main8 at bottom of file do { printMoney (Money 2 4 6 8) ; putStr "\n" ; printMoney (Money 2 0 2 0) ; putStr "\n" ; printMoney (Money 0 0 0 0) ; putStr "\n" } -} ------------------- Problem 9 -------------------- instance Eq Money where (==) (Money q1 d1 n1 p1) (Money q2 d2 n2 p2) = q1 == q2 && d1 == d2 && n1 == n2 && p1 == p2 -- reduce_pennies_to_half is written as intended for the exam; -- do not change it even if you really want to reduce_pennies_to_half (Money q d n p) = Money q d n (if (q + d + n) `div` p >= 1 then q + d + n else p) main9 = -- some tests referred to in 9b and 9c do { print ((Money 2 3 4 15) == (Money 4 0 1 0)) ; print ((Money 2 3 4 15) == (Money 2 3 4 15)) ; print (reduce_pennies_to_half (Money 2 3 4 15) == reduce_pennies_to_half (Money 4 0 1 0)) ; print (reduce_pennies_to_half (Money 2 3 4 15) == reduce_pennies_to_half (Money 2 3 4 15)) ; } -------------------- main --------------------- main = do { return () ; -- main8 ; main9 }