Quiz 3

Statistics:

mean     21.909 (241.000/11)
stddev   5.648
median   23.000
midrange 17.750-25.000

#   avg
1   7.73 / 10
2   4.36 / 10
3   7.82 / 10
 + 2-point bonus

Question 3-1

Write a Haskell function countZeroes that takes a list of integers as a parameter and returns the number of zeroes contained in the list. For example, if I called countZeroes [5,0,0,8,0], it should return 3, since the list contains three zeroes.

Question 3-2

Write a Haskell function named doubleFunc which takes as its parameter a function f from integers to integers and returns a function from integers to integers which is always twice f. For example, using this function I should be able to do the following.

f x = x + 5
g x = 3 * x
fd = doubleFunc f
gd = doubleFunc
With these definitions, fd 3 should return 2 f(3) = 2 (3 + 5) = 16$ and gd 10 should return 2 (3 * 10) = 60.

Question 3-3

Reduce the following lambda expression, showing your intermediate steps. It should reduce to a number.

(λf.λa.f(a + 3) - (f a) - (f 3)) (λx.x * x)) 7

Solutions

Solution 3-1

countZeroes [] = 0
countZeroes (0:xs) = 1 + countZeroes xs
countZeroes (_:xs) = countZeroes xs

Solution 3-2

doubleFunc f = \x -> 2 * f(x)

Solution 3-3

(λf.λa.f(a + 3) - (f a) - (f 3)) (λx.x * x)) 7
(λa.(λx.x * x) (a + 3) - (λx.x * x) a - (λx.x * x) 3) 7
(λx.x * x) (7 + 3) - (λx.x * x) 7 - (λx.x * x) 3
(7 + 3) * (7 + 3) - (λx.x * x) 7 - (λx.x * x) 3
(7 + 3) * (7 + 3) - 7 * 7 - (λx.x * x) 3
(7 + 3) * (7 + 3) - 7 * 7 - 3 * 3
10 * 10 - 7 * 7 - 3 * 3
100 - 7 * 7 - 3 * 3
100 - 49 - 3 * 3
51 - 3 * 3
51 - 9
42