Some VMs, most notably the JVM, are said to not support TCO. As a result, language like Clojure require the user to use loop
recur
instead.
However, I can rewrite self-tail calls to use a loop. For example, here's a tail-call factorial:
def factorial(x, accum):
if x == 1:
return accum
else:
return factorial(x - 1, accum * x)
Here's a loop equivalent:
def factorial(x, accum):
while True:
if x == 1:
return accum
else:
x = x - 1
accum = accum * x
This could be done by a compiler (and I've written macros that do this). For mutual recursion, you could simply inline the function you're calling.
So, given that you can implement TCO without requiring anything of the VM, why don't languages (e.g. Clojure, Armed Bear Common Lisp) do this? What have I missed?
defn
. – Slosh