检查 defmacro (clojure) 中的符号相等性
这将返回false
。
(defmacro scratch [pattern]
`(= 'b (first ~pattern)))
(scratch '(b))
然而,以下的输出是b
。
(defmacro scratch2 [pattern]
`(first ~pattern))
(scratch2 '(b))
如何设置第一个宏返回 true
?
This returns false
.
(defmacro scratch [pattern]
`(= 'b (first ~pattern)))
(scratch '(b))
However, the output of the following is b
.
(defmacro scratch2 [pattern]
`(first ~pattern))
(scratch2 '(b))
How do I setup the first macro to return true
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正在发生这种情况,因为您在宏中引入的 'b 是命名空间的:
例如:
而您传递的值不是:
所以,您可以将命名空间符号传递给宏,如下所示:
或者您可以修复宏以使用无命名空间符号(qoute-unquote 的已知技巧):
但您真正想要的是在编译时检查这个符号,因为您拥有的这个宏作为普通函数更好,因为它不使用任何与宏相关的优点。
它可能看起来像这样:
可以找到有关命名空间的信息 本文
that is happening, because the 'b that you introduce in macro is namespaced:
example:
while the value you pass isn't:
so, you can pass the namespaced symbol to a macro, like this:
or you can fix you macro to use unnamespaced symbol (known trick with qoute-unquote):
but what you really want, is to check this one in compile-time, because this macro you have is better as a plain function, since it doesn't employ any macro-related goodness.
It could look like this:
something about namespacing can be found in this article