Re: Understanding Clojure Closures

2009-11-11 Thread mbrodersen
Thanks Howard. Another great answer. Morten On Nov 12, 2:58 am, Howard Lewis Ship 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. > >

Re: Understanding Clojure Closures

2009-11-11 Thread mbrodersen
Great answer Alex. Thanks! Morten On Nov 12, 12:34 am, Alex Osborne wrote: > mbrodersen wrote: > > 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

Re: Understanding Clojure Closures

2009-11-11 Thread Howard Lewis Ship
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-r

Re: Understanding Clojure Closures

2009-11-11 Thread Meikel Brandmeyer
Hi, On Nov 11, 2:34 pm, Alex Osborne wrote: > (let [t (fn [x] (if (zero? x) 0 (+ x (t (dec x)] (t 2)) But also note, that you can give an anonymous function a name. %) (let [t (fn t [x] (if (zero? x) 0 (+ x (t (dec x)] (t 2)) Sincerely Meikel -- You received this message because you

Re: Understanding Clojure Closures

2009-11-11 Thread Alex Osborne
mbrodersen wrote: > 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

Understanding Clojure Closures

2009-11-11 Thread mbrodersen
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 c