Key to CSE341 Midterm, Winter 2025 handout #6
1. Expression Value
-------------------------------------------------------------------------------
reduce(uncurry(+.), numbers) 19.0
map(List.hd % List.tl % reverse, [2; 3; 5; 8]
[1--3; 2--4; 5--6; 3--9])
reduce(uncurry( *. ), map(Float.round, numbers)) 210.0
filter((fun x -> 20 / x < 2), (1--12) @ (8--14)) [11; 12; 11; 12; 13; 14]
map((fun x -> x--4), 2--5) [[2; 3; 4]; [3; 4]; [4]; []]
reduce(uncurry( * ), map((fun x -> 3 * x - 1), 80
1--3))
map((fun n -> n -. 1.1), numbers) [6.2; 0.3; 2.1; 3.8; 1.1]
reduce(uncurry(+), filter((fun n -> 6 mod n = 0), 6
1--5))
map((fun x -> (x, x - 1)), 2--4) [(2, 1); (3, 2); (4, 3)]
implode(reduce(uncurry(@), map(List.tl % explode, "ooaraz"
["foo";"bar";"baz"])))
2. Expression Type
-------------------------------------------------------------------------------
lst float list
(lst, map(Float.round, lst)) float list * float list
map((fun x -> (Float.round(x), x)), lst) (float * float) list
Float.round % List.hd % List.tl float list -> float
fun x -> [(x, [x])] 'a -> ('a * 'a list) list
3. The binding is:
val answer = 47
4. One possible solution appears below.
let sum = reduce2 (+) % map2 Char.code % explode
let counts = map2 ((--) 1) % (--) 1
5. One possible solution appears below.
let rec evens(lst) =
match lst with
| [] -> 0
| x::xs -> if x mod 2 = 0 then 1 + evens(xs)
else evens(xs)
6. One possible solution appears below.
let map_index(f, lst) =
let rec helper(lst, n) =
match lst with
| [] -> []
| x::xs -> f(x, n) :: helper(xs, n + 1)
in helper(lst, 0)
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 tighten(tree) =
match tree with
| Empty -> Empty
| Node(root, Empty, Empty) -> Node(root, Empty, Empty)
| Node(root, left, Empty) -> tighten(left)
| Node(root, Empty, right) -> tighten(right)
| Node(root, left, right) -> Node(root, tighten(left), tighten(right))
9. One possible solution appears below.
let index_sum(lst) =
reduce(uncurry(+), 0::map_index((fun (n, i) -> n * i), map(evens, lst)))
let min_sum(lst) = qsort((fun (a, b) -> evens(a) > evens(b)), lst)
10. One possible solution appears below.
let rec pick(n, lst) =
match (n, lst) with
| (0, _) -> [[]]
| (_, []) -> []
| (n, x::xs) -> insert(x, pick(n - 1, xs)) @ pick(n, xs)