使用两个反引号和逗号Common Lisp [英] Using two backquotes and commas, Common Lisp

查看:157
本文介绍了使用两个反引号和逗号Common Lisp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习通用Lisp,但在理解两个反引号和两个逗号结合使用时遇到了问题:

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)

而不是类似的东西:

`(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))

仅使用列表和'?

推荐答案

根据规格

这是Common Lisp HyperSpec关于嵌套反引号的说法:

如果嵌套了反引号语法,则应首先扩展最里面的反引号形式.这意味着,如果连续出现多个逗号,则最左边的逗号属于最里面的反引号.

If the backquote syntax is nested, the innermost backquoted form should be expanded first. This means that if several commas occur in a row, the leftmost one belongs to the innermost backquote.

R5RS计划规范还包括有关反引号:

The R5RS Scheme spec also includes these details about backticks:

准引用形式可以嵌套.仅对与最外层反引号出现在相同嵌套级别的未引号的组件进行替换.嵌套级别在每个连续的准引用内增加一,而在每个未引用内减少一.

Quasiquote forms may be nested. Substitutions are made only for unquoted components appearing at the same nesting level as the outermost backquote. The nesting level increases by one inside each successive quasiquotation, and decreases by one inside each unquotation.

还请记住,每次评估仅折叠一个反引号,就像常规引号一样,它不是递归的.

Also keep in mind that only one backtick gets collapsed per evaluation, just like a regular quote, it's not recursive.

要查看这三个细节如何相互作用,让我们扩展一下示例.这个表情...

To see how these three details interact, let's expand your example a bit. This expression...

``(a ,,(+ 1 2) ,(+ 3 4))

对此进行评估(以SBCL表示法):

Gets evaluated to this (in SBCL notation):

`(A ,3 ,(+ 3 4))

  1. 左反引号塌陷,因此(+ 1 2)被匹配的逗号(根据HyperSpec,第二个 nd 逗号)转义了.
  2. 另一方面,(+ 3 4)没有足够的逗号以进行扩展(这是R5RS提到的).
  3. 只有一个反引号折叠了,因为反引号没有递归扩展.
  1. The left backtick got collapsed, so it the (+ 1 2) got escaped by the matching comma (the 2nd comma, according to the HyperSpec).
  2. On the other hand, the (+ 3 4) didn't have enough commas to get expanded (which is what R5RS mentions).
  3. Only one backtick got collapsed, because backticks don't get recursively expanded.

扩展两个逗号

要摆脱另一个难题,就需要另一个评估级别:

Expanding both commas

To get rid of the other backtick, another level of evaluation is needed:

(eval ``(a ,,(+ 1 2) ,(+ 3 4)))

两个引号都不见了,我们只剩下一个简单的清单:

Both backticks are gone, and we're left with a plain list:

(A 3 7)

这篇关于使用两个反引号和逗号Common Lisp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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