特殊形式和宏之间有哪些实际区别?
特殊形式和宏之间有任何实际差异吗?它们有何不同?
Are there any practical differences between special forms and macros? In what do they differ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
特殊形式和宏之间有任何实际差异吗?它们有何不同?
Are there any practical differences between special forms and macros? In what do they differ?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
这些术语并不完全同义词,但它们也不是排他性的(此答案假设为Scheme):
eval
所有子表达式,然后应用
第一个子表达式的结果到其他子表达式的结果列表.)因此,您可以说“特殊形式”是一个与接口或语义相关的术语,而“宏”是一个与实现相关的术语>。 “特殊形式”意味着“这些表达式使用特殊规则进行评估”,而“宏”意味着“这是用于评估某些表达式的特殊规则的实现”。
现在一件重要的事情是,大多数Scheme 特殊形式可以被定义为来自一个非常小的基元核心的宏:
lambda
、if
和宏。仅提供这些的最小方案实现仍然可以将其余部分实现为宏;最近的计划报告通过引用诸如可以用宏定义的“库语法”之类的特殊形式来进行区分。然而,在实践中,实用的Scheme系统通常实现一组更丰富的形式作为原语。从语义上讲,表达式唯一重要的是使用什么规则来评估它,而不是如何实现该规则。因此从这个意义上说,特殊形式是作为宏还是基元实现并不重要。但另一方面,Scheme 系统的实现细节经常“泄露”,所以你可能会发现自己很关心它......
The terms aren't quite synonymous, but they aren't exclusive either (this answer assumes Scheme):
eval
all of the subexpressions, and thenapply
the result of the first one to the list of the results of the others.)So you could say that "special form" is a term that pertains to interface or semantics, whereas "macro" is a term that pertains to implementation. "Special form" means "these expressions are evaluated with a special rule," while "macro" means "here's an implementation of a special rule for evaluating some expressions."
Now one important thing is that most Scheme special forms can be defined as macros from a really small core of primitives:
lambda
,if
and macros. A minimal Scheme implementation that provides only these can still implement the rest as macros; recent Scheme Reports have made that distinction by referring to such special forms as "library syntax" that can be defined in terms of macros. In practice, however, practical Scheme systems often implement a richer set of forms as primitives.Semantically speaking, the only thing that matters about an expression is what rule is used to evaluate it, not how that rule is implemented. So in that sense, it's not important whether a special form is implemented as a macro or a primitive. But on the other hand, the implementation details of a Scheme system often "leak," so you may find yourself caring about it...
Lisp 具有某些语言原语,它们构成了 Lisp 形式:
(sin 2.1)< /code> 或类似
((lambda (ab) (+ ab 2)) 3 4)
因此,特殊形式和宏之间最重要的实际区别是:特殊运算符是内置的语法和语义。它们不能由开发人员编写。宏可以由开发人员编写。
Lisp has certain language primitives, which make up Lisp forms:
(sin 2.1)
or like((lambda (a b) (+ a b 2)) 3 4)
So the most important practical difference between special forms and macros is this: special operators are built-in syntax and semantics. They can't be written by the developer. Macros can be written by the developer.
与特殊形式相反,宏形式可以进行宏扩展:
In contrast to special forms, macro forms can be macroexpanded:
对我来说,最实际的区别在于调试器:宏不会显示在调试器中;宏不会显示在调试器中。相反,宏扩展中的(通常)模糊代码会显示在调试器中。调试这样的代码确实很痛苦,但在开始依赖宏之前,有充分的理由确保它们坚如磐石。
For me the most practical difference has been in the debugger: Macros don't show up in the debugger; instead, the (typically) obscure code from the macro's expansion shows up in the debugger. It is a real pain to debug such code and a good reason to ensure your macros are rock solid before you start relying upon them.
懒人的超级简短答案
您可以随时编写自己的宏,尽管您无法添加特殊形式而不重新编译clojure。
the super short answer for the lazy
You can write your own macroes any time you want, though you can't add special forms without recompiling clojure.