8. Sum Types I

Only one of the given possible alternatives is used.

A sum type represents a choice: one of several possibilities. Choose Good or Evil. A Grade could be A, B, C, D, or F. A Maybe Int is one of these types; it could be Just 10 or Nothing.

  1. Define a TrackEvent which could be either Sprint "100m" or Sprint "400m" (or similar) or LongDistance 5 or LongDistance 26 (or similar). The idea is that the LongDistance is measured in miles, but there are no actual units included in that data.

  2. Write a similar function which takes in two TrackEvents and gives True if both are Sprints or both are LongDistance with the distance less than 10 (“short long distance”), or both are LongDistance with the distance greater than or equal to ten (“very long distance”).

  3. The sky is Sunshine or Cloudy. The temperature is Hot, Cold, or Freezing. Write a data type Weather that holds sky and temperature information.

  4. Write a function goodNews which is true when the weather is sunshine and cold or cloudy and hot.

  5. (coldSky). Given a list of Weather, find the sky state of the first Freezing day. Since there might not be a freezing day, this function should return a Maybe.

Last modified October 25, 2023: Exercises to learn alternatives in types. (3192f07)