2 ms·
I'm also fairly sure that tail recursion can be done as a compiler trick. Only for a single function, at least on the JVM. For mutually recursive functions (ie
by ori_b 18y ago
I'm also fairly sure that tail recursion can be done as a compiler trick.
Only for a single function, at least on the JVM. For mutually recursive functions (ie, a() tailcalls b() which tailcalls a()) you either need the VM to do the optimization, or some explicit control over the stack pointer, or implementing your own function call model within one function.
For the first, you would need some sort of new tailcall instruction, or the VM would have to be guaranteed to do tail call optimizations. (You can't really give control of the stack explicitly while keeping things portable and efficient)
For the second, it's workable... if you don't care about interoperating with other languages on the JVM.