Clojure 中的匿名宏
在 Common LISP 中,您可以执行以下操作
(macro lambda (x) (list (quote car) (list (quote cdr) x)))
看起来这在 Clojure 中是不可能的(匿名宏)。
- 这是真的吗?
- 为什么这个被遗漏了?
In Common LISP you can do the following
(macro lambda (x) (list (quote car) (list (quote cdr) x)))
It looks like this is not possible (an anonymous macro) in Clojure.
- Is this true?
- Why was this left out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
clojure.contrib.macro-utils
中有一些macrolet
(或其他东西)。然而,对我来说,它闻起来有点像黑客。包含在clojure.core
中已在 TODO 雷达上,但目前没有高优先级。编辑:另请参阅此处 http://dev.clojure.org/display/design/letmacro 。
There is some
macrolet
(or other things) inclojure.contrib.macro-utils
. However it smells a little like a hack to me. Inclusion inclojure.core
is on the TODO radar, but doesn't have high priority at the moment.EDIT: See also here http://dev.clojure.org/display/design/letmacro.
这是我的理解(通过阅读文档和来源),Clojure 目前(从版本 1.2 开始)仅支持使用 defmacro 定义的命名宏。
然而,您有可能滥用 Clojure 的内部结构,通过直接对某些匿名变量调用 Java 函数 Var.setMacro() 来获取某种匿名宏。这就是 defmacro 在底层使用的东西来构造宏。
例如,您可以这样做:
Clojure 阅读器随后会将 Macro-var 中包含的 var 解释为宏。然而,您必须弄清楚如何正确定义所有内容,并且在 Clojure 的未来版本中可能会全部崩溃。
Clojure 1.3(目前处于 alpha 版本,但已完全运行)带来了各种其他增强功能,例如符号宏,因此也值得一看。
It is my understanding (from reading the documentation and the source) that Clojure currently (as of version 1.2) only supports named macros defined with defmacro.
It's possible however that you can abuse the internals of Clojure to get some sort of anonymous macro by directly calling the Java function Var.setMacro() on some anonymous var. This is what defmacro uses under the hood to construct macros.
e.g. you can do:
And the Clojure reader will subsequently interpret the var contained in macro-var as a macro. However you'll have to figure out how to define everything correctly and it might all break in future versions of Clojure.
Clojure 1.3 (currently in alpha, but fully operational) brings various other enhancements, e.g. symbol macros, so might also be worth a look.