1) To give you an idea of what to expect at the final. The final will contain one exercise for each of the groups of topics (I, II etc.) described in this page.
2) To serve for you as preparation for the final: The exercises in this page are of the same level of difficulty, or slightly higher (and generally longer), than those which will be given in the final. So if you can solve well all of them you have a good chance of doing very well at the final. For several of these exercises, I wrote also the solution here. It is of course recommanded, for your maximum benefit, that you try to solve the exercise yourself and only afterwards look at my solution and compare it with yours.
- Catuscia Palamidessi
Type ::= TVar | Type -> Type | Type * Type
Show that this grammar is ambiguous by giving two derivation trees for each of the following type expressions:
int fact(int x) { if (x == 0) return 1;
else return fact(--x) * x;
}
a) What is the result returned by the call fact(3) ?
b) Would the definition be correct if we substitute the command
return fact(--x) * x;
with
{ int y = x; return fact(--x) * y; } ?
z : integer;
procedure p (x:integer) body
write a body for p so that the following piece of code
z := 1 ; p(z) ; write(z)
will have all different outcomes in case of pass by value, pass by reference,
and pass by value-result.
procedure p;
begin
x: integer;
procedure q; x := x+1;
procedure r; begin x: integer in
x := 1; q ; write(x) end
in
x:= 2; r
end
What is the output produced by calling p
in
a) A language with static scope
b) A language with dynamic scope
void p() {int x; q = &x; *q = 1; }
where q is a global variable declared as pointer to int and initialized by q = new int. What output can we expect to be produced by the following piece of code: *q = 0 ; p() ; printf(*q) ; ?
IV. Exercises on types
Some of these operations can be implemented as constructors and the others as functions. Be careful about choosing the right set of constructors!
V. Exercises on Functional Programming
curry takes a function f of two arguments paired (i.e. defined as f(x,y) = e) and gives back the equivalent curried function (i.e. the function g such that g x y = e). uncurry does the reverse, i.e. takes a function like g and gives back f.
VI. Exercises on Logic Programming
Define a predicate infix_visit(T, L) which holds if L is
the list of nodes produced by the infix visit of the binary tree T.
type(app(E1,E2),B,R) :- type(E1,A->B,R), type(E2,A,R). /* application */
cannot be translated so easily in ML. (It is possible, but it
require to write a lot of additional code). Try to explain why.
class C {
private: int x;
protected: int y;
public: int z;
};
class C1: public C {
private: int x1;
public: int y1;
};
class C2: private C {
private: int x2;
public: int y2;
};
Assume that in main there is the following declaration:
C1 o1;
For each of the following expressions in main, say whether they are allowed or not (they can be forbidden either because they violate the class definition or the protection mechanism)
o1.x , o1.y , o1.z , o1.x1 , o1.y1 , o1.x2 , o1.y2
template <class T> cell {
private: T info;
public: void set(T x){ info = x; }
T get() { return info; }
};
Define the subclass coloured_cell by extending the class
cell with:
- a field colour, indicating the colour of the cell, represented
as a character ("w" for white, "b" for black, "r"
for red, etc.),
- the method set_colour, which set the content of the
field colour, and
- the method get_colour, which returns the content
of the field colour.
Choosing the right signature, types and parameters is part of
the exercise.