1. val lst = [(1, 5), (~4, ~4), (2, 8), (~1, 2), (~3, ~5)]; a) hd(tl(lst)) (~4,~4) b) reduce(op *, 1--4) 24 c) map (fn x => #2(x)) lst [5,~4,8,2,~5] d) List.filter (fn(a, b) => a >= b) lst [(~4,~4),(~3,~5)] e) map (abs o Int.max) lst [5,4,8,2,3] f) map (curry op-- 2) (3--5) [[2,3],[2,3,4],[2,3,4,5]] g) reduce(op @, map (fn(a, b) => [a, b]) lst) [1,5,~4,~4,2,8,~1,2,~3,~5] 2. val x = ("Suzy", "Smith", 27); a) x string * string * int b) (2.0, x) real * (string * string * int) c) fn(a, b) => explode(hd(b) ^ a) string * string list -> char list d) fn(x, y) => [x, y] 'a * 'a -> 'a list e) fn(x, y) => ((y, ~y), trunc(x)) real * int -> (int * int) * int 3. answer stores [21,6,3] 4. a) val largest = (List.foldl Int.max 0) o (List.foldl op@ []); val largest = hd o (curry quicksort op>=) o (map (hd o (curry quicksort op >=))); val largest = hd o (curry quicksort op>=) o reduce2 op@; val largest = hd o curry quicksort op>= o (map2 hd o (map2 (curry quicksort op>=))); b) val totalLength = List.foldl (op+) 0 o (map String.size); val totalLength = length o (reduce2 op@) o (map explode); val totalLength = size o String.concat; val totalLength = reduce2 op+ o (map size); val totalLength = size o reduce2 op^; val totalLength = length o explode o (reduce2 op^); val totalLength = reduce2 op+ o map2 (length o explode); 5. functions fun orderPairs([]) = [] | orderPairs((a, b)::rest) = (if a < b then (a, b) else (b, a)) :: orderPairs(rest); (* second solution *) val orderPairs = map (fn(t) => (Int.min t, Int.max t)); 6. functions (* contains(l1, l2) is true iff l1 has l2 inside it *) fun startsWith(_, []) = true | startsWith([], _) = false | startsWith(first1::rest1, l2 as first2::rest2) = first1 = first2 andalso startsWith(rest1, rest2); 7. functions fun mapAll([], L) = L | mapAll(f::rest, L) = mapAll(rest, (map f L)); (* second solution *) fun mapAll(funcs, L) = let fun composeAll([]) = (fn(x) => x) (* identity function *) | composeAll(f::fs) = composeAll(fs) o f in map (composeAll(funcs)) L end; 8. binary tree fun prune(Empty, min, max) = [] | prune(Node(data, left, right), min, max) = if data < min then prune(right) else if data > max then prune(left) else Node(data, prune(left), prune(right)); 9. functions (hard) fun indexOf(_, []) = 0 | indexOf([], _) = ~1 | indexOf(first1::rest1, l2 as first2::rest2) = if first1 = first2 andalso startsWith(rest1, rest2) then 0 else let val i = indexOf(rest1, l2) in if i >= 0 then i+1 else ~1 end; fun indexOf(_, []) = 0 | indexOf(listA, listB) = let fun help(index, [], listB) = ~1 | help(index, listA, listB) = if startsWith(listA, listB) then index else help(index+1, tl(listA), listB) in help(0, listA, listB) end; fun indexOf(a, b) = let fun search([],i) = if b = [] then 0 else ~1 | search(x::xs, i) = if startsWith(x::xs, b) then i else search(xs, i+1) in search(a,0) end;