2024 Haskell Test A
Write your answers on blank sheets of paper. Skip to a new page if you skip a problem, so you can put your answers in order in the final submission.
Abbreviated reference for Code World:
- Point is (Double, Double)
- pictures :: [Picture]->Picture
- circle r, rectangle width height
- solidCircle, solidRectangle, colored, translated x y, scaled x y
- Event : TimePassing 4.2, KeyPress “A”, PointerMovement (x,y), PointerPress (x,y)
-
In your Code World animation, you want to keep track of a Point (pos), a Point for the direction (dir), a list of Points (pts), and a Double (gtime). Write a data definition for
Game
that allows you to include these elements in separate fields with the accessors given. -
Your player is supposed to move in the direction
dir
every 1.0 units of game time. Write a part of the event handler that will update the game struct as time passes. (Update the game time as well!) Use the record update syntax if you know it.evth :: Event -> Game -> Game evth (TimePassing p)
Example: position is (0,1), direction is (3,4), and 1.0 units of time pass: the position is then (3,5). Position is (10,8), direction is (1,-1), and 3.2 units of time pass: player is then at (13.2,4.8).
-
There are two items we put in most of our
deriving
clauses. Explain what they do. Briefly!Eq
Show
-
Make your
Game
a member of theOrd
typeclass by comparing only thegtime
and ignoring the rest of the items. -
Using only your “Stone Age” programming skills, write the replicate function
myreplicate :: Int -> a -> [a]
-
Use
foldl
to remove duplicates from a list. Write your own fold helper function and complete the function call.undup [1,1,1,2,3,2] == [1,2,3] undup xs = foldl ...finish me...
-
Could this be the signature of a foldl helper function? Explain why not or give an example of how to use it.
seven :: Int -> [Int] -> Int
-
The class definition for
Functor f
requires a functionfmap :: (a -> b) -> f a -> f b
Explain why the following definition does not make
Int
into a functor:g x = x*x fmap g x = x*x
-
In an Advent of Code problem, you are presented with a matrix of unknown dimensions stored in a file
f
.10 30 50 70 21 43 65 87 37 74 111 148
Use
Matrix
as an abbreviation for the type[[Int]]
.type Matrix = [[Int]]
Write the function
getMatrix :: String -> Matrix
that receives as input the result fromreadFile
and produce a matrix of numbers.exInput="10 30 50 70\n21 43 65 87\n37 74 111 148\n" getMatrix exInput == [[10,30,50,70],[21,43,65,87],[37,74,111,148]]
Show an example of what any helper function does. Try to keep this straightforward instead of overcomplicating it.
-
Given a matrix of numbers, the function
isArith
determines if each row is an “arithmetic sequence”. That means that within a given row the numbers increase by the same amount every time you move one column to the right. The amounts being added can be different for different rows.In the example above:
- the first row is created by adding 20 each time, so it is good;
- the second row is created by adding 22 each time, so is it is good;
- the third row is created by adding 37, so it is good.
Since every row is created by adding a constant to the first number,
isArith
returns true.Write
isArith :: Matrix -> Bool
. -
The function
toSquare :: Matrix -> Matrix
adds rows or columns of zeros to make a square matrix. WritetoSquare
. You may assume the input matrix is a rectangle. Extra credit for handling non-rectangles.In the example above, the output of
toSquare
would be:10 30 50 70 21 43 65 87 37 74 111 148 0 0 0 0
Another example would be a 1 column matrix turning into a square matrix:
10 10 0 0 toSquare 21 == 21 0 0 37 37 0 0
-
The function
deleteCol :: Int -> Matrix -> Matrix
takes in a numberk
and removes columnk
from the matrix, beginning the count with column 0.Using the example immediately above,
deleteCol 1
produces:10 50 70 21 65 87 37 111 148 0 0 0
-
(
matrixBuild
) Given a list ofMaybe Int
, create a matrix by appending each number to the “current row” and going to the next row when you hitNothing
.Example:
matrixBuild [Just 10, Just 50, Just 70, Nothing, Just 21, Just 65, Just 87] == [[10,50,70],[21,65,87]]
Notes
I did not like matrixBuild
as a demonstration of Maybe
skills.
Look for another way to test that.