Here's an update on syntax-quote in the WikiBook (Reader Macro
section):

The most complicated reader macro is syntax-quote, denoted by ` (back-
tick). When used on a symbol, syntax-quote is like quote but the
symbol is resolved to its fully-qualified name:

`meow    ; (quote cat/meow) ...assuming we are in the namespace cat

When used on a list, vector, or map form, syntax-quote quotes the
whole form except, a) all symbols are resolved to their fully-
qualified names and, b) components preceded by ~ are unquoted:

(defn rabbit [] 3)
`(moose ~(rabbit))     ; (quote (cat/moose 3))   ...assume namespace
cat

(def zebra [1 2 3])
`(moose ~zebra)        ; (quote (cat/moose [1 2 3]))

Components preceded by ~@ are unquote-spliced:

`(moose ~...@zebra)       ; (quote (cat/moose 1 2 3))

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:

`(moose ~(squirrel `(whale ~zebra)))

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)

may be interpreted to mean

(concat |x1| |x2| |x3| ... |xn|)

where the | | are used to indicate a transformation of an xj as
follows:

    * |form| is interpreted as (list `form), which contains a syntax-
quoted form that must then be further interpreted.

    * |~form| is interpreted as (list form).

    * |~...@form| is interpreted as form.

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, an expression such as

``(~~a)

would be expanded as follows:

(concat (list `concat) (list (concat (list `list) (list a))))

and then evaluated.

Of course the same expression could also be equivalently expanded as

(list `list a)

which is indeed much easier to read. Clojure employs the first
algorithm which is more generally applicable in cases where there is
also splicing.

The principle is that the result of an expression with syntax-quotes
nested to depth k is the same only after k successive evaluations have
been performed, regardless of the kind of expansion algorithm (Guy
Steele).

For Vectors, Maps, and Sets we have the follwing rules:

`[x1 x2 x3 ... xn]

is equivalent to

(apply vector `(x1 x2 x3 ... xn))

`{x1 x2 x3 ... xn}

is equivalent to

(apply hash-map `(x1 x2 x3 ... xn))

`#{x1 x2 x3 ... xn}

is equivalent to

(apply hash-set `(x1 x2 x3 ... xn))

At this time, Clojure does not allow you to define your own reader
macros, but this may change in the future.
--~--~---------~--~----~------------~-------~--~----~
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
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to