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.
-
Write the
Dilateclass declaration. -
A
Point3Dis defined as below. MakePoint3Dan instance of theDilateclass.newtype Point3D = Point3D (Double, Double, Double) exA = Point3D (5,12,13) -
Write the function
dilateListwhich takes in double and a list of items in a class that canDilate, and puts out a list of the dilated items.- What is the type signature of
dilateList? - Write the function
dilateList.
- What is the type signature of
-
(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.
-
Suppose that a kind of line is determined by a point
(Double, Double)and a slopeDouble. Write a data definition forLinewith accessorsptandslope. -
When you dilate a line the point changes but the slope does not. Write an instance declaration for this fact.
II. Stone Age
-
The
mynubfunction removes duplicates from a list, not otherwise changing the order. Write themynubfunction using only “stone age” tools.check_mynub = ([1,2,3,5] == mynub [1,2,3,2,2,2,2,5,1]) -
The
dropUnwrapfunction takes in a list ofMaybe band puts out a list ofb. Items that areNothingare 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).
-
(
findy :: Int -> [(Int,Int)] -> [(Int,Int)]). Return all of the points that have the given y value. -
(
pythagsum :: [(Int,Int)] -> Int) Find the sum of (x squared + y squared) for each point in the input list.