Re:JDK7 to have escape analysis on by default...also back po...
by sunkencity on Wednesday October 07, 2009 @08:14AM
(#866549)
This is why clojure has a special form for tail call where you can "recur" a loop by re-binding the variables. When proper tail call comes to JVM, it'll be possible to do write the loop as a function call instead of hiding it inside a function.
(def factorial
(fn [n]
(loop [cnt n acc 1]
(if (zero? cnt)
acc
(recur (dec cnt) (* acc cnt))))))
Re:JDK7 to have escape analysis on by default...also back po...
by xtho on Wednesday October 07, 2009 @08:48AM
(#866588)
In java, you could use the "while" keyword for that.
Anyway, tail-calls aren't necessarily loops. If I'm not mistaken, tail-call optimisation for looping constructs only (which the poster a few levels above probably had in mind) wouldn't require a special byte code.
Re:JDK7 to have escape analysis on by default...also back po...
by tmountain on Wednesday October 07, 2009 @11:18AM
(#866861)
With the added benefit that it will warn you if you're not really in tail position. The only place where recur falls flat is with mutual recursion in which case you need to use trampoline. I love me some Clojure.
Right, that's the example that I had in mind.
I can see that for more complicated situations something more would be required, but I always figured that if the JVM is turing complete that it should be possible to 'target' any language to it without having to invent special bytecodes. Probably this is a direct consequence of the JVM and java being developed in tandem, it is not as general as I thought it was.
Thanks!