Scheme provides a number of abstract functions for processing lists.
Figure
collects the specification of the
most important ones. Using these functions greatly simplifies many
programming tasks and helps readers understand programs quickly. The
following exercises provide an opportunity to get acquainted with these
functions.
Exercise 21.2.1
Example:
(equal? (diagonal 3)
(list
(list 1 0 0)
(list 0 1 0)
(list 0 0 1)))
Exercise 21.2.2
Use map to define the following functions:
Exercise 21.2.3
Here is the version of filter that DrScheme provides:
;; filter : (X -> boolean) (listof X) -> (listof X)
;; to construct a list of X from all those items on alon
;; for which predicate? holds
(define (filter predicate? alon)
(cond
[(empty? alon) empty]
[else (cond
[(predicate? (first alon))
(cons (first alon) (filter predicate? (rest alon)))]
[else (filter predicate? (rest alon))])]))
Use filter to define the following functions: