你怎么能改写“开始"?在计划? [英] How can you rewrite "begin" in Scheme?
问题描述
正如维基百科文章所述,begin
在 Scheme 中是一种库形式,可以使用更基本的形式(如 lambda
)重写.
As the Wikipedia article explains, begin
in Scheme is a library form that can be rewritten using more fundamental forms like lambda
.
但是你如何重写begin
,特别是考虑到以下几点?
But how do you rewrite a begin
, especially considering the following?
x
===> error: undefined identifier: x
(begin (define x 28) x)
===> 28
x
===> 28
推荐答案
你不能.问题是begin
有两个作用:一个是对一堆有副作用的表达式进行排序,另一个是用于拼接"宏结果.您可以使用带有上述定义的 begin
的事实是第二个特性的结果,您不能自己编写.
You cannot. The thing is that begin
has two roles: one is sequence a bunch of side-effectful expressions, and the other is that it is used to "splice" macro results. The fact that you can use begin
with a definition as in the above is a result of this second feature, and you cannot write it yourself.
如果你真的想了解整个故事,那么你可以将 begin
定义为一个简单的宏,这使得它只执行排序方面(而且它确实可以如此实施,尽管通常不是).但是,您需要在拼接定义(顶级或内部)中添加对 begin
的显式识别.这意味着宏实现很好,但它不能真正成为库,因为核心扩展器应该知道它.(并且因为语言是词法范围的,核心扩展器没有好的方法来识别核心语言中未定义的 begin
.)
If you really want to follow the whole story, then you could define begin
as the simple macro which makes it do only the sequencing aspect (and it can indeed be implemented as such, though usually it isn't). But, you need to add explicit recognition of begin
s to splice definitions (toplevel or internal). This means that a macro implementation is fine, but it cannot really be a library because the core expander should know about it. (And because the language is lexically scoped, there is no good way for the core expander to identify begin
s that are not defined in the core language.)
总结所有这些,你可以说 R5RS 只是将 begin
分类为库语法"是错误的,因为它不能在库中定义......但即使如此也不是完全准确,因为 R5RS 将库语法"定义为派生表达式".因此,真正错误的一点是,begin
的两个面之一在其他地方实现,在扩展器中(用于定义上下文).
To summarize all of this, you could say that R5RS is only wrong in classifying begin
as "library syntax", since it can't be defined in a library... but even that's not entirely accurate since R5RS defines "library syntax" as just "derived expressions". The real wrong point is, therefore, the fact that one of begin
s two faces is implemented elsewhere, in the expander (for definition contexts).
另请注意,R6RS 澄清了整个问题:begin
的两个面是明确的,现在它是核心语言的一部分,不是库形式",甚至不是派生的形式.
Note also that R6RS clarifies the whole deal: the two faces of begin
are made explicit, and it is now part of the core language, not a "library form", and not even a derived form.
这篇关于你怎么能改写“开始"?在计划?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!