Eval not working on unexpanded macro quote

2019-07-18 04:54发布

In common lisp I can do this:

src-> (defmacro macro-hello () `"hello")

(eval '(macro-hello))

no problem.

In clojure:

(defmacro macro-hello [] `"hello")

(eval '(macro-hello))

gives me an error. Have I done something wrong?

Clojure Error:

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 more Java Result: 1

[Edited]: added ending double quote

2条回答
地球回转人心会变
2楼-- · 2019-07-18 05:34

Your code works for me.

user> (defmacro macro-hello [] `"hello")
#'user/macro-hello
user> (eval '(macro-hello))
"hello"

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.

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

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.

(I'm assuming you made a typo in your Clojure code, with the missing double-quote.)

查看更多
做个烂人
3楼-- · 2019-07-18 05:39

I like to work out of /opt on Mac and Linux boxes. To get the Clojure source. (% is Unix prompt)

% cd /opt

% git clone git://github.com/richhickey/clojure.git; #From Unix command line, you'll have an /opt/clojure dir

% cd /opt/clojure

% /opt/netbeans-6.7.1/java2/ant/bin/ant; # Run ant. It ships with Netbeans.

% cd /opt; # mkdir /opt if it's not there.

% git clone git://github.com/richhickey/clojure-contrib.git; # Get contrib

% /opt/netbeans-6.7.1/java2/ant/bin/ant -Dclojure.jar=../clojure/clojure.jar ; # Tell ant where clojure.jar is located

I rename jars to clojure.jar and clojure-contrib.jar

Copy these jars to your Netbean's project lib directory.

查看更多
登录 后发表回答