2023 Exam Review I

I. Class and Instance

The Dilate class requires a function dilate that takes in a double (the dilation factor) and an instance of the Dilate class, and puts out a new instance of the dilate class.

  1. Write the Dilate class declaration.

  2. A Point3D is defined as below. Make Point3D an instance of the Dilate class.

     newtype Point3D = Point3D (Double, Double, Double)
     exA = Point3D (5,12,13)
    
  3. Write the function dilateList which takes in double and a list of items in a class that can Dilate, and puts out a list of the dilated items.

    1. What is the type signature of dilateList?
    2. Write the function dilateList.
  4. (Advanced, optional.) If you can Dilate a class, you can also Dilate the Maybe version of that class. Write an instance declaration that conveys this fact.

  5. Suppose that a kind of line is determined by a point (Double, Double) and a slope Double. Write a data definition for Line with accessors pt and slope.

  6. When you dilate a line the point changes but the slope does not. Write an instance declaration for this fact.

II. Stone Age

  1. The mynub function removes duplicates from a list, not otherwise changing the order. Write the mynub function using only “stone age” tools.

    check_mynub = ([1,2,3,5] == mynub [1,2,3,2,2,2,2,5,1])
    
  2. The dropUnwrap function takes in a list of Maybe b and puts out a list of b. Items that are Nothing are eliminated.

     check_du = ([5,30] == dropUnwrap [Just 5, Nothing, Just 30, Nothing])
    

III. Iron Age

In this section you are supposed to use map, filter and foldl (or any of its variants).

  1. (findy :: Int -> [(Int,Int)] -> [(Int,Int)]). Return all of the points that have the given y value.

  2. (pythagsum :: [(Int,Int)] -> Int) Find the sum of (x squared + y squared) for each point in the input list.

Last modified December 15, 2023: Decent exam review. (24eadc7)