(* examples from the text *) fun repeatEach([]) = [] | repeatEach(a::rest) = a::a::repeatEach(rest); fun sum([]) = 0 | sum(a::rest) = a + sum(rest); fun switchPair([]) = [] | switchPair((a,b)::rest) = (b, a)::switchPair(rest); (* Wrong! *) fun sumProduct([]) = (0, 1) | sumProduct(x::rest) = (* (x + #1(sumProduct(rest)), x * #2(sumProduct(rest))); *) let val (a, b) = sumProduct(rest) in (x +a, x * b) end; (* 2.2.4 -- in class*) fun count([]) = 0 | count(a::rest) = 1 + count(rest); (* 2.2.5 -- in class *) fun findIth([], i) = ~1 | findIth(a::rest, 0) = a | findIth(a::rest, i) = findIth(rest, i - 1); (* 2.2.6 -- in class *) fun reverse([]) = [] | reverse(a::rest) = reverse(rest)@ [a] (* Ex 2.2.10 -- in class *) fun listify([]) = [] | listify(a::rest) = [a] :: listify(rest); (* Ex 2.2.12 -- in class *) fun splitList([]) = ([], []) | splitList((a,b)::rest) = let val (aa, bb) = splitList(rest) in (a::aa, b::bb) end; (* Ex 2.2.13 -- homework *) fun tuplify([], []) = [] | tuplify(a::rest, []) = ?? | tuplify([], b::rest) = ?? | tuplify(a::aRest, b::bRest) = ?? (* Ex 2.2.14 -- in class *) fun sumPairs([]) = [] | sumPairs((a,b)::rest) = (a + b)::sumPairs(rest); (* Ex 2.2.15 -- homework *) fun sumPairList([]) = (0, 0) | sumPairList((a,b)::rest) = let val (c, d) = sumPairList(rest) in ?? end; (* For the homework assigned next time (for Jan 27) *) (* Ex 2.4.14 -- homework *) (* The second parameter is a list of lists *) fun addToEach(x, []) = ?? | addToEach(x, aa::rest) = ?? (* aa is itself a list ! *) (* Ex 2.4.15 -- homework *) fun powerset([]) = ?? (* Think carefully: What is the powerset of the empty set? *) | powerset(a::rest) = ??