Is append the identity function for map?

Viewed 67

In doing some tests I've noticed that append always gives me the same output as input when using map:

#lang sicp
(map append '(1 2 3 4 5))
(map (lambda (x) x) '(1 2 3 4 5))
; (1 2 3 4 5)
; (1 2 3 4 5)
(map append '((1 2)(3 4)))
(map (lambda (x) x) '((1 2)(3 4)))
; ((1 2) (3 4))
; ((1 2) (3 4))

That seems pretty neat/unexpected to me. Is this in fact the case? If so, how does the append work to give the identity property?

1 Answers

The append procedure takes zero or more list arguments, and a final argument that can be any object. When the final argument is a list, the result of appending is a proper list. When the final argument is not a list, but other list arguments have been provided, the result is an improper list. When only one argument is provided, it is just returned. This behavior with one argument is exactly the behavior of an identity procedure.

> (append '(1 2) '(3))
(1 2 3)
> (append '(1 2) 3)
(1 2 . 3)
> (append '(1 2))
(1 2)
> (append 3)
3

The call (map append '(1 2 3 4 5)) is equivalent to:

> (list (append 1)
        (append 2)
        (append 3)
        (append 4)
        (append 5))
(1 2 3 4 5)

Here, append is just acting as an identity procedure, as described above.

Related