Data Types

Question 1 Label each of the following things as enumerated type, tuple, record type or list.

(Barcode, Item)
['c','o','h','v','t','e','d','m','n','o','e','t','e','y','t','s','u','t']
data Colour = Red | Green | Blue | Yellow | Black
data Ant = Ant { antPosition :: Coord
               , antOrientation :: Direction
               , antTransition :: [Transition SquareTurn]}

Enumerated data types

Question 2 Write Month as an enumerated data type, i.e complete the following data type:

data Month = ...

Question 3 Write Season as an enumerated data type.

Question 4 Write a function that takes a month and returns the season it is in.

Tuples

Tuples are a convenient way of combining two types into one type without explicitly creating a new data type. Consider following implimentation of and:

and :: Bool -> Bool -> Bool
and p q = case p of
    True -> case q of
        True -> True
        _ -> False
    False -> case q of
        _ -> False

You saw this in PAL last week.

Question 4 There is a better way of writing this function without the use of nested cases.

Question 5 Try implimenting this using tuples while keeping the type declaration the same. i.e: use the type declaration

and :: Bool -> Bool -> Bool

Records

Question 6 Define a record type for two dimensional coordinates.

Question 7 Write a function to find the distance from the origin.

Question 8 Write a function to double the x-coordinate.

Lists

In your assignment, you’ve seen lists defined as:

data List a = Empty | Entry a (List a)

Haskell defines lists as:

data [a] = [] | a:[a]

(or at least it would if it could - but don’t worry too much about this)

Question 9 Write a function that takes in lists as how you’ve defined it in your assignment and outputs a list how Haskell prelude defines it. i.e: Write a function listConvert with the following type signature:

listConvert :: List a -> [a]