2024 Review A

  1. (q1) You get a list of Maybe Int Give out a list of Int: all of the numbers that square to something be less than 100. Write a type signature and the function.

  2. (q2) You get a list of Maybe Int. Return their sum. Count -5 for each Nothing.

  3. Define a data using the field names in the definition the data should be called Game and contain a Point called pos, an Int called score, and a list of Point called enemies.

  4. (ce: Game -> Int) Count how many enemies are in the game

  5. (gold : Game -> Game) Increase the score by 20. Write your code in a way that will not change if you add more fields to the Game.

  6. (ke: Game -> Point -> Game) remove any enemy within 10 of the given point.

  7. 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.

  8. (sm: Game -> Game) update the game by sorting the list of enemies based on how close they are to pos.

  9. What is the purpose of a StdGen? Why does randomR output a StdGen?

  10. Modify (or pretend you modified) your Game record to contain a StdGen called rng. Write a function teleport :: Game -> Game that moves your pos to a random location with -9 <= x <= 9 and -5 <= y <= 5.

  11. The type a being in the Eq typeclass means there is a function:

    (=) : a -> a -> Bool
    

    Make your modified Game type an instance of Eq by checking everything except the rng for equality.

  12. Saying a type b is Busy means there is a function grind :: b -> Double -> b and also a function earn :: b -> Double -> Double. Write the class definition.

  13. Given

    data SW = StudentWorker{studyHours :: Double, jobHours :: Double}
    

    Make SW into an instance of Busy by having grind add to studyHours. The earn function for SW multiplies jobHours by the double given, which is interpreted as dollars per hour.

Last modified December 21, 2024: End of semester review and exams. (8a5ce22)