@Phlex & @MikeM, apologies I checked back on Chouser's post and it helped me
to understand that Clojure takes a different approach to macros than Common
Lisp:
Common Lisp
-----------------------------
(defmacro foobar ()
`(+ a b))
(let
((a 5)
(b 6))
(foobar))
Clojure
-----------------------------
(defmacro foobar []
`(+ ~'a ~'b))
(let
[a 5
b 6]
(foobar))
The is answer to the WHY: Clojure believes its a good thing that it's harder
to shoot yourself in the foot than Common Lisp. I agree.
Thanks
On Mon, Nov 3, 2008 at 2:35 PM, David Nolen <[EMAIL PROTECTED]> wrote:
> @Phlex, I understand why the code is usually bad form.
> @MikeM, I had already noticed that it worked on the REPL when I defined
> vars. My questions was _why_ it does not work in the case where I have
> locals via a let expression?
>
> David
>
> On Mon, Nov 3, 2008 at 12:27 PM, MikeM <[EMAIL PROTECTED]>wrote:
>
>>
>>
>>
>> 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 [email protected]
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
-~----------~----~----~----~------~----~------~--~---