On Sep 2, 2012, at 3:00 AM, Leo Antoli wrote:
> Hi,
> I understand JVM doesn't support tail recursion optimization yet.
>
> But couldn't it be done by Clojure itself ? I guess I'm missing something, but for instance for this code:
>
> (def lineage-1
> (fn [class-symbol so-far]
> (if (nil? class-symbol) so-far
> (lineage-1
> (class-symbol-above class-symbol)
> (cons class-symbol so-far)))))
>
> the Reader and/or Eval could analyze it and add "recur" keyword if possible to get:
>
> (def lineage-1
> (fn [class-symbol so-far]
> (if (nil? class-symbol) so-far
> (lineage-1
> (recur
> (class-symbol-above class-symbol)
> (cons class-symbol so-far))))))
I think the answer is that the way `def` works is that:
1. It creates the symbol `lineage-1`.
2. It creates a function that has a reference to the symbol `lineage-1`.
3. It binds the new function to the symbol `lineage-1`.
Note that at the time of step 2, the function doesn't know that `lineage-1` will be its own future name.
-----
Brian Marick, Artisanal Labrador
Contract programming in Ruby and Clojure
Occasional consulting on Agile
Writing /Functional Programming for the Object-Oriented Programmer/:
https://leanpub.com/fp-oo