为什么 (list 'quote 'x) 计算结果为 'x 而不是 ('x) 或 (quote 'x)?

发布于 2024-10-03 13:28:12 字数 176 浏览 0 评论 0原文

我正在尝试学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆沫 2024-10-10 13:28:12

read-evaluate-print 循环首先读取,然后评估

'quote 被读取为“名称为 QUOTE 的符号”

5 被读取为“数字 5”

因此 (list 'quote 5) 被评估为“创建一个列表,其第一个element 是名称为 QUOTE、第二个元素为 5" 的符号。

该评估的结果可以写为“(quote 5)”。 “'5”是另一种表达方式,某些(可能是大多数)lisp 实现中的打印机将选择打印较短的形式而不是较长的形式。

当你通过在 repl 中输入来学习这些东西时,你需要记住这一点
阅读和评估的两个步骤是不同的,但是循环同时执行

尝试

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

一次执行一步,或者

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

说服自己“(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

* (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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文