There was a problem when proofreading this page.
Steele and Sussman
23
The Art of the Interpreter
get a single element.
We could simply add more procedural parameters to MAPCAR:
(DEFINE (MAP F OP ID L)
(COND ((NULL L) ID)
(T (OP (F L)
(MAP F OP ID (CDR L))))))
Using this, we can make a copy of the list L:
(MAP CAR CONS '() L)
We can simulate (MAPCAR F L):
(MAP (LAMBDA (X) (F (CAR X))) CONS '() L)
Indeed, we can write:
(DEFINE (MAPCAR F L)
(MAP (LAMBDA (X) (F (CAR X))) CONS '() L))
We can sum the elements of L:
(MAP CAR + 0 L)
We can take the product of the elements of L:
(MAP CAR * 1 L)
We can count the pairs of duplicate elements of L:
(MAP (LAMBDA (X) X)
(LAMBDA (Y N) (COND ((MEMBER (CAR Y) (CDR Y))
(+ N 1))
(T N)))
0
L)
If we have occasion to take the sum over lots of lists in different places, we might want to package the operation "sum over list" — we get awfully tired of writing "CAR + 0". We can write:
(DEFINE (MAPGEN F OP ID)
(LAMBDA (L) (MAP F OP ID L)))
The result of (MAPGEN CAR + 0) we might call SUM — it is a procedure of one argument which will sum the elements of a list. The reason we wrote a procedure to construct SUM, rather than just writing: