On 21.04.2009, at 00:26, Mark Volkmann wrote:
> Is it correct to say that macros are expanded at read-time?
No. Only reader macros are expanded at read time, standard macros are
expanded at compile time.
A more subtle point concerns the distinction between compile time and
run time, because it doesn't really exist, although the distinction
is still a useful approximation. Compilation happens when a form
defining a function is evaluated, but other evaluations happen as
well. Instead of "compile time", I'd say "namespace evaluation time",
and instead of "run time", I'd say "program evaluation time" or
something like that.
Example:
(let [x (* 2 3)]
(defn foo [y] (+ x y)))
After the file containing this piece of code has been read, and thus
transformed into a list of lists and vectors, it is evaluated. The
first subexpression to be evaluated is (* 2 3), whose result is bound
to x. Then the macro form (defn ...) is expanded, resulting in
(def foo (fn [y] (+ x y)))
(fn ...) is a special form (well, not quite, there is another of
macro but I'll skip that) whose evaluation causes the function to be
compiled. The result, a "compiled function" object, becomes the value
of the var foo.
Note that a macro definition like defn is a function, defined in the
namespace clojure.core which has been evaluated already. Note also
that the function that defines the defn macro calls other functions,
such as cons, conj, first, next, etc., for which the evaluation of
the example code is "run time".
In summary, there are really only two phases in the preparation of a
namespace, "read time" and "evaluation time". Macros are called as
part of the evaluation process. Compilation is really an
implementation detail in all that. LIke other Lisps, Clojure could be
implemented as a pure interpreter, a pure compiler, or a mixture of
the two. It happens to be a pure compiler (even the (* 2 3) in the
example above is compiled, then executed, and the compiled expression
is discarded), but you don't need to know that to understand what the
result of the evaluation is.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---