Ch08 Quiz C

Forest walk quiz on basic data types.
  1. Create a Direction data type that consists of one of North, South, East or West.

  2. Create a Posn alias for (Int,Int).

  3. Write the function d2p which converts a Direction to a Posn by associating (0,1) with North (the positive y axis direction in math class), (1,0) with East (the positive x direction), and similarly (0,-1) with South and (-1,0) with West.

  4. (shortWalk) Write a function that takes in the starting position, a number of steps, and a Direction to go in, and outputs the ending position.

     shortWalk :: Posn -> Int -> Direction -> Posn
    
  5. (longWalk) Write a function to find the ending position after going on a sequence of short walks, one after the other.

     longWalk :: Posn -> [(Int,Direction)] -> Posn
     ex =
       longWalk (5,13) [(3,South), (2,West), (7,South)] == (3,2)
    
  6. Create a new datatype Forage which holds one of Hungry, Berries Int or Nuts Int. Make a variable q6 that holds 6 berries.

  7. (calories) Hungry means no calories. Each berry is worth 5 calories, and each nut is worth 30 calories. The number with Berries how many 5 calorie berries were found. Treat Nuts similarly.

  8. (foodFind) Write a function to determine how many calories you find on a long walk.

     foodFind :: [(Posn, Forge)] -> Posn -> [(Int,Direction)] -> Int
    

    The given information is:

    • a list of giving the locations of all of the food;
    • your current position
    • a list of steps and directions in the form used in longWalk.

    Count food if you occupy the same position as the food after walking one (Int, Direction) leg of the journey.

    Include calories you find at the start or end location.

    You may assume that the path never goes to the same location twice.