Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does (list 'quote 'x) evaluate to 'x and not ('x) or (quote 'x)?

I'm trying to learn LISP and was going through a code example where something similar to the following code is used:

(list 'quote 5)

This evaluates to '5 in the REPL. I expected it to evaluate to ('5) or (quote 5)

I'm trying this out in the CLISP REPL.

Any help would be appreciated.

like image 481
levrach Avatar asked Nov 19 '10 16:11

levrach


1 Answers

The read-evaluate-print loop first reads, then evaluates

'quote is read as "the symbol whose name is QUOTE"

5 is read as "the number 5"

So (list 'quote 5) is evaluated as "make a list whose first element is the symbol whose name is QUOTE and whose second element is 5".

The result of this evaluation can be written as "(quote 5)". "'5" is another way of saying this, and the printer in some (probably most) lisp implentations will choose to print the shorter form instead of the longer one.

When you're learning this stuff by typing at the repl you need to remember that the two steps of reading and evaluation are distinct, but that the loop is doing both

Try

* (read-from-string "(list 'quote 5)")
(LIST 'QUOTE 5)

to do one step at a time, or

* (first (read-from-string "(quote 5)"))
QUOTE
* (second (read-from-string "(quote 5)"))
5
* (read-from-string "(quote 5)")
'5

to convince yourself that "(quote 5)" and "'5" are the same thing

like image 120
telent Avatar answered Sep 30 '22 12:09

telent