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

只谈情不闲聊 提交于 2019-12-19 07:47:33

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/4226948/why-does-list-quote-x-evaluate-to-x-and-not-x-or-quote-x

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