On Jan 13, 11:35 am, Rock <rocco.ro...@gmail.com> wrote:
> I've added some info regarding the backquote expansion mechanism in
> the Reader section here:
>
> http://en.wikibooks.org/wiki/Learning_Clojure#The_Reader
>
> I tried to answer the author's question regarding the possible
> expansion order in nested backquotes and the general algorithm Clojure
> apparently employs, which seems to be very similar to the one
> "suggested" in the CL HyperSpec, though practically every CL
> implementation adopts its own and is free to do so.
>
> I hope I get his and, of course, Rich's blessing.
>
> Happy Clojure.
>
> Rock

I've just noticed that my changes attend approval to be viewed by
anyone else.

Here's the part I edited:




Reader Macros

A reader macro (not to be confused with a regular macro) is a special
character sequence which, when encountered by the reader, modifies the
reader behavior. Reader macros exist for syntactical concision and
convenience.

'foo                  ; (quote foo)

#'foo                 ; (var foo)

@foo                  ; (clojure/deref foo)

^foo                  ; (clojure/meta foo)

#^{:ack bar} foo      ; (clojure/with-meta foo {:ack bar})

#"regex pattern"      ; create a java.util.regex.Pattern from the
string (this is done at read time,
                      ; so the evaluator is handed a Pattern, not a
form that evaluates into a Pattern)

#(foo %2 bar %)       ; (fn [a b] (foo b bar a))

The #() syntax is intended for very short functions being passed as
arguments. It takes parameters named %, %2, %3, %n ... %&.

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/Vectors/Maps, 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

(clojure.core/concat [x1] [x2] [x3] ... [xn])

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

    * [form] is interpreted as (clojure.core/list `form), which
contains a syntax-quoted form that must then be further interpreted.

    * [~form] is interpreted as (clojure.core/list form).

    * [...@form] is interpreted as form.

If the syntax-quote syntax is nested, the innermost syntax-quoted form
should be 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).

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