25 January 2015/R1
Name:
Group:
Pages:
Exam in Functional Programming
Lecturer Adrian Groza
Time: 14-17. The exam is closed-book, closed-notes, open-mind. Write clearly; unreadable = wrong!
If we observe any attempt of deception, the whole exam will be evaluated to 0 points.
Available Points
Achieved Points
1.
Expression evaluation.
(a)
(b)
(c)
(d)
Exercise 1
20
Exercise 2
25
Exercise 3
25
Exercise 4
20
Sum
90
Write the type proles for the following ML function denitions:
(2)
(2)
(2)
(2)
fun dist(vel, seconds) = vel * (seconds div 3600);
fun foo(x,y) = x::y;
fun bar(x,y) = x;
fun combine(nil, nil) = []
| combine(x::xs, y::ys) = (x,y)::combine(xs, ys);
(e) fun flattenTups(nil) = nil
(2)
(f) fun baz((x,y), (p,q)) = [x,y,p,q];
(g) fun twice f x = f (f x);
(h) fun ugly f1 f2 L = ((f1 L), (f2 L));
(4)
(2)
(4)
| flattenTups((x,y)::rest) = x::y::flattenTups(rest);
2.
Using list comprehension implement the following functions:
(a) Count positive numbers in a list: countPositives :: [Int] -> Int
(b) Get a list of all 'tails' of a list: suffixes :: String -> [String]
(c) Write a function c :: [Int] -> Int that returns the product of the dierence of successive elements in the list. If the list has one element the function should return one.
List comprehension.
c
c
c
c
c
3.
[3,1,4,2,5]
[2,4,6,8] =
[1,2,2,1] =
[-1,2,-3,4]
[42] = 1
= (3-1) * (1-4) * (4-2)
(2-4) * (4-6) * (6-8) =
(1-2) * (2-2) * (2-1) =
= ((-1)-2) * (2-(-3)) *
* (2-5) = 36
-8
0
((-3)-4) = 105
Structural induction.
(a) Dene the functions preorder on a tree and length of a list.
(b) Denine the function nonodes which returns the number of nodes of the input tree.
(c) Prove the following theorem: length (preorder t) = nonodes t. Show the axiom used at
step in your demonstration.
4.
(5)
(5)
(15)
each
(5)
(5)
(15)
Lambda Calculus.
(a) Apply -reduction to the following expression as much as possible: (z.z) (y.y y) (x.x a)
(b) Given:
not = x.((x false) true)
true = x.y.x
false = x.y.y
Prove the following: not (not true) = true. Indicate at each step the reduction applied.
(5)
(15)