Ch08 Quiz C
-
Create a
Direction
data type that consists of one ofNorth
,South
,East
orWest
. -
Create a
Posn
alias for(Int,Int)
. -
Write the function
d2p
which converts aDirection
to aPosn
by associating(0,1)
withNorth
(the positive y axis direction in math class),(1,0)
withEast
(the positive x direction), and similarly(0,-1)
withSouth
and(-1,0)
withWest
. -
(
shortWalk
) Write a function that takes in the starting position, a number of steps, and aDirection
to go in, and outputs the ending position.shortWalk :: Posn -> Int -> Direction -> Posn
-
(
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)
-
Create a new datatype
Forage
which holds one ofHungry
,Berries Int
orNuts Int
. Make a variable q6 that holds 6 berries. -
(
calories
) Hungry means no calories. Each berry is worth 5 calories, and each nut is worth 30 calories. The number withBerries
how many 5 calorie berries were found. TreatNuts
similarly. -
(
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.