Thanks Howard. Another great answer. Morten
On Nov 12, 2:58 am, Howard Lewis Ship <hls...@gmail.com> wrote: > Symbols are late resolved to functions. > > (def t (fn ...)) means define a Var bound to symbol t, and store the > function in it. In JVM terms, the function becomes a new class that is > instantiated. > > (t (dec x)) means locate the Var bound to symbol t -- at execution > time (not compilation time) --- de-reference the function object > stored there and invoke it. > > In fact, t can be temporarily rebound (using the binding macro) or > even completely replaced (using alter-var-root!) at any time. The Var > remains the same but the contents of the Var are changed. > > Because of how Clojure is structured, the Var object need only be > resolved from the symbol once (in the generated Java bytecode, the Var > appears as a static final field). > > > > On Wed, Nov 11, 2009 at 5:17 AM, mbrodersen <morten.broder...@gmail.com> > wrote: > > A quick question about how closures work in Clojure. > > > In this simple recursive expression: > > > (def t (fn [x] (if (zero? x) 0 (+ x (t (dec x)))))) > > > The fn special form is evaluated within a context where t is not yet > > bound. > > > t is only bound AFTER fn has captured its environment. > > > In other words, the closure captured by fn doesn't know anything about > > t (or maybe only that t is unbound). > > > And yet it still works if I call t: > > > (t 5) => 15 > > > My question is how Clojure ensures that t is bound to the closure > > after the closure has already been captured? > > > Thanks > > Morten > > > -- > > 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 > > Note that posts from new members are moderated - please be patient with > > your first post. > > 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 > > -- > Howard M. Lewis Ship > > Creator of Apache Tapestry > > The source for Tapestry training, mentoring and support. Contact me to > learn how I can get you up and productive in Tapestry fast! > > (971) 678-5210http://howardlewisship.com -- 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 Note that posts from new members are moderated - please be patient with your first post. 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