Hi, Am 25.07.2009 um 15:08 schrieb ajlopez:
1) Why the fn special form receives an array as list of parameters? That is, why Clojure is using: (fn [x y].... instead of (fn (x y) ... or both Any rationale behing this decision?
In Clojure vectors are used everywhere, where the grouping parentheses are used for in other Lisps is not a functional call. Examples are let, binding and - as you said - fn. So whenever you (something in parentheses), you can be normally be sure, that something is a kind of function. (eg. maps and keywords are also "functions", that is, they implement IFn).
2) I read in core.clj definitions like:
(def
#^{:arglists '([x])
:doc "Return true if x is a String"}
string? (fn string? [x] (instance? String x)))
Why not:
(def
#^{:arglists '([x])
:doc "Return true if x is a String"}
string? (fn [x] (instance? String x)))
that is, fn WITHOUT the name of the defined function. The name after
fn could be used in a recursive call, but, is it needed in this
context? for debugging purpose?
A side effect of naming the anonymous fn is that instead of "fn__34" you can read "string_QMARK" in a stack trace. This helps when debugging. I often do this for multimethods because it's otherwise difficult to find out which method exactly was invoked. (defmethod foo [String Integer] foo--string-integer [a b] ...)
3) Why the redefs like:
;during bootstrap we don't have destructuring let, loop or fn, will
redefine later
(def
#^{:macro true}
let (fn* let [& decl] (cons 'let* decl)))
(def
#^{:macro true}
loop (fn* loop [& decl] (cons 'loop* decl)))
(def
#^{:macro true}
fn (fn* fn [& decl] (cons 'fn* decl)))
from loop* to loop, let* to let, fn* to fn... Are all *-ended names
special forms?
loop, let and fn are "special" forms. For convenience however the support a feature called destructuring. (let [[a [b c]] [1 [2 3]]] [a b c]) => [1 2 3] This works also in loop and in the argument list of fns. However this feature is implemented in Clojure, hence it is not available at that point in time. Later, when the prerequisites are defined the macros are redefined to use destructuring and the expand into a call of the corresponding *-form. The *-forms are the "real" special forms.
4) why not a def for macros? In the above fragment, I read that "fn is a macro" is specified in a metadata tag. Why this decision? I'm playing with an interpreter written in C# (not a compiler) and the only metadata I need to process, by now, is the meta tag...
There is defmacro, which defines a macro. At this early point it's not defined, yet. Hence the manual definition with the metadata. Macros are just functions with the special macro tag in the metadata of the Var holding them. This tag is inquired by the compiler to see whether it's a function or a macro. Hope this helps. Sincerely Meikel
smime.p7s
Description: S/MIME cryptographic signature
