'cdadr' on nested data list in lisp

帅比萌擦擦* 提交于 2019-12-10 17:09:14

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!