Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'cdadr' on nested data list in lisp

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.]

like image 904
skang404 Avatar asked Dec 26 '22 21:12

skang404


1 Answers

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.

like image 94
Joshua Taylor Avatar answered Jan 03 '23 05:01

Joshua Taylor