I am writing a rather long series of articles about Scheme on Artima, "The Adventures of a Pythonista in Schemeland" (maybe somebody here has heard of it). Last week I arrived at point of discussing hygienic macros (http://www.artima.com/weblogs/viewpost.jsp?thread=260195) and I wanted to spent a few words about the differences between Scheme and other lisps. In particular I want to asset the status of Clojure macros with respect to hygiene.
Since they look a lot like Common Lisp macros, I initially assumed that Clojure macros were not hygienic, but after performing some experiment I realized that I was half wrong. For instance, I expected this code to fail due to free-symbol capture: user=> (defmacro m [x] `(list ~x)) #'user/m user=> (let [list 1] (m 2)) (2) Instead, it works correctly. Therefore, I assume that Clojure is renaming the free symbols in the macro body (in this case 'list). On the other hand, Clojure macros are not fully hygienic, otherwise a would not be defined in this example: user=> (defmacro def-a[x] `(def a ~x)) #'user/def-a user=> (def-a 1) #'user/a user=> a 1 It seems that in order to avoid variable capture for bound symbols I need to use good old (gensym): user=> (defmacro def-a[x] (let [a (gensym)] `(def ~a ~x))) #'user/def-a user=> (def-a 1) #'user/G__77 Am I right in my assumptions? Is there a document explaining how Clojure macros work? The macro system seems quite unique, in the sense that I do not know of any other macro system working in the same way. Michele Simionato --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---