## Solutions – Practical 1

February 2, 2010

This practical should have been straightforward, once you got to grips with the editor, Unix, and interaction with the ML system.

A simple answer to Question 2 would be

 fun FF 0 = 1    | FF 1 = 3    | FF 2 = 5    | FF n = 1 + FF(n-1) + FF(n-3)

But this would take aeons to compute FF(200), so the iterative version given is necessary.

Since there was some confusion over what was intended (despite the fact that the instructions were clear), answers that counted internal nodes, rather than nodes, were also marked correct.

structure Answers1 : A1 = struct
(* Question 1 *)
fun C (n,r) = if r <= 0 then 1
else n * C(n-1, r-1);

(* Question 2 - improved *)
local
fun FFtriple 0 = (1,3,5)
| FFtriple n =
let val (a, b, c) = FFtriple (n-1)
in
(b, c, a+c+1)
end
in
fun FF n = let val (a,_,_) = FFtriple n
in a
end
end;

val FF20  = FF 20
and FF200 = FF 200;

(* Question 3 *)
fun G(0, b) = 1
| G(n, b) =
if n <= b then 2 + G(n-1, b)
else G(n-1, b) + G(n-1-b, b) + 1;

(* Question 4 *)
fun RB 0 = 1
| RB n = RB(n-1) + RB(n div 2) + 1;

val RB100 = RB 100;
end;

(C) Michael Fourman 1994-2006