# Sample midterm solutions

26 October 2017

You have one hour to complete these questions. Answer on the paper given. If you need additional paper let me know, but you must turn it in also. You may not use a computer or calculator. All notes and electronic devices must be put away.

thwok 'a' = 'y'
thwok 'n' = 'z'
thwok 'm' = 'g'
thwok  _  = 's'
1. The preceding function definition, thwok, uses pattern matching on characters. What is the result of each of these expressions?

1. thwok 'm''g'

2. thwok 'y''s'

3. map thwok "panama""syzygy"

2. Write down a type signature for thwok.

thwok :: Char -> Char

flim x
| even x = 2*x + 1
| x > 10 = 3*x - 2
| otherwise = x + 1
1. The preceding function, flim, uses Boolean guards to distinguish three cases. What is the result of each of these expressions?

1. flim 25

2. flim 1131

3. flim 34

4. map flim [8..12][17,10,21,31,25]

square x = x*x
eek y 0 = 1
eek y z
| even z = eek (square y) (z div 2)
| otherwise = y * eek y (z - 1)
1. Use the preceding functions, square and eek, to derive the result of the following expression. square does exactly what it says. eek is recursive and uses both pattern-matching (on zero) and guards.

eek 3 5

eek 3 5 →
3 * eek 3 4 →
3 * eek 9 2 →
3 * eek 81 1 →
3 * 81 * eek 81 0 →
3 * 81 * 1 →
243

grup :: [Integer] -> [Integer]
grup = filter (< 20)
bink :: [Integer] -> Integer
bink = sum . grup . take 5
bonk :: [Integer] -> Integer
bonk = sum . take 5 . grup
1. The preceding functions are defined using partial application and function composition. What is the result of each of these expressions?

1. bink [80,13,92,19,15,-7]47

2. bonk [80,13,92,19,15,-7]40

3. bink []0

4. bonk [18..25]37

1. Recall that a type is a Functor if it has a function fmap that can apply a function to its element type(s). For lists, fmap is the same as map. But fmap also works on Maybe types, the Right side of an Either type, and the second element in a pair. That is, we have all these instances:

fmap :: (a -> b) -> [a] -> [b]
fmap :: (a -> b) -> Maybe a -> Maybe b
fmap :: (a -> b) -> Either c a -> Either c b
fmap :: (a -> b) -> (c, a) -> (c, b)

What is the result of each of these expressions?

1. fmap (*2) (Just 5)Just 10

2. fmap (*2) (Left 5)Left 5

3. fmap (*2) (Right 5)Right 10

4. fmap (*2) (5,6)(5,12)

5. fmap (+1) \$ fmap (*2) [1..4][3,5,7,9]

pelt :: [Integer] -> [Integer]
pelt [] = 
pelt (h:t) = h : h : pelt t
1. The preceding function, pelt, is recursive and uses pattern-matching on a list argument. What is the result of each of these expressions?

1. pelt []

2. pelt [6,6,0]

3. pelt [7,2][7,7,2,2,0]

korn :: String -> String
korn = zipWith max "jjjjj"
1. The preceding function, korn, is defined as a partial application using zipWith and max. Recall that max returns the greater of its two arguments:

• max 3 55
• max 'a' 'b''b'
• max 'z' 'k''z'

What is the result of each of these expressions?

1. korn "hello""jjllo"

2. korn "quiz""qujz"

3. korn "haskell""jjskj"

main = putStrLn "All done!"

# Extra questions

The above indicates the approximate length of the real exam, but here are some additional practice questions.

quan :: [a] -> [a]
quan [] = []
quan (h:t) = quan t ++ [h]
1. Recall that (++) is the list concatenation operator.

• "test" ++ "two""testtwo"
• [1..5] ++ [8..10][1,2,3,4,5,8,9,10]

What is the result of each of these expressions?

1. quan "hello""olleh"

2. quan [2..5][5,4,3,2]

twee :: [Integer] -> [Integer]
twee = map (+3)
florm :: [Integer] -> [Integer]
florm = filter even
pink :: [Integer] -> Integer
pink = sum . florm . twee
ponk :: [Integer] -> Integer
ponk = sum . twee . florm
punk :: [Integer] -> Integer
punk = sum . twee . florm . twee
1. The preceding functions are defined using partial application and function composition. What is the result of each of these expressions?

1. pink [4..7]18

2. ponk [4..7]16

3. punk [4..7]24