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
Dilate
class declaration. -
A
Point3D
is defined as below. MakePoint3D
an instance of theDilate
class.newtype Point3D = Point3D (Double, Double, Double) exA = Point3D (5,12,13)
-
Write the function
dilateList
which 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 forLine
with accessorspt
andslope
. -
When you dilate a line the point changes but the slope does not. Write an instance declaration for this fact.
II. Stone Age
-
The
mynub
function removes duplicates from a list, not otherwise changing the order. Write themynub
function using only “stone age” tools.check_mynub = ([1,2,3,5] == mynub [1,2,3,2,2,2,2,5,1])
-
The
dropUnwrap
function takes in a list ofMaybe b
and puts out a list ofb
. Items that areNothing
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).
-
(
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.