4 ms·
> Clojure has run into a lot of problems due to the JVM, probably the most famous one being the lack of tail call optimization. What in particular about the JV
by cabalamat 17y ago
> Clojure has run into a lot of problems due to the JVM, probably the most famous one being the lack of tail call optimization.
What in particular about the JVM prevents Clojure from being able to do tail-call optimization? My (admittedly naive) understanding of TCO is that it involves turning tail recusion into a loop, and Java can certainly handle loops OK.
- WilliamLP 17y agoI'm naive too, but apparently there are use cases for TCO for state machines where the tail calls are between various functions, not necessarily in a simple loop.
- crux_ 17y ago> My (admittedly naive) understanding of TCO is that it involves turning tail recursion into a loop This is close, but not the full story. TCO turns tail recursion into a GOTO (well, a goto-with-arguments). In the case of a single function which tail-calls itself, this becomes a loop, but for multiple functions that are mutually recursive, that's not the case. Note that the issues with the lack of TCO boil down to efficiency: there are ways of faking TCO atop a runtime that does not support it (meaning deeply tail-recursive code will run, rather than overflow the stack), but they come at with a runtime cost.