On Nov 3, 9:57 am, David Nolen <[EMAIL PROTECTED]> wrote:
> (defmacro foobar []
>   `'(+ a b))
>
> (let
>     [a 5
>      b 6]
>   (eval (foobar)))
>
> I know that the above is completely useless but I'm just trying to get
> an intuitive understanding of how macros work under Clojure.  At the
> REPL when I try to evaluate the second form, I get an null pointer
> exception.  Why can't the result of the foobar macro access the values
> of a and b?
>

If you macroexpand,

>(macroexpand '(foobar))

you'll see that the a and b symbols in your macro are resolved to be
vars in the namespace in which the macro is defined (I defined the
macro in the user namespace):

(quote (clojure/+ user/a user/b))

Note that symbol + was resolved to be the + function from boot.clj,
since my user namespace refers to the clojure namespace.

So when the foobar macro is run, the resulting code doesn't look at
the a and b in your let, but instead tries to find vars named a and b.
If you:

> (def a 1)
> (def b 2)

then execute

> (let
>     [a 5
>      b 6]
>   (eval (foobar)))

you'll get 3.

Hope that helps.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to