|
From: Andrew P. <an...@pi...> - 2003-02-27 13:39:48
|
This blew me away:
> (setf c '((PIG2 BLUE4) (PIG1 RED)))
((PIG2 BLUE4) (PIG1 RED))
> (mapcan #'(lambda (p) (cdr (assoc p c))) '(PIG1 PIG2))
(RED BLUE4)
> c
((PIG2 RED BLUE4) (PIG1 BLUE4))
c has been modified, but not in the way I expected: the cdrs of
'(PIG2 BLUE4) and '(PIG1 RED) have been swapped. Does this mean
that the lambda really returns by reference? And what is the point
of this? It seems to me that whichever way you combine the two
lists, you need three pointers (one for the beginning of the first
list, one for the beginning of the second, and one to traverse the
first), so why pick the way that has unintuitive results?
Moreover, the reference I am using
http://www.lispworks.com/reference/HyperSpec/Body/f_mapc_.htm#mapcan
indicates that (mapcar ...) is equivalent to (apply #'nconc (mapcar
...)), and
> (setf c '((PIG2 BLUE4) (PIG1 RED)))
((PIG2 BLUE4) (PIG1 RED))
> (apply #'nconc (mapcar #'(lambda (p) (cdr (assoc p c))) '(PIG1 PIG2)))
(RED BLUE4)
> c
((PIG2 BLUE4) (PIG1 RED BLUE4))
So is mapcan allowed to do this? Or am I wrong in understanding
that the two must have the same side-effects?
Thanks,
Andrew
PS. Please Cc: me on replies.
|