8. Walk II

Exercises introducing record syntax.

In this assignment you should practice writing all of your functions two ways: using pattern matching and using the record updating syntax with {...}.

  1. The Student data type contains an Int age and a String name. Write a record syntax data line.
  1. 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
    
  1. Make a Game datatype with pos field with type (Int,Int) and a points of type Int.
  1. Write a move_right fucntion 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
    
  1. Write a function to add 6 to the number of points.

     score_6 :: Game -> Game
    
  2. Define a data type AttackType to store this information: an attack can be one of the following:

    • Nothingg
    • Whack
    • Smash
    • Fire Int

    The (Fire 15) attack means a fire attack that will give you 15 points.

  1. Write an updateScore function that takes in a Game and an AttackType and puts out a game with more points. The number of points to add depends on the AttackType.

    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}
Last modified September 25, 2025: Daily exercises learning to make records. (54ffa6a)