Lisp:报价的评估
以下哪个表达式的 Lisp 语法是正确的?
(+ 1 (quote 1))
==> 1 (???)
(+ 1 (eval (quote 1))
==> 2
我目前正在编写自己的 lisp 解释器,不太确定如何正确处理引号。我见过的大多数 Lisp 解释器都将这两个表达式计算为“2”。但是,难道不应该根本不评估引用,从而只有第二个才是合法的表达式吗?那为什么它还能起作用呢?这是某种语法糖吗?
Which of the following expressions has correct lisp syntax?
(+ 1 (quote 1))
==> 1 (???)
(+ 1 (eval (quote 1))
==> 2
I'm currently writing my own lisp interpreter and not quite sure how to handle the quotes correct. Most lisp interpreters I've had a look at evaluate both expressions to "2". But shouldn't the quote be not evaluated at all and thereby only the second one be a legal expression? Why does it work then anyway? Is this some kind of syntactical sugar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非有特殊形式,大多数 Lisp 首先计算参数,然后应用函数(因此出现了 eval-and-apply 短语)。
您的第一个表单
(+ 1 '1)
将首先评估其参数1
和'1
。常量数字计算自身,而引用计算其引用的内容,因此您需要将+
应用于1
和1
,产生<代码>2。第二种形式类似,不带引号的 1 只会执行一次
eval
,再次产生1
:Barring special forms, most Lisps evaluate the arguments first, then apply the function (hence the eval-and-apply phrase).
Your first form
(+ 1 '1)
would first evaluate its arguments1
and'1
. Constant numerics evaluate to themselves, and the quote evaluates to what it quotes, so you'd be left applying+
to1
and1
, yielding2
.The second form is similar, the unquoted 1 will just go through
eval
once, yielding1
again:数字会自行求值,因此
(quote 1)
与1
相同。Numbers evaluate to themselves so
(quote 1)
is the same as1
.