Homework answers / question archive /
CS-205 – Declarative Programming
Question 1: Functional Programming
Write a Haskell function that takes the surface of a cube as input and computes the volume of the cube
CS-205 – Declarative Programming
Question 1: Functional Programming
Write a Haskell function that takes the surface of a cube as input and computes the volume of the cube
Computer Science
Share With
CS-205 – Declarative Programming
Question 1: Functional Programming
- Write a Haskell function that takes the surface of a cube as input and computes the volume of the cube. Within your function you should first compute an intermediate result.
[4 marks]
- Briefly explain how interactive programs can be written in Haskell. You may use as example a program that reads a string from the keyboard and writes its reverse on the screen. No complete Haskell code is required. Pseudo code or informal explanations are fine.
[6 marks]
- Write a Haskell function countLarge that counts the numbers in a list of integers that are larger than 100.
Define countLarge in three ways, using
-
- recursion;
- list comprehension;
- higher-order functions.
- marks]
- What are the most general types of the following functions and expressions?
- increasingPair (x,y) = x < y
- g x y = 2*x + y
- [( &&),(|| )]
[3 marks]
- Consider the following data type of trees and the function flatten that flattens a tree into a list:
data Tree = Leaf | Node Tree Int Tree
flatten :: Tree -> [ Int ] flatten Leaf = []
flatten (Node l n r) = flatten l ++ [n] ++ flatten r
A tree is called search tree if it flattens to a list that is ordered.
-
- Give two examples of trees such that one of them is a search tree but the other isn’t. Compute the flattenings of both trees.
- Write a function insert such that if n is an integer and t is a search tree then insert n t is again a search tree that contains the nodes of t and in addition the integer n. If n occurs already in t, then insert n t should be equal to t.
- marks]
Question 2: Programming in Prolog and Verification in Haskell
- Indicate for the following pairs of terms whether they are unifiable or not. If a pair is not unifiable, explain why this is the case. If a pair is unifiable, give a unifier.
- isMotherOf(elisabeth,X) and isFatherOf(phillip,charles).
- isMotherOf(elisabeth,X) and isMotherof(Y,harry).
- 2+X and 5.
- smaller(X,double(X)) and smaller(double(10),Y).
- [X|Y] and [a,b,c,d].
[5 marks]
- Proof search: Explain which proof search strategy Prolog has, give its advantages and describe how it works by using the following example. Draw a proof tree for lucky(X) and indicate which paths yield solutions.
rich(gareth). plumber(gareth). knows(gareth,mari). lucky(X) :- knows(X,Y), plumber(Y). lucky(X) :- knows(Y,X), plumber(Y). lucky(X) :- rich(X).
[7 marks]
- Programs with Accumulator: Consider the following Prolog program:
len([],0).
len([_|L],N) :len(L,N1),
N is N1 + 1.
mystery([],0).
mystery([L|Ls],K) :mystery(Ls,K1), len(L,N),
K is N + K1.
-
- What does the query ?- mystery([[2],[1,1],[0,0,0]],K) yield?
- Show how this program can be modified to be tail recursive using an accumulating parameter in a program mysteryA(Ls,A,K) where A stores intermediate results, so that mystery2(Ls,K), defined by
mystery2(Ls,K) :- mysteryA(Ls,0,K).
will yield the same results as mystery(Ls,K).
[6 marks]
- Program Verification in Haskell: Consider the following definitions:
(++) :: [a] -> [a] -> [ a ]
[] ++ ys = ys
(x:xs) ++ ys = x : (xs ++ ys)
f :: [a] -> [ a ] f [] = [] f (x:xs) = f xs ++ [ x,x ]
Prove by induction on lists xs that
f (xs ++ ys) = f ys ++ f xs.
In the proof you may use that following laws hold for all finite lists xs and ys.
-
- (xs ++ ys) ++ zs = xs ++ (ys ++ zs)
- xs ++ [] = xs