(* Ex 3.11.7, allEven (in class) *) fun allEven([]) = true (* | allEven(a::rest) = if a mod 2 = 0 then allEven(rest) else false; *) | allEven(a::rest) = (a mod 2 = 0) andalso allEven(rest); (* Ex 3.11.8, hasEven (in class) *) fun hasEven([]) = false (* | hasEven(a::rest) = if a mod 2 = 0 then true else hasEven(rest); *) | hasEven(a::rest) = (a mod 2 = 0) orelse hasEven(rest); (* Ex 3.11.9, notAllEven (in class) *) fun notAllEven([]) = false | notAllEven(a::rest) = if a mod 2 = 1 then true else notAllEven(rest); (* Ex 3.11.10, noneEven (in class) *) fun noneEven([]) = true | noneEven(a::rest) = if a mod 2 = 0 then false else noneEven(rest); (* From Section 3.13 *) (* Wrong version! *) fun allHaveDivisor([], yy) = true | allHaveDivisor(xx, []) = false | allHaveDivisor(x::xRest, y::yRest) = if x mod y = 0 then allHaveDivisor(xRest, y::yRest) else allHaveDivisor(x::xRest, yRest); (* Correct version *) fun allHaveDivisor([], yy) = true | allHaveDivisor(x::xRest, yy) = let fun hasDivisor([]) = false | hasDivisor(y::yRest) = if x mod y = 0 then true else hasDivisor(yRest); in hasDivisor(yy) andalso allHaveDivisor(xRest, yy) end; (* Ex 3.13.1, allLessThan (for all, for all) (in class) *) fun allLessThan([], yy) = true | allLessThan(x::xRest, yy) = (* lessThanAll asks the question "is x less than all things in this list?" *) let fun lessThanAll([]) = true | lessThanAll(y::yRest) = if x < y then lessThanAll(yRest) else false; in lessThanAll(x) andalso allLessThan(xRest, yy) end; (* Ex 3.13.3, allHaveAddInv (for all, there exists) (in class) *) fun allHaveAddInv(original) = let fun hasAddInv(x, []) = false | hasAddInv(x, y::yRest) = x + y = 0 orelse hasAddInv(x, yRest); fun allHaveAddInvHelper([]) = true | allHaveAddInvHelper(x::xRest) = hasAddInv(x, original) andalso allHaveAddInvHelper(xRest) in allHaveAddInvHelper(original) end; (* Ex 3.13.4 (there exists, for all) (HW) *) fun hasDivisorOfAll(original) = let fun isDivisorOfAll(x, []) = ?? | isDivisorOfAll(x, y::yRest) = ?? fun hasDivisorOfAllHelper([]) = ?? | hasDivisorOfAllHelper(x::xRest) = isDivisorOfAll(x,original) orelse hasDivisorOfAllHelper(xRest) in ?? end; (* Ex 3.13.5 (there exists, there exists) (HW) *) fun hasCommonElement([], bb) = ?? | hasCommonElement(a::aRest, bb) = let fun containsA([]) = ?? | containsA(b::bRest) = ?? in ?? end;