2023 Sem I Final Exam B
Definitions: a “higher order function” is a function that takes another function as input. Chapter 6 contains the main discussion of these functions.
Locate
-
The
Locate
class has two methodsloc
andmoveto
.- The method
loc
that takes in a single thing in the class and puts out a pair of integers(Int,Int)
. - The
moveto
method takes in an(Int,Int)
and an item in the class, and puts out an item of the same type.
Write the class declaration for
Locate
. - The method
-
Write code so that
Block
is in theLocate
classdata Block = Block (Int,Int)
Inexact
The definition of Inexact
below is intended to tag results with a
plus or minus tolerance (like in science class).
data Inexact a = Inexact {val :: a, tolerance :: Double}
-
Write code to make an inexact point
ept
at $(3.5, 7.03)$ with a tolerance of $\pm 0.05$.type Point = (Double, Double)
-
Write code to make
Inexact
into aFunctor
.
Stone Age and Beyond
The substitutor function sbt
, takes in a list of ordered pairs and one
integer x-coordinate that is supposed to be changed to another.
sbt :: Int -> Int -> [(Int, Int)] -> [(Int,Int)]
sbt origval newval xs = []
ex_sbt = sbt 10 5 [(10,15),(30,10),(10,40),(40,15)] ==
[( 5,15),(30,10),( 5,40),(40,15)]
-
Write substitutor using nothing but recursion, pattern matching, and basic functions from early in the book. Avoid
fst
,snd
,head
, andtail
for full credit. -
Write the same function using a higher order function.
Random
A Trap
is a block that triggers other blocks tagged with the same
string to fall. Trap
is also in the Locate
class, but you do
not need to write that code.
data Trap = TrapBlock String (Int. Int)
data TrapGame = TrapGame {myrng :: StdGen, traps :: [Trap]}
-
The function
newtrap :: String -> TrapGame -> TrapGame
that adds a new trap with the given name to the game. The x and y coordinates for the trap should be random numbers from 4 through 20.before_tg = TrapGame ... -- code omitted before_check = (traps tg) == [TrapBlock "FireArrow" (4,8)] after = newtrap "PressurePad" tg
Write the
newtrap
function.
Misc
-
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.
-
Programming
-
(
diagonalRun
) A diagonal run is a list of coordinates $(x_i, y_i)$ so that $x_{i+1} = 1 + x_i$ and similarly $y_{i+1} = 1 + y_{i}$. In the coordinate plane these would be points along a line of slope one.Given a list of ordered pairs of ints, find the length of the longest diagonal run.
Examples:
easy = [(0,0),(1,1),(2,2),(3,3),(4,4)] ++ [(1,-2),(2,-1),(3,0)]
easy_result = (diagonalRun easy == 5)
hard = [(4,4),(2,-1),(0,0),(1,-2),(1,1),(3,0),(2,2),(3,3)]
hard_result = (diagonalRun hard == 5)