Last modified: Wednesday, February 1, 20:40
Please be sure to be using Ocaml 3.12
n_timesand uses semicolons rather than commas in
This problem set has three parts. You should
write your solution to each part in a separate file:
part3.ml. To help get you started,
we have provided a stub file for each part on CMS. You should download and
edit these files. Once you have finished, submit your solution using CMS,
linked from the course website.
Give the types of each of the following OCaml expressions. For
example, the type of
(1, 2) is
int * int.
[(3 , 4); (5, 6)]
fun f (z, v, t) -> if f z == "inf" && v t == "mar" then "win" else "lose"
char list option
bool -> 'a -> ('b -> 'a) -> 'b -> 'a
('a -> 'a) -> 'a -> 'a
char * int * float -> int * string -> int
??? with an expression that (1) makes the code
type check correctly and (2) does not cause the code to raise an
exception. Give the type of the expression you chose.
let num = ??? in let first = num / 100 in let second = num mod 100 in let third = first - second - second - second in let ans = first + second + third in if ans != 42 then failwith "argh"
let nums = ??? in let filterer zardoz = List.filter (fun x -> x > 0) zardoz in let splitter zardoz = let (a,b) = List.split zardoz in b in let zardoz = filterer (splitter nums) in if List.length zardoz = 0 then failwith "argh"
The following function executes correctly, but it was written with poor style. Rewrite it with better style. Please consult the CS 3110 style guide.
let rec zardoz f lst = if (List.length lst == 0) != false then  else if (List.length lst == 1) == true && true
then [f (List.hd lst)] @ zardoz f  else let hd = List.hd lst in let tl = List.tl lst in let v = f hd in let lstdv = [v] in let rest = zardoz f tl in lstdv @ rest
cond_dup : 'a list -> ('a -> bool) -> 'a listthat takes in a list and preciate and duplicates all elements which satisfy the condition expressed in the predicate. For example,
cond_dup [3;4;5] (fun x -> x mod 2 = 1) = [3;3;4;5;5].
n_times : ('a -> 'a) * int * 'a -> 'asuch that
v ntimes. For example,
n_times((fun x-> x+1), 50, 0) = 50.
range : int -> int -> int listsuch that
range num1 num2returns an ordered list of all integers from
num2inclusive. For example,
range 2 5 = [2;3;4;5]. Use failwith if
zipwith : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c listsuch that
zipwith f l1 l2generates a list whose ith element is obtained by applying
fto the ith element of
l1and the ith element of
l2. If the lists have different lengths, the extra elements in the longer list should be ignored. For example,
zipwith (+) [1;2;3] [4;5] = [5;7].
let hyp a b = sqrt((a *. a ) +. (b *. b)). The following code uses higher order functions to calculate the same value. Fill in the ??? below so that the whole function correctly calculate the hypotenuse of a right triangle.
let hyp a b = let d x = ??? in let u (x1, x2) f = ??? in let p f x = ??? in p sqrt (u (u (d a) ( *.), u (d b) ( *.)) (+.))
buckets : ('a -> 'a -> bool) -> 'a list -> 'a list listthat partitions a list into equivalence classes. That is,
buckets equiv lstshould return a list of lists where each sublist in the result contains equivalent elements, where two elements are considered equivalent if
equivreturns true. For example:
buckets (=) [1;2;3;4] = [;;;] buckets (=) [1;2;3;4;2;3;4;3;4] = [;[2;2];[3;3;3];[4;4;4]] buckets (fun x y -> (=) (x mod 3) (y mod 3)) [1;2;3;4;5;6] = [[1;4];[2;5];[3;6]]