'cdadr' in the list of nested data in lisp

Studying cons , cdr and car for list processing, I tried the following:

 (cadr '('(1) '(2))) '(2) 

which gives the second item in the list, as expected. Considering the following:

 (cdadr '('(1) '(2))) ((2)) 

How are the data consistent with the code and still give no errors?

How was this evaluated?

cdr on '(2) should give nil what is. Why not higher?

[I am new to clisp and stackoverflow, so forgive me.]

+4
source share
2 answers

quote is a special operator that returns its only unvalued argument. The form (quote ...) can be shortened using ' how '... Because the ' handled by the reader, the form

 '('(1) '(2))) 

actually read the same way

 (quote ((quote (1)) (quote (2))) 

The most external quote application for the argument ((quote (1)) (quote (2))) returns this argument. cadr this argument is a list

 (quote (2)) 

whose first element is the quote symbol, and the second element is a list of one element 2 .

+8
source

Because of the quotation marks. You should write (cadr '((1) (2))) .

In your list (caadr '('(1) '(2))) QUOTE is displayed.

Actually, your list of '('(1) '(2)) really '((quote (1)) (quote (2))) , which may show better why you get this result.

+2
source

Source: https://habr.com/ru/post/1501286/


All Articles