Key to CSE341 Midterm, Winter 2026 handout #7
1. Expression Value
-------------------------------------------------------------------------------
reduce(uncurry(+), 4--7) 22
reduce(uncurry( * ), map(List.hd, lst)) 504
filter((fun x -> x mod 3 = 1), 0--15) [1; 4; 7; 10; 13]
map((fun x -> reduce(uncurry(+), x)), lst) [24; 14; 12; 39]
map((fun x -> float_of_int(x) +. 0.5), 8--11) [8.5; 9.5; 10.5; 11.5]
filter((fun x -> List.length(x) = 3), lst) [[7; 8; 9]; [3; 4; 5];
[12; 13; 14]]
map((fun x -> [x; x + 3]), 3--6) [[3; 6]; [4; 7]; [5; 8];
[6; 9]]
reduce(uncurry(+.), map(float_of_int, 5--8)) 26. (or 26.0)
map(List.length, map((fun x -> 3--x), 3--6)) [1; 2; 3; 4]
reduce(uncurry(+),map(List.hd,map(List.tl, lst))) 28
2. Expression Type
-------------------------------------------------------------------------------
lst (string * int) list
List.hd(List.tl(lst)) string * int
List.tl(List.tl(lst)) (string * int) list
fun x -> (int_of_float(x), x) float -> int * float
fun (x, y) -> (y, x) 'a * 'b -> 'b * 'a
3. The binding is:
val answer = 25
4. One possible solution appears below.
let f1 = ( *. ) 2.8 % float_of_int
let f2 = (mod) 13 % ( * ) 2
let f3 = filter2 ((>) 100)
5. One possible solution appears below.
let rec take(n, lst) =
match (n, lst) with
| (0, _) -> []
| (m, []) -> invalid_arg("list has too few elements")
| (m, x::xs) -> x::take(m - 1, xs)
6. One possible solution appears below.
let rec drop(n, lst) =
if n = 0 then lst
else if n < 0 || lst = [] then invalid_arg("too few elements")
else drop(n - 1, List.tl(lst))
7. One possible solution appears below.
let rec insert(x, lst) =
match lst with
| [] -> []
| y::ys -> (x::y)::insert(x, ys)
8. One possible solution appears below.
let rec sum(lst) =
match lst with
| Empty -> 0
| Node(x, lst) -> x + sum(lst)
let rec insert_sorted(lst, n) =
match lst with
| Empty -> Node(n, Empty)
| Node(x, lst) ->
if n <= x then Node(n, Node(x, lst))
else Node(x, insert_sorted(lst, n))
9. One possible solution appears below.
let rec subsets(lst) =
match lst with
| [] -> [[]]
| a::rest ->
let sub = subsets(rest)
in sub @ insert(a, sub)