# Learning Clojure/Reader Macros
## 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.
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:
Applying syntax-quote to an atomic value expands to that same value. For instance:
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:
Components preceded by ~@ are unquote-spliced:
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.
is interpreted to mean
where the | | 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 is expanded first. This means that if several ~ occur in a row, the leftmost one belongs to the innermost syntax-quote.
An important exception is the empty list:
is interpreted to mean
Following the rules above, and assuming that the var a contains 5, an expression such as
would be expanded (behind the curtains) as follows:
and then evaluated, producing;
Of course the same expression could also be equivalently expanded as
which is indeed much easier to read. Clojure employs the former 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 are performed, regardless of the expansion algorithm (Guy Steele).
For Vectors, Maps, and Sets we have the following rules:

### Nested Syntax-quotes

Syntax-quote is easiest to understand if we define it by saying what a syntax-quoted expression returns. To evaluate a syntax-quoted expression, you remove the syntax-quote and each matching tilde, and replace the expression following each matching tilde with its value. Evaluating an expression that begins with a tilde causes an error.
A tilde matches a syntax-quote if there are the same number of tildes as syntax-quotes between them, where b is between a and c if a is prepended to an expression containing b, and b is prepended to an expression containing c. This means that in a well-formed expression the outermost syntax-quote matches the innermost tilde(s).
Suppose that x evaluates to user/a, which evaluates to 1; and that y evaluates to user/b, which evaluates to 2.
You can prepare that from the REPL as follows:
To evaluate the expression
we remove the first syntax-quote and evaluate what follows any matching tilde. The rightmost tilde is the only one that matches the first syntax-quote. If we remove it and replace the expression it's prepended to, y, with its value, we get:
Notice how x got "resolved" to user/x. Any unqualified symbol gets resolved in Clojure! This differentiates it (for the better) from Common Lisp.
In this latter expression, both of the tildes match the syntax-quote, so if we were to evaluate it in turn, we would get:
A tilde-at (~@) behaves like a tilde, except that the expression it's prepended to must both occur within and return a list or sequence in general. The elements of the returned sequence are then spliced into the containing sequence. So
evaluates to

### User-defined Reader Macros

At this time, Clojure does not allow you to define your own reader macros, but this may change in the future.