(* From Section 1.12 *) (* Examples with numbers *) fun factorial(0) = 1 | factorial(n) = n * factorial(n-1); (* Ex 1.12.4. Homemade multiply *) fun multiply(x, 0) = 0 | multiply(x, y) = x + multiply(x, y-1); (* Ex 1.12.6. Summation. *) fun sum(1) = 1 | sum(n) = sum(n-1) + n; (* Examples with strings *) fun capitalizeAll("") = "" | capitalizeAll(s) = str(Char.toUpper(String.sub(s, 0))) ^ capitalizeAll(substring(s, 1, size(s) - 1)); (* 1.12.2. Reverse a string. *) fun reverse("") = "" | reverse(s) = reverse(substring(s, 1, size(s) -1)) ^ str(String.sub(s, 0)); (* list 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);