2024 Review A
-
(
q1
) You get a list ofMaybe Int
Give out a list ofInt
: all of the numbers that square to something be less than 100. Write a type signature and the function. -
(
q2
) You get a list ofMaybe Int
. Return their sum. Count -5 for eachNothing
. -
Define a data using the field names in the definition the data should be called
Game
and contain aPoint
calledpos
, anInt
calledscore
, and a list ofPoint
calledenemies
. -
(
ce: Game -> Int
) Count how many enemies are in the game -
(
gold : Game -> Game
) Increase the score by 20. Write your code in a way that will not change if you add more fields to theGame
. -
(
ke: Game -> Point -> Game
) remove any enemy within 10 of the given point. -
Can this be the signature of a fold helper function?
f :: Maybe Double -> Point -> Maybe Double
Explain how you could use it, or explain why not.
-
(
sm: Game -> Game
) update the game by sorting the list of enemies based on how close they are topos
. -
What is the purpose of a
StdGen
? Why doesrandomR
output aStdGen
? -
Modify (or pretend you modified) your
Game
record to contain aStdGen
calledrng
. Write a functionteleport :: Game -> Game
that moves yourpos
to a random location with -9 <= x <= 9 and -5 <= y <= 5. -
The type
a
being in theEq
typeclass means there is a function:(=) : a -> a -> Bool
Make your modified
Game
type an instance ofEq
by checking everything except therng
for equality. -
Saying a type
b
isBusy
means there is a functiongrind :: b -> Double -> b
and also a functionearn :: b -> Double -> Double
. Write the class definition. -
Given
data SW = StudentWorker{studyHours :: Double, jobHours :: Double}
Make
SW
into an instance ofBusy
by havinggrind
add tostudyHours
. Theearn
function for SW multipliesjobHours
by the double given, which is interpreted as dollars per hour.