特殊形式和宏之间有哪些实际区别?

发布于 2025-01-07 16:06:41 字数 31 浏览 0 评论 0原文

特殊形式和宏之间有任何实际差异吗?它们有何不同?

Are there any practical differences between special forms and macros? In what do they differ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

老旧海报 2025-01-14 16:06:41

这些术语并不完全同义词,但它们也不是排他性的(此答案假设为Scheme):

  • 特殊形式(在Scheme报告中也称为语法)是一个不根据函数应用程序的默认规则进行计算的表达式。 (为了明确起见,默认规则是eval所有子表达式,然后应用第一个子表达式的结果到其他子表达式的结果列表.)
  • 宏系统是一种语言功能,允许在语言本身内定义新的特殊形式。 是使用宏系统定义的特殊形式。

因此,您可以说“特殊形式”是一个与接口语义相关的术语,而“宏”是一个与实现相关的术语>。 “特殊形式”意味着“这些表达式使用特殊规则进行评估”,而“宏”意味着“这是用于评估某些表达式的特殊规则的实现”。

现在一件重要的事情是,大多数Scheme 特殊形式可以被定义为来自一个非常小的基元核心的宏:lambdaif 和宏。仅提供这些的最小方案实现仍然可以将其余部分实现为宏;最近的计划报告通过引用诸如可以用宏定义的“库语法”之类的特殊形式来进行区分。然而,在实践中,实用的Scheme系统通常实现一组更丰富的形式作为原语。

从语义上讲,表达式唯一重要的是使用什么规则来评估它,而不是如何实现该规则。因此从这个意义上说,特殊形式是作为宏还是基元实现并不重要。但另一方面,Scheme 系统的实现细节经常“泄露”,所以你可能会发现自己很关心它......

The terms aren't quite synonymous, but they aren't exclusive either (this answer assumes Scheme):

  • A special form (also known as a syntax in the Scheme Reports) is an expression that's not evaluated according to the default rule for function application. (The default rule, just to be explicit, is to eval all of the subexpressions, and then apply the result of the first one to the list of the results of the others.)
  • The macro system is a language feature that allows definition of new special forms within the language itself. A macro is a special form defined using the macro system.

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...

川水往事 2025-01-14 16:06:41

Lisp 具有某些语言原语,它们构成了 Lisp 形式:

  • 文字数据:数字、字符串、结构……
  • 函数调用,如 (sin 2.1)< /code> 或类似 ((lambda (ab) (+ ab 2)) 3 4)
  • 特殊运算符 用于特殊形式。这些是原始的内置语言元素。请参阅 Common Lisp 中的特殊运算符。这些都需要在解释器和编译器中实现。 Common Lisp 没有为开发人员提供引入新的特殊运算符或提供您自己的版本的方法。代码解析工具需要理解这些特殊运算符;这些工具在 Lisp 社区中通常被称为“code walkers”。在 Common Lisp 标准的定义过程中,确保数量非常小,并且所有扩展都是通过新函数和新宏完成的。
  • :宏是转换源代码的函数。转换将递归地发生,直到源代码中没有宏为止。 Common Lisp 具有内置宏并允许用户编写新宏。

因此,特殊形式和宏之间最重要的实际区别是:特殊运算符是内置的语法和语义。它们不能由开发人员编写。宏可以由开发人员编写。

Lisp has certain language primitives, which make up Lisp forms:

  • literal data: numbers, strings, structures, ...
  • function calls, like (sin 2.1) or like ((lambda (a b) (+ a b 2)) 3 4)
  • special operators used in special forms. These are the primitive built-in language elements. See Special Operators in Common Lisp. These need to be implemented in the interpreter and compiler. Common Lisp provides no way for the developer to introduce new special operators or to provide your own version of these. A code parsing tool will need to understand these special operators; these tools are usually called 'code walkers' in the Lisp community. During the definition of the Common Lisp standard, it was made sure that the number is very small and that all extensions otherwise are done via new functions and new macros.
  • macros: macros are functions which are transforming source code. The transformation will happen recursively until no macro is left in the source code. Common Lisp has built-in macros and allows the user to write new ones.

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.

无敌元气妹 2025-01-14 16:06:41

与特殊形式相反,宏形式可以进行宏扩展:

CL-USER(1): (macroexpand '(with-slots (x y z)
                              foo
                            (format t "~&X = ~A" x)))

(LET ((#:G925 FOO))
  (DECLARE (IGNORABLE #:G925))
  (DECLARE (SB-PCL::%VARIABLE-REBINDING #:G925 FOO))
  #:G925
  (SYMBOL-MACROLET ((X (SLOT-VALUE #:G925 'X))
                    (Y (SLOT-VALUE #:G925 'Y))
                    (Z (SLOT-VALUE #:G925 'Z)))
    (FORMAT T "~&X = ~A" X)))
T

In contrast to special forms, macro forms can be macroexpanded:

CL-USER(1): (macroexpand '(with-slots (x y z)
                              foo
                            (format t "~&X = ~A" x)))

(LET ((#:G925 FOO))
  (DECLARE (IGNORABLE #:G925))
  (DECLARE (SB-PCL::%VARIABLE-REBINDING #:G925 FOO))
  #:G925
  (SYMBOL-MACROLET ((X (SLOT-VALUE #:G925 'X))
                    (Y (SLOT-VALUE #:G925 'Y))
                    (Z (SLOT-VALUE #:G925 'Z)))
    (FORMAT T "~&X = ~A" X)))
T
〆凄凉。 2025-01-14 16:06:41

对我来说,最实际的区别在于调试器:宏不会显示在调试器中;宏不会显示在调试器中。相反,宏扩展中的(通常)模糊代码会显示在调试器中。调试这样的代码确实很痛苦,但在开始依赖宏之前,有充分的理由确保它们坚如磐石。

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.

何时共饮酒 2025-01-14 16:06:41

懒人的超级简短答案

您可以随时编写自己的宏,尽管您无法添加特殊形式而不重新编译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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文