(* Wrong! *) fun sumProduct([]) = (0, 1) | sumProduct(x::rest) = (x + sumProduct(rest), x * sumProduct(rest)); fun sumProduct([]) = (0, 1) | sumProduct(x::rest) = (x + #1(sumProduct(rest)), x * #2(sumProduct(rest))); fun sumProduct([]) = (0, 1) | sumProduct(x::rest) = let val (s, p) = sumProduct(rest) in (x + s, x * p) end; (* Ex 2.2.12 -- in class *) fun splitList([]) = ([], []) | splitList((a,b)::rest) = let val (cc,dd) = splitList(rest); in (a::cc, b::dd) end; (* Ex 2.2.14 -- in class *) fun sumPairs([]) = [] | sumPairs((a,b)::rest) = (a + b)::sumPairs(rest); (* For HW, about powersets *) (* 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) = ?? (* Background: the case expression *) fun factorial(0) = 1 | factorial(n) = n * factorial(n-1); fun factorial(n) = case n of 0 => 1 | x => x * factorial(x-1); (* Background: the "option" family of types *) fun homemadeHd(x::rest) = SOME x | homemadeHd([]) = NONE; fun findIth([], i) = NONE | findIth(a::rest, 0) = SOME a | findIth(a::rest, i) = findIth(rest, i - 1); datatype tree = Oak | Elm | Maple | Spruce | Fir | Pine | Willow; datatype vegetable = Carrot | Zucchini | Tomato | Cucumber | Lettuce; datatype grain = Wheat | Oat | Barley | Maize; datatype plot = Grove of tree | Garden of vegetable | Field of grain | Vacant; fun getTreeList([]) = [] | getTreeList(a::rest) = case a of Grove(t) => SOME t :: getTreeList(rest) | x => NONE::getTreeList(rest); getTreeList([Field(Oat), Grove(Elm), Grove(Fir), Vacant, Garden(Carrot), Grove(Oak)]);