A newcomer to Lisp. I know that
(mapcar #'list '(1 2) '(3 4))
will give
'((1 3) (2 4))
and based on my understanding of how apply works, I expect
(apply #'(lambda (&rest x) (mapcar #'list x)) '((1 2) (3 4)))
to return the same result. Instead, I am getting
'(((1 2)) ((3 4)))
I am confused because
(apply #'append '((1 2) (3 4)))
gives me
'(1 2 3 4)
as expected. What is going on?
Simplify it. Suppose you used
Ainstead of(1 2), andBinstead of(3 4):Because
&rest xtakes all the arguments and packs them up as a list, soxhas the value(A B).Then
mapcariterates twice, passingAtolist, producing(A), then it does the same withB. Thenmapcarmakes a list of those, producing( (A) (B) )It’s useful to put print statements in there to see what’s going on.