import Data.Typeable {- Exercise 7.1 -} -- 7.1.1 Implement the types data Fuel = NaturalGas | Nuclear | Hydro | Wind deriving Show data FuelType = Renewables | Fussel deriving Show data Summary = CarbonZero | Resource deriving Show class Electricity a where generation :: a -> Integer -- Make Fuel instance of Electricity instance Electricity Fuel where generation = undefined -- Make Fueltype instance of Electricity instance Electricity FuelType where generation = undefined -- Make Summary instance of Electricity instance Electricity Summary where generation = undefined -- 7.1.2 Implement the comparison generateMore :: (Electricity a, Electricity b) => a -> b -> Bool generateMore x y = undefined -- Difference between generateMore and generateMore2 {- Exercise 7.2 -} -- 7.2.1 Implement the data constructors for complex number and define slope via "error" data Complex slope :: Complex -> Double slope = undefined slopeSum :: Complex -> Complex -> Complex -> Double slopeSum = undefined -- 7.2.2 Implement slope and slopeSum via the Maybe-type slopeMaybe :: Complex -> Maybe Double slopeMaybe = undefined slopeSumMaybe :: Complex -> Complex -> Complex -> Maybe Double slopeSumMaybe = undefined -- 7.2.3 Compare "error" and "Maybe" approach {- Exercise 7.3 -} -- 7.3.1 Implement the data constructors below data Sex data Person data HealthCategory instance Eq HealthCategory where _ == _ = undefined data Exercise pal :: Exercise -> Double pal exercise = undefined -- 7.3.2 health :: Person -> HealthCategory health person = undefined -- 7.3.3 bmr :: Person -> Double bmr person = undefined -- 7.3.4 healthAdvise :: Person -> Exercise -> String healthAdvise person exercise = undefined {- Test Heads-} test_list f ss str = all (test_fun f) ss where test_fun f s = f (fst s) == snd s || error ("function " ++ str ++ " on input " ++ show (fst s) ++ " delivered " ++ show (snd s) ++ ", but expected was " ++ show (f (fst s))) test_list2 f ss str = testFun (\p -> f (fst p) (snd p)) ss where testFun f = all (\p -> f (fst p) == snd p || error (str ++ ":on input "++(show (fst p))++" output is "++(show (f (fst p)))++" but should be "++(show (snd p)))) {- Tests for 7.1 -} -- 7.1.1 No test available test_Feul = zip [NaturalGas , Nuclear , Hydro , Wind] [52, 928, 632, 587] test_FeulType = zip [Renewables , Fussel] [1469, 1519] test_Summary = zip [CarbonZero , Resource] [2397, 3916] test_definition = test_list generation test_Feul "generation1" && test_list generation test_FeulType "generation2" && test_list generation test_Summary "generation3" -- 7.1.2 Implement the comparison test_compare_data = (generateMore NaturalGas Wind == False) && (generateMore Wind Fussel == False) && (generateMore Renewables Fussel == False) && (generateMore Resource CarbonZero == True) && (generateMore Nuclear Fussel == False) || error ("7.1.2 is not passed") {- Tests for 7.3 -} -- 7.3.1 -- personList = map (\(sex, weight, height, age) -> Person sex weight height age) [(Male, 138, 1.79, 54), (Female, 43, 1.58, 32), (Male, 82, 1.83, 40)] -- testPerson = all (\(Person sex weight height age) -> typeOf sex == typeOf (Female :: Sex) && typeOf weight == typeOf (1.0 :: Double) && typeOf height == typeOf (1.0 :: Double) && typeOf age == typeOf (1 :: Integer)) personList -- testHealthCategory = all (\(cat1, cat2, result) -> (cat1 == cat2) == result) [(Underweight, Underweight, True), (Overweight, Overweight, True), (Healthy, Healthy, True), (Underweight, Overweight, False), (Overweight, Healthy, False), (Healthy, Overweight, False)] -- testPal = all (\(cat, result) -> pal cat == result) [(Sedentary, 1.53), (Moderate, 1.76), (Active, 2.25)] -- 7.3.2 -- testHealth = all (\(person, category) -> health person == category) (zip personList [Overweight, Underweight, Healthy]) -- 7.3.3 -- testBmr = all (\(person, val) -> abs (bmr person - val) < 1e-4) (zip personList [1598.35, 920.244, 926.55]) -- 7.3.4 :: No test available -- test73 = testPerson && testHealthCategory && testPal && testHealth && testBmr