2023 Exam Review II
Semester I Final
Vocabulary: a “higher order function” is a function that takes another function as input. You saw them in Chapter 6.
-
(
q1
) Read part (b) before writing part (a)-
Write a very basic (“Stone age”) function that uses only recursion to join all of the words in a list together with hypens. Notice that there is no hyphen at the end.
check_q1a = q1 ["apple","cheese","egg"] == "apple-cheese-egg"
-
Instead, number each gap. If you can do this, you may skip the first part. There is no number at the end.
check_q1b = "apple1cheese2egg3giardinera" == q1 ["apple","cheese","egg","giardinera"]
-
If you used
head
ortail
in your code, explain how you could have done without them.
-
-
Duplicit y
means that there are methodslie :: y -> Int cheat :: y -> y
-
Write the class declaration for
Duplicit
. -
Given the declaration below, put
Pos a b
in theDuplicit
class by using a+b forlie
and switching a and b forcheat
.data Pos = Pos Int Int
-
-
(Randomness.) You are writing an animation that shows a random size solid blue square. The dimension of the square changes every time you click the mouse.
data SqGame = SqGame {side :: Double, mygen :: StdGen} drawh :: SqGame -> SqGame drawh m = colored blue $ solidRectangle (side m) (side m)
Write an event handler that makes the size be a random double $1\le s \le 5$.
-
The
template
is a list of numbers that you think of as moving all together on the number line for example by subtracting 3 from each one.template = [100, 150, 120] result = (moveit 3 template == [97, 147, 117])
The
stops
is a specific list of numbers that you are trying to reach with thetemplate
. For example, a stops list could bestops = [20, 80, 147, 190]
The
seeker
function takes in a template and a stops list, and returns the smallest positive integerk
so that at least one element of the template moved to the leftk
places on the number line is also in the stops list.seeker :: [Int] -> [Int] -> Int seeker template stops = 0 -- wrong exs = (seeker [10,29,38] [5,15,25,35] == 3)
Write the seeker function.
-
The
uncurry
function has a signatureuncurry :: (a->b->c) -> (a,b) -> c
- Briefly explain the meaning of this signature.
- Give an example of one way this function could work.
-
In this question, ideally you will demonstrate you understand how to use higher order functions (LYAH, Chapter 6).
The function below takes in two positive integers $(r,s)$ with $s$ less than $r$ and produces a Pythagorean triple.
p2A :: (Integer, Integer) -> (Integer, Integer, Integer) p2A (r,s) = ((r-s)*(r+s), 2*r*s, r*r+s*s) [(3,4,5),(6,8,10),(5,12,13),(8,15,17),(12,16,20),(7,24,25), ...]
Using that function, do each of the following:
-
List some Pythagorean triples: Create a list
pB
that hasp2A
applied to every integer value of $1 \le r \le 10$ and $1 \le s < r$. -
(
p2B
) Some of the Pythagorean triples come out not in ascending order, like $r=4$ paired with $s=1$ gives(15,8,17)
. Mathematically, the third element of the ordered triple is always greater than either of the other two. Write a functionfixp2
that will put a triple in ascending order. -
Show how to use each of the approaches listed below to apply the
fixp2
function to the elements ofp2
.- list comprehension
- advanced function from chapter 6
-
(
p2C
) Some of the triples have a greatest common divisor that is not 1. We want to get rid of those. Do this with a higher order function.A reminder that the signature of gcd is:
gcd :: Integer -> Integer -> Integer
-
Next time ask students to write the
gcd
function as well.
-