8. Walk II
In this assignment you should practice writing all of your functions
two ways: using pattern matching and using the record updating syntax
with {...}.
- The
Studentdata type contains anIntage and aStringname. Write a record syntaxdataline.
-
Write two versions of a function to make the student one year older. One using pattern matching, the other using record updating.
older :: Student -> Student
- Make a
Gamedatatype withposfield with type(Int,Int)and apointsof typeInt.
-
Write a
move_rightfucntion to change the x coordinate of the pos in the Game to be x plus a given number. Write a second version so you have both pattern matching and record updating versions.move_right :: Game -> Int -> Game
-
Write a function to add 6 to the number of points.
score_6 :: Game -> Game -
Define a data type
AttackTypeto store this information: an attack can be one of the following:NothinggWhackSmashFire Int
The
(Fire 15)attack means a fire attack that will give you 15 points.
-
Write an
updateScorefunction that takes in aGameand anAttackTypeand puts out a game with more points. The number of points to add depends on theAttackType.AttackType Points Nothingg 0 Whack 2 Smash 5 Fire n n updateScore :: Game -> AttackType -> Game
Testing Code
ex1 :: Student
ex1 = Student {age=16, name="Benny"}
ex2 = (Student {age=17, name="Benny"} == older ex1)
ex3 :: Game
ex3 = Game{pos=(3,4), points=50}
ex4a = move_right ex3
ex4b = (ex4a == Game {pos=(4,4), points=50})
ex5a = score_6 ex4a
ex5b = (ex5a == Game {pos=(3,4), points=56})
ex6a :: AttackType
ex6a = Nothingg
ex6b :: AttackType
ex6b = Whack
ex6c :: AttackType
ex6c = Smash
ex6d :: AttackType
ex6d = Fire 15
ex7a = updateScore ex4a ex6a == ex4a
ex7b = updateScore ex4a ex6b == Game {pos=(3,4), points=52}
ex7c = updateScore ex4a ex6c == Game {pos=(3,4), points=55}
ex7d = updateScore ex4a ex6d == Game {pos=(3,4), points=65}