Key to CSE341 Section #3 Problems 1. One possible solution to each appears below. fun isPrime(n) = let fun isFactor(m) = n mod m = 0 val factors = filter(isFactor, 2--trunc(Math.sqrt(real(n)))) in n > 1 andalso factors = [] end; fun isPrime(n) = n > 1 andalso filter(fn(x) => n mod x = 0, 2--trunc(Math.sqrt(real(n))) = []; 2. One possible solution appears below. (* sum of squares of values from a list *) fun sumOfSquares(lst) = reduce(op +, map(fn x => x * x, lst)); 3. One possible solution appears below. (* sum of squares of 1 through n *) fun sumOfSquares2(n) = reduce(op +, map(fn x => x * x, 1--n)); 4. One possible solution appears below. (* product of first n odd numbers *) fun oddProduct(n) = reduce(op *, map(fn x => 2 * x + 1, 1--n)); 5. One possible solution appears below. (* length of a list *) fun len(lst) = reduce(op +, 0::map(fn x => 1, lst)); 6. One possible solution for each appears below. a) val double = curry op* 2; b) val prependStar = curry op ^ "*"; c) val oneTo = curry op-- 1; d) val addOne = map2 (curry op+ 1) e) val f = abs o curry op+ 10 o curry op* 2; f) val primeProduct = reduce2 op* o filter2 isPrime; 7. a) fun f c:char = 1.0 is interpreted as fun (f c):char = 1.0 should be fun f (c:char) = 1.0 b) fun f x::xs = [] is interpreted as fun (f x)::xs = [] should be fun f (x::xs) = [] c) print Int.toString 123 is interpreted as (print Int.toString) 123 should be print (Int.toString 123) d) val add2 = map2 curry op+ 2 is interpreted as val add2 = (map2 curry) op+ 2 should be val add2 = map2 (curry op+ 2)