I'm giving a few different problems for the ML material covered
on Nov 17, approximately corresponding to Section 7.5.
Enter these in the automated grading system as "7.5.a"
etc.
7.5.a
Use map to write a function sumPairs
that takes a list of pairs and returns a list of the sum of those pairs.
For example, sumPairs([(1,2), (3,4), (5,6)])
would return [3,7,11].
(This is like Exercise 2.2.14, which we did in class on Sept 6, but using
map.)
7.5.b
Use filter to write a function keepLessThan
that takes an int and a list of ints and
returns a list like the one given but including only those ints
that are less than the given one.
For example, keepLessThan(5, [3,7,2,9,8,1])
would return [3,2,1].
7.5.c
Use map and filter to write a function
halveEvens that takes a list of ints
and returns a list containing only the halves of the even elements
(so, odd elements are removed completely).
For example, halveEvens([8,14,17,6,9,11,4]) would
return [4,7,3, 2].
(This is like a problem from Test 1, but using map
and filter.)
For convenience, here are map and filter:
fun map(f, []) = [] | map(f, a::rest) = f(a)::map(f, rest); fun filter(f, []) = [] | filter(f, a::rest) = if f(a) then a::filter(f, rest) else filter(f, rest);
Also, you can find all the code from class here.