Thanks for your reply. I guess I was forgetting that at macroexpansion time 
(if I'm using the word correctly), we don't have access to the binding 
between u and 10. The same error I message I noted above does appear for (+ 
2 'u).

And, if I understand correctly, what was really happening with the macro 
bar was that in ('+ 2 'u), '+ was looking itself up in 2, not finding 
itself, and so returning the default value 'u. This was the expansion of 
the macro, and so at run time, it was bound to 10.

I think.

On Tuesday, January 26, 2016 at 2:02:22 PM UTC-8, Dan Burton wrote:
>
> > I know that this macro will add 2 to its argument:
>
> This isn't what that macro does. That macro takes a piece of syntax, and 
> sticks that syntax in the hole in the expression (+ 2 HOLE). The 
> macro doesn't evaluate the expression, it just generates it. It is the 
> evaluation of *that* expression that yields the plus-two result.
>
> The arguments to a macro are not values, they are syntax. The result of a 
> macro isn't a value, it's syntax. When you call (foo u), you are not 
> passing the value of u into the macro. You are passing in the syntactic 
> symbol u. That's why it says you can't add a Symbol to a Number. Your macro 
> is trying to compute (at "compile time") (+ 2 'u). 
>
> On Tuesday, January 26, 2016, <echigoy...@gmail.com <javascript:>> wrote:
>
>> I'm new to clojure and macros and am having trouble wrapping my head 
>> around some simple things.
>>
>> I know that this macro will add 2 to its argument:
>>
>> user=> (defmacro plus-two [x] `(+ 2 ~x))
>> #'user/plus-two
>> user=> (plus-two 5)
>> 7
>> user=> (def u 10)
>> #'user/u
>> user=> (plus-two u)
>> 12
>>
>> I tried the following just to see what will happen and don't understand 
>> what's going on:
>>
>> user=> (defmacro foo [x] (+ 2 x))
>> #'user/foo
>> user=> (foo 5)
>> 7
>> user=> (foo u)
>>
>> ClassCastException clojure.lang.Symbol cannot be cast to 
>> java.lang.Number  clojure.lang.Numbers.add (Numbers.java:128)
>>
>> I tried quoting the plus:
>>
>> user=> (defmacro bar [x] ('+ 2 x))
>> #'user/bar
>> user=> (bar 5)
>> 5
>> user=> (bar u)
>> 10
>>
>> This makes sense, since (bar u) is the same as + 2 u (without 
>> parentheses), which returns u. But I don't understand what's happening with 
>> foo, and I can't check it with macroexpand since that gives the same error. 
>> Is there a way to recreate this error without defining a macro?
>>
>> Thanks in advance for any and all responses.
>>
>>
>> -- 
>> 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
>> Note that posts from new members are moderated - please be patient with 
>> your first post.
>> To unsubscribe from this group, send email to
>> clojure+unsubscr...@googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/group/clojure?hl=en
>> --- 
>> You received this message because you are subscribed to the Google Groups 
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to clojure+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
> -- 
> -- Dan Burton
>

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to