-- Exercise 1 data Rat = Rat Integer Integer normaliseRat :: Rat -> Rat normaliseRat = undefined createRat :: Integer -> Integer -> Rat createRat = undefined instance Eq Rat where -- to implement instance Ord Rat where -- to implement instance Show Rat where show r = undefined instance Num Rat where -- to implement -- Exercise 2 data Vegetable -- to be defined data Candy -- to be defined type Shoppinglist a = [(a,Integer)] -- item and quantity veggies :: Shoppinglist Vegetable veggies = undefined -- [ (Corn Store, 2), (Corn Named, 3), (Mushroom Named, 1), (Beans, 5) ] candies :: Shoppinglist Candy candies = undefined -- [ (Duplo Family, 1), (Twix Single, 5), (Balisto, 2) ] class Cost a where -- to implement instance Cost Candy where -- to implement instance Cost Vegetable where -- to implement shoppingCosts :: Cost a => Shoppinglist a -> Double shoppingCosts = undefined -- Tests {- the tests currently do not compile because of the non-availibility of the data types of exercise 2; activate the tests after their definitions by removing the comment delimiters {- .. -} around tests. -} {- tests = do --Rational Number tests check "normaliseRat" "True" (case (normaliseRat (Rat (-1) (-2)), normaliseRat (Rat 2 4)) of (Rat n1 d1, Rat n2 d2) -> (n1, d1) == (n2, d2)) check "createRat" "-1/2" (createRat 5 (-10)) check "Equality on Rats" "[True,False]" ([Rat 3 5 == Rat (-9) (-15), Rat 3 5 == Rat 5 3]) check "Order on Rats" "[True,False,True]" ([Rat 3 5 < Rat 3 4, Rat 2 4 < Rat 2 4, Rat (-5) 3 < Rat 10 1]) check "Show on Rats" "[3,4/5,-1/2]" ([Rat 3 1, Rat 4 5, Rat 1 (-2)]) check "Num on Rats" "[1/2,7/12,3/4,-1]" ([3 * Rat 1 6, Rat 1 3 + Rat 1 4, abs (Rat (-3) 4), signum (negate (Rat 3 4))]) --Typeclass tests let veggiesTest = [(Mushroom Store, 20), (Beans, 101), (Corn Named, 1)] let candiesTest = [(Twix Medium, 2), (Duplo Single, 10), (Duplo Single, 100)] check "Cost function" "[0.5,3.49,2.39,2.59,1.79,0.89]" ([cost (Duplo Single), cost (Duplo Family), cost (Twix Medium), cost (Corn Named), cost (Mushroom Store), cost Beans]) check "shoppingCosts function" "[17.15,8.99,128.28,59.78]" ([shoppingCosts veggies, shoppingCosts candies, shoppingCosts veggiesTest, shoppingCosts candiesTest]) -} check name e c = do putStr ("*** " ++ name ++ ": ") if show c == e then putStrLn "OK" else putStrLn ("ERROR; expected '" ++ e ++ "', but found '" ++ show c ++ "'")