Problem 1: Suppose that the following list of
bindings was entered into the OCaml interpreter in the
sequence shown. For each binding, write down :
(a) If
the expression is accepted, the value bound ("fun" for
functions) and its type,
(b) If the expression is
rejected due to a type error, "type error",
(c) If the
expression is rejected due to an unbound variable, the name
of the variable that is not bound. Recall that if a type
error occurs, then the variable binding does not happen.
Check your answers by entering this sequence into the
interpreter.
- let foo (f,g) = fun x -> (f x,g x);;
- let a = (foo ( (fun x -> x*x), (fun x -> [x]) )) 4;;
- let b = (foo ( (fun x -> -x), (fun x -> 0::x) )) (4,[4]);;
- let bar (f,a) = fun b -> f (a,b);;
- let sum (a,b) = a+b;;
- let c = (bar (sum,1));;
- let d = c 2;;
- let f = (bar (bar,bar)) sum;;
- let g = f 4;;
- let h = g 4;;
- let i = h 4;;
- let j = i 4;;
Problem 2:
In each part below you are required to complete the implementation of a
function by filling in the appropriate expressions in the blanks (parts marked
...
).
let rec mapFns (fs,xs) =
match (fs,xs) with
([],_) -> ...
| (_,[]) -> ...
| (f::fs,x::xs) -> ...
let
compose (f,g) = ... ;;
let rec iterate (f,n) =
match n with
0 -> ...
| _ -> compose ((...),(...))
;;