8. Walk I
A sequence of short exercises to become familiar with the first half of constructors.
- Make
AListmean[Int].
- Write a function (
ao) that takes in an AList and puts out an Int. Theaofunction adds up all of the odd numbers in the list. Include signature.
- Make
WP(for “word pair”) mean(String, String).
- Write a function (
ce) counting how many times the two strings are equal in a list ofWP. Include signature.
- Make a data type called
Celsiusthat holds a Float measuring the temperature.
- Make a similar data type called Fahrenheit.
- (
tempchange) Make a function that convertsCelsiustoFahrenheitaccording to the rule (f = 9/5 c + 32).
- Names: Surname is a String, and GivenName is a String. Make two data types, one for each.
- The
marryfunction takes two Surname and combines them with a hyphen to create a new Surname. Write the signature and function.
-
Beginning with the
Pointdefinition below, write ad0function measuring the distance to the origin.data Point = Origin | Point Float Float
- Make a datatype
Drinkwhich isWaterorGatorade.
- Make a calories function that converts Drink to Int calories. Water is 0, Gatorade is 15. Include a signature.
Tester code
p1 :: AList
p1 = [10,20,30]
p2 = (ao p1 == 40)
p3 :: WP
p3 = ("Chicago","Illinois")
p4 = (ce [("X","Y"),("A","A"),("B","B"),("C","D")] == 2)
p5 :: Celsius
p5 = Celsius 37.0
p6 :: Fahrenheit
p6 = Fahrenheit 98.6
p7 = (p6 == tempchange p5)
p8a :: Surname
p8a = Surname "Mauer"
p8b = Surname "Oats"
p8c :: GivenName
p8c = GivenName "Doc"
p9 = (marry p8a p8b) == Surname "Mauer-Oats"
p10a = (d0 Origin == 0)
p10b = (d0 (Point 3.0 4.0)) == 5.0
p11a :: Drink
p11a = Gatorade
p11b :: Drink
p11b = Water
p12 = (calories Water, calories Gatorade) == (0,15)