问题
While studying cons
, cdr
and car
to handle lists I tried following :
(cadr '('(1) '(2)))
'(2)
which gives the second item in the list as expected. Whereas following gives :
(cdadr '('(1) '(2)))
((2))
How is data being concerted to code and still not giving error?
How was this evaluated?
cdr on '(2) should give nil, which is does. Why not above?
[I am new to both clisp and stackoverflow so pardon me.]
回答1:
quote is a special operator which returns its single unevaluated argument. The form (quote ...)
can be abbreviated using ' as '...
. Since the '
is handled by the reader, the form
'('(1) '(2)))
is actually read the same as
(quote ((quote (1)) (quote (2)))
The outermost application quote
to the the argument((quote (1)) (quote (2)))
returns that argument. The cadr
of that argument is the list
(quote (2))
whose first element is the symbol quote
, and whose second element is a list of a single element 2
.
回答2:
Because of quotes. You should write (cadr '((1) (2)))
.
With your list, (caadr '('(1) '(2)))
yields QUOTE
.
Actually, your list '('(1) '(2))
is really '((quote (1)) (quote (2)))
, which may show better why you get this result.
来源:https://stackoverflow.com/questions/18697105/cdadr-on-nested-data-list-in-lisp