CS471 Program Language Structure I
Fall 2000
Using the list-processing primitives car, cdr, cons and null? (Scheme) or hd, tl, :: and = (ML), write the following functions:
1. filter, that takes a function that has one parameter and a simple list of values as its two arguments, and returns a list consisting only of those elements that return true when the passed function is applied to them. e.g. if there exists a function odd that return true if its argument is an odd number, then:
(filter
odd (list 1 2 3 4 5)) or filter odd [1,2,3,4,5]
returns the list (1 3 5) or
[1,3,5].
2. accumulate, that takes a function that has two
parameters , an initial value and a simple list of values as its three
arguments, and returns a single value that is the result of applying the function
to the elements of the list, accumulating the value as it goes through the
list. e.g.
(accumulate
+ 0 (list 1 2 3 4 5)) or
accumulate
+ 0 [1,2,3,4,5]
returns the value 15.
·
Points will not be taken off for bad syntax, but the recursive
strategy must be clear from the answer you give. It would help if you explain
the strategy, rather than simply giving the answer.
December 6th. in class.