为什么(列表'quote'x)求值为'x,而不是('x)或(quote'x)? [英] Why does (list 'quote 'x) evaluate to 'x and not ('x) or (quote 'x)?

查看:85
本文介绍了为什么(列表'quote'x)求值为'x,而不是('x)或(quote'x)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习LISP,并且正在通过一个代码示例,其中使用了类似于以下代码的代码:

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

(列出'quote 5')

(list 'quote 5)

这在REPL中计算为'5.我希望它的计算结果为('5)或(引用5)

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

我正在CLISP REPL中进行尝试.

I'm trying this out in the CLISP REPL.

任何帮助将不胜感激.

推荐答案

read-evaluate-print循环首先读取,然后求值

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

'quote读为名称为QUOTE的符号"

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

5读为数字5"

因此(列表'引用5)被评估为创建一个列表,其第一个元素是名称为QUOTE且第二个元素为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".

该评估的结果可以写为(引用5)". '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.

当您通过键入repl来学习这些内容时,您需要记住 阅读和评估的两个步骤是截然不同的,但是循环在同时进行了

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

尝试

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

说服自己(引号5)"和'5"是同一回事

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

这篇关于为什么(列表'quote'x)求值为'x,而不是('x)或(quote'x)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆