Uploaded by Jeff Erhabor

comp prog

advertisement
Comparative Programming
January 2018 Paper
1. Given the λ-terms: I = λx.x, T = λx.xIII and U = λzy.y(λx.xz):
(a) Write the λ-term T U out in full (without abbreviations, and including
ALL brackets).
[5 marks]
(b) Draw the β-reduction graph of the λ-term T U .
[10 marks]
(c) By building a type derivation, find the type of the term U .
[25 marks]
(d) For λ-terms X and Y , explain how the pair (X, Y ) can be represented as
a λ-term. Include in your answer λ-terms to build and to project each
component of a pair. Write (T, U ) in your chosen encoding.
[10 marks]
2. (a) Write a function rev in Haskell syntax that uses an accumulating
parameter to reverse a list. Include the type of your function in your
answer.
[10 marks]
(b) Write a function equal in Haskell syntax that takes two lists of elements
(where each element has a type that is an instance of the Eq class) and
checks whether they are equal (i.e., returns True if they have exactly the
same elements in the same order, False otherwise). Give the most general
(polymorphic) type for equal.
[10 marks]
(c) Using equal and rev write a function palindrome that checks whether a
list is a palindrome. A list is a palindrome if the list is the same in reverse.
The lists [1,0,0,1], [True, False, True] and [0,1,2,3,3,2,1,0] are examples of
palindromes.
[10 marks]
(d) Write a set of Prolog clauses that can be used to test if a list is a palindrome.
State clearly what your predicates represent.
[10 marks]
(e) Using your answer to part (d), give two example SLD trees to demonstrate
your clauses, using the lists: [1,0,1] and [1,0,0].
[10 marks]
3. (a) Explain the concept of dynamic lookup. To what extent is this concept
related to overloading?
[10 marks]
(b) Explain the main problem associated with multiple inheritance, and
possible solutions to this problem. Provide examples to support your
explanations.
[15 marks]
(c) Explain how to write the Haskell function
f x = if x==0 then 1 else x*f(x-1)
1
G6021
Comparative Programming
as the fixed point of a functional. Use the syntax of PCF, and include
types in your answer.
[10 marks]
(d) Define CPS, and explain how to convert the Haskell function:
f x = if x==0 then 1 else x*f(x-1)
so that it can compiled into a loop (i.e., without recursion).
2
[15 marks]
End of Paper
Download