Common Lisp 使用两个反引号和逗号
我正在学习 common lisp,我在理解两个反引号和两个逗号的用法时遇到了问题:
``(a ,,(+ 1 2))
我的意思是,我不知道为什么它被评估为:
`(A ,3)
而不是类似的东西:
`(A 3)
我正在解释自己两个逗号都被“消耗”,以便评估表单前面的两个反引号,因此没有一个逗号应该离开,但仍然有一个。 会是什么样子?
``(a ,,(+ 1 2))
仅使用列表和 '
I'm learning common lisp and I have a problem with understanding the usage of two backquotes combined with two commas:
``(a ,,(+ 1 2))
I mean, I don't have a clue why it's evaluated to:
`(A ,3)
rather than something like that:
`(A 3)
I'm explaining myself that both commas were 'consumed' in order to evaluate two backquotes in front of the form so none of the commas should've left and yet there's still one.
How would look
``(a ,,(+ 1 2))
using only list and ' ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从规范来看,
Common Lisp HyperSpec 关于嵌套 反引号 是这么说的:
R5RS 方案规范还包括有关 反引号:
另请记住,每次评估只会折叠一个反引号,就像常规引用一样,它不是递归的。
实际应用的规则
要了解这三个细节如何相互作用,让我们稍微扩展一下您的示例。这个表达式...
计算结果如下(以 SBCL 表示法):
(+ 1 2)
被匹配的逗号(第 2nd< /sup> 逗号,根据 HyperSpec)。(+ 3 4)
没有足够的逗号来扩展(这就是 R5RS 提到的)。扩展两个逗号
要摆脱另一个反引号,需要进行另一级评估:
两个反引号都消失了,我们留下了一个简单的列表:
From the specs
This is what the Common Lisp HyperSpec says about nested backticks:
The R5RS Scheme spec also includes these details about backticks:
Also keep in mind that only one backtick gets collapsed per evaluation, just like a regular quote, it's not recursive.
Rules in action
To see how these three details interact, let's expand your example a bit. This expression...
Gets evaluated to this (in SBCL notation):
(+ 1 2)
got escaped by the matching comma (the 2nd comma, according to the HyperSpec).(+ 3 4)
didn't have enough commas to get expanded (which is what R5RS mentions).Expanding both commas
To get rid of the other backtick, another level of evaluation is needed:
Both backticks are gone, and we're left with a plain list:
不,两个逗号都被消耗掉了。有两层引用和两层逗号。现在有一层引用和一层逗号。事实上,GNU Common Lisp (2.44.1) 将你的表达式求值为
That 与这完全相同
,但更明确地“求值”了两个逗号。
No, both commas were consumed. There were two levels of quoting and two levels of commas. Now there's one level of quoting and one level of commas. In fact, GNU Common Lisp (2.44.1) evaluates your expression as
That's exactly the same thing as
but more explicitly has "evaluated" both commas.