On Sep 28, 12:13 pm, Michael Wood <esiot...@gmail.com> wrote:
> Hi
>
> 2009/9/26 gerryx...@gmail.com <gerryx...@gmail.com>:
>
>
>
> > (defn float2 [f a b]
> > (f (float a ) (float b)))
>
> > (float2 + 1 2) => 3.0
>
> > (defmacro mfloat2 [f a b]
> > (f (float a) (float b)))
> > (mfloat2 + 1 2 ) => 2.0 ??? macro expend to last expression in
> > list,right?
>
> This is because (mfloat2 + 1 2) expands to:
> + (float 1) (float 2)
This is wrong. There is no way for a macro to expand to multiple
expressions.
What happens is, when you call (mfloat + 1 2) the macro evaluates ('+
(float 1) (float 2)), ie. it calls the *symbol* + with parameters 1.0
and 2.0. Symbols, when used as functions, look themselves up in
whatever associative thing you give them as the first parameter, OR
return the second parameter in case of lookup failure. So, ('+ 1.0
2.0) evaluates to 2.0, which is the macro expansion. and as 2.0
evaluated is 2.0, it is the actual result.
in the defn case, it works because the f parameter is actually
evaluated, and thus it's the + *function* and not the symbol '+.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---