8. Walkthrough I
A sequence of short exercises to become familiar with the first half of constructors.
- Make
AList
mean[Int]
.
- Write a function (
ao
) that takes in an AList and puts out an Int. Theao
function 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
Celsius
that holds a Float measuring the temperature.
- Make a similar data type called Fahrenheit.
- (
tempchange
) Make a function that convertsCelsius
toFahrenheit
according 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
marry
function takes two Surname and combines them with a hyphen to create a new Surname. Write the signature and function.
-
Beginning with the
Point
definition below, write ad0
function measuring the distance to the origin.data Point = Origin | Point Float Float
- Make a datatype
Drink
which isWater
orGatorade
.
- Make a calories function that converts Drink to Int calories. Water is 0, Gatorade is 15. Include a signature.