问题描述
在常见的Lisp中,我可以这样做:
In common lisp I can do this:
src->(defmacro宏,您好() 你好")
src-> (defmacro macro-hello () `"hello")
(eval'(宏-你好))
(eval '(macro-hello))
没问题.
在Clojure中:
(defmacro macro-hello [] 你好")
(defmacro macro-hello [] `"hello")
(eval'(宏-你好))
(eval '(macro-hello))
给我一个错误.我做错了什么吗?
gives me an error. Have I done something wrong?
Clojure错误:
Clojure Error:
线程"main"中的异常java.lang.Exception:在这种情况下无法解析符号:macro-hello(NO_SOURCE_FILE:12) 在clojure.lang.Compiler.analyze(Compiler.java:4340) 在clojure.lang.Compiler.analyze(Compiler.java:4286) 在clojure.lang.Compiler $ InvokeExpr.parse(Compiler.java:2767) 在clojure.lang.Compiler.analyzeSeq(Compiler.java:4498) 在clojure.lang.Compiler.analyze(Compiler.java:4325) 在clojure.lang.Compiler.analyze(Compiler.java:4286) 在clojure.lang.Compiler $ BodyExpr $ Parser.parse(Compiler.java:3862) 在clojure.lang.Compiler $ FnMethod.parse(Compiler.java:3697) 在clojure.lang.Compiler $ FnMethod.access $ 1100(Compiler.java:3574) 在clojure.lang.Compiler $ FnExpr.parse(Compiler.java:2963) 在clojure.lang.Compiler.analyzeSeq(Compiler.java:4494) 在clojure.lang.Compiler.analyze(Compiler.java:4325) 在clojure.lang.Compiler.eval(Compiler.java:4530) 在clojure.core $ eval__3990.invoke(core.clj:1728) 在com.yourcompany.defpackage $ _main__4.invoke(defpackage.clj:12) 在clojure.lang.AFn.applyToHelper(AFn.java:171) 在clojure.lang.AFn.applyTo(AFn.java:164) 在com.yourcompany.defpackage.main(未知来源)由以下原因引起:java.lang.Exception:无法解析符号:macro-hello在这种情况下 在clojure.lang.Compiler.resolveIn(Compiler.java:4682) 在clojure.lang.Compiler.resolve(Compiler.java:4628) 在clojure.lang.Compiler.analyzeSymbol(Compiler.java:4605) 在clojure.lang.Compiler.analyze(Compiler.java:4307) ...另外17个Java结果:1
Exception in thread "main" java.lang.Exception: Unable to resolve symbol: macro-hello in this context (NO_SOURCE_FILE:12) at clojure.lang.Compiler.analyze(Compiler.java:4340) at clojure.lang.Compiler.analyze(Compiler.java:4286) at clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:2767) at clojure.lang.Compiler.analyzeSeq(Compiler.java:4498) at clojure.lang.Compiler.analyze(Compiler.java:4325) at clojure.lang.Compiler.analyze(Compiler.java:4286) at clojure.lang.Compiler$BodyExpr$Parser.parse(Compiler.java:3862) at clojure.lang.Compiler$FnMethod.parse(Compiler.java:3697) at clojure.lang.Compiler$FnMethod.access$1100(Compiler.java:3574) at clojure.lang.Compiler$FnExpr.parse(Compiler.java:2963) at clojure.lang.Compiler.analyzeSeq(Compiler.java:4494) at clojure.lang.Compiler.analyze(Compiler.java:4325) at clojure.lang.Compiler.eval(Compiler.java:4530) at clojure.core$eval__3990.invoke(core.clj:1728) at com.yourcompany.defpackage$_main__4.invoke(defpackage.clj:12) at clojure.lang.AFn.applyToHelper(AFn.java:171) at clojure.lang.AFn.applyTo(AFn.java:164) at com.yourcompany.defpackage.main(Unknown Source)Caused by: java.lang.Exception: Unable to resolve symbol: macro-hello in this context at clojure.lang.Compiler.resolveIn(Compiler.java:4682) at clojure.lang.Compiler.resolve(Compiler.java:4628) at clojure.lang.Compiler.analyzeSymbol(Compiler.java:4605) at clojure.lang.Compiler.analyze(Compiler.java:4307) ... 17 moreJava Result: 1
[已编辑]:添加了结尾双引号
: added ending double quote
推荐答案
您的代码对我有用.
user> (defmacro macro-hello [] `"hello")
#'user/macro-hello
user> (eval '(macro-hello))
"hello"
这是通过流血的Clojure实现的. 无法解析符号"表示在当前名称空间中找不到名为macro-hello
的宏.您是从REPL还是在源文件中运行它?我在REPL上按字面意思输入了您的声明.
This is via bleeding-edge Clojure. "Unable to resolve symbol" means it can't find the macro called macro-hello
in the current namespace. Are you running this from the REPL or in a source file? I typed your statements at a REPL literally.
不确定这是否是您的问题原因,但请注意Clojure中`
和'之间的区别. `
执行名称空间解析,而'
不执行.
Not sure if this is a cause of problems for you, but please note the difference between `
and ' in Clojure. `
does namespace resolution and '
doesn't.
user> `macro-hello
user/macro-hello
user> 'macro-hello
macro-hello
这与Common Lisp的行为不同.反引号引用一个像`"hello"
这样的字符串没有多大意义,因为字符串不属于名称空间,但也不会造成任何伤害.
This is different from Common Lisp's behavior. Backtick-quoting a String like `"hello"
doesn't make much sense, since Strings don't belong to namespaces, but it also doesn't hurt anything.
(我假设您在Clojure代码中输入了错字,但缺少了双引号.)
(I'm assuming you made a typo in your Clojure code, with the missing double-quote.)
这篇关于评估不适用于未扩展的宏引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!