views:

288

answers:

2

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  A: 

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

Brian Carper
Sorry about the ending double quote.I can do this no problem:(eval '(eq 0 0))Source file. I'm using enclojure with netbeans. Right now I can't get the REPL to work.What version of clojure are you using? Maybe that the problem.The eval function should have access to the environment. And it does if you use an already pre-defined variable (function/macro). It is only when I try to eval on my own created functions/macros that it soesn't work.
Gutzofter
I'm using bleeding-edge Clojure from git (http://github.com/richhickey/clojure/tree/master). If you start a REPL from a command line via `java -cp clojure.jar clojure.main` does it work? It could be Enclojure giving you problems.
Brian Carper
that's good to know, but I would then need to know how to put it into a jar file
Gutzofter
You can replace the clojure.jar in the lib dir in Netbeans. I'll put the instructions to get the source in an answer.
Sorry Brian, I missed this:"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."backquote is different in clojure? Interesting, I will have to research this.
Gutzofter
+1  A: 

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.

Thanks for the instructions
Gutzofter