More info on syntax-quote
22 May 2009, 18:24
Rocco Rossi (1 post)
I would really like to see more information regarding syntax-quote, especially the behind-the-scenes expansion algorithm as documented here:
In particular, I feel this info could be very useful in the book:
If a symbol is non-namespace-qualified and ends with ‘#’, it is resolved to a generated symbol with the same name to which ‘_’ and a unique id have been appended. e.g. x# will resolve to x_123. All references to that symbol within a syntax-quoted expression resolve to the same generated symbol.
For all forms other than Symbols, Lists, Vectors and Maps, `x is the same as ‘x.
Syntax-quotes can be nested within other syntax-quotes:
For Lists syntax-quote establishes a template of the corresponding data structure. Within the template, unqualified forms behave as if recursively syntax-quoted.
`(x1 x2 x3 … xn)
is interpreted to mean(clojure.core/concat x1 x2 x3 … xn ) where the are used to indicate a transformation of an xj as follows:
If the syntax-quote syntax is nested, the innermost syntax-quoted form is expanded first. This means that if several ~ occur in a row, the leftmost one belongs to the innermost syntax-quote.
Following the rules above, and assuming that the var a contains 5, an expression such as
would be expanded (behind the curtains) as follows:
(clojure.core/concat (clojure.core/list (quote clojure.core/concat)) (clojure.core/list (clojure.core/concat (clojure.core/list (quote clojure.core/list)) (clojure.core/list a))))
and then evaluated, producing;
(clojure.core/concat (clojure.core/list 5))
24 Jun 2009, 13:08
Stuart Halloway (76 posts)
I think the level of detail in the book is appropriate given length constraints, but I am hoping to write something longer and more detailed that would include a gentler yet more thorough introduction to macros. Thanks for the feedback!
|You must be logged in to comment|