Common Lisp中的逗号逗号 [英] comma-comma-at in Common Lisp

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

问题描述

我对逗号-逗号的用法感到困惑.

`,@abc

可能会说:我不知道在反引号之前进行拼接的意思是什么.永远不要再这样做."

但是以某种方式,口译员不会抱怨,而只是选择这样做

`(beg <splice stuff here and write comma in front of each of them> end)

是否与 CLHS反引号中的规则兼容?


有关读者的信息,答案后的注释指的是答案的旧版本,并导致答案的更新.新读者无需阅读评论.


另请参见应扩展CHLS的"innermost backquoted形式第一"含义

解决方案

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

因此,第一个逗号取消了第二个反引号的引用;然后,@会拼接到parms的内容中,因为所有引号都已撤消.取消引用不会按照您所描述的步骤逐步替换"表单-在决定如何评估所包含的表单时,逗号只会备份在带引号的表单结构中.

(let ((parms '(x y)))
  `(list 'beg ,@parms 'end))

显然是(list 'beg x y 'end),这就是您要追求的.生成可创建所需结构作为其输出的形式.额外的外部反引号只是该语法的简写方式,与'(foo bar)是(c4>的简写方式相同).

I am confused about how comma-comma-at does what it does.

An example of use of comma-comma-at is in Is there a use for double unquote (double comma) when defining a Lisp macro?

It seems to me that

(let ((parms '(x y)))
  ``(beg ,,@parms end))

expands to or something equivalent to

`(beg ,x ,y end)

How does it expands to that?

I was thinking, if I evaluate the double backquote form, it causes the second comma to do its work, and the result is:

`(beg ,<splice stuff here> end)

and it looks like Lisp interpreter should complain and say "I don't know what it means to splice stuff before a comma" in the sense that when Lisp encounters

`,@abc

it would be likely to say "I don't know what it means to splice stuff before a backquote. Don't ever do that again."

But somehow, the interpreter does not complain and simply choose to do

`(beg <splice stuff here and write comma in front of each of them> end)

Is that compatible with rules in CLHS backquote?


For readers information, comments following the answer refer to an old version of the answer, and lead to update of the answer. New readers don't need to read the comments.


Also see CHLS "innermost backquoted form should be expanded first" meaning

解决方案

CLHS states: "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."

So, the first comma unquotes the second backquote; the ,@ then splices in the contents of parms, as all quotes have been undone. Unquoting does not "replace" forms in steps as you describe - the commas just back up in the structure of backquoted forms when deciding how to evaluate the contained forms.

(let ((parms '(x y)))
  `(list 'beg ,@parms 'end))

obviously evaluates to (list 'beg x y 'end), which is what you are after here; producing a form that creates the desired structure as its output. The additional outer backquote is simply a shorthand for this syntax, in the same manner as '(foo bar) is (equality issues aside) a shorthand for (list 'foo 'bar).

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

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