On Thu, May 28, 2009 at 5:42 PM, Meikel Brandmeyer <[email protected]> wrote:
> Am 28.05.2009 um 23:29 schrieb CuppoJava:
>
>> In my recent macro-writing adventure, I discovered that
>> (gensym) is not actually equivalent to using #. Can
>> someone explain to me how # actually works in backquoted
>> form?
>
> The foo# form will replace the foo symbol statically in the
> syntax-quote form. So when you call deftemp the second
> time, you basically reassign the same Var you assigned
> to before.
>
> With gensym you always create a new symbols. Hence
> there is no conflict.
That's a fascinating point, and I hadn't considered it
before. It's subtle enough it might be worth go over again.
Here are two functions the do roughly the same thing --
return a list with a single gensym'ed symbol in it:
(defn f-auto []
`(foo#))
(defn f-gen []
(let [foo (gensym "foo_")]
`(~foo)))
But when called multiple times, you can clearly see the
difference, where f-auto's auto-gensym produces the same
symbol every time:
user=> (f-gen)
(foo_442)
user=> (f-gen)
(foo_446)
user=> (f-gen)
(foo_450)
user=> (f-auto)
(foo__425__auto__)
user=> (f-auto)
(foo__425__auto__)
user=> (f-auto)
(foo__425__auto__)
Thanks for bringing this up.
--Chouser
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---