为什么 (list 'quote 'x) 计算结果为 'x 而不是 ('x) 或 (quote 'x)?
我正在尝试学习 LISP,并且正在查看一个代码示例,其中使用了类似于以下代码的内容:
(list 'quote 5)
这在 REPL 中计算为 '5。我预计它的计算结果为 ('5) 或 (quote 5)
我正在 CLISP REPL 中尝试这一点。
任何帮助将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
read-evaluate-print 循环首先读取,然后评估
'quote 被读取为“名称为 QUOTE 的符号”
5 被读取为“数字 5”
因此 (list 'quote 5) 被评估为“创建一个列表,其第一个element 是名称为 QUOTE、第二个元素为 5" 的符号。
该评估的结果可以写为“(quote 5)”。 “'5”是另一种表达方式,某些(可能是大多数)lisp 实现中的打印机将选择打印较短的形式而不是较长的形式。
当你通过在 repl 中输入来学习这些东西时,你需要记住这一点
阅读和评估的两个步骤是不同的,但是循环同时执行
尝试
一次执行一步,或者
说服自己“(quote 5)”和“'5”是同一件事
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
to do one step at a time, or
to convince yourself that "(quote 5)" and "'5" are the same thing