3 ms·
I would very much like a very simple, basic, tail recursion to be available in Java. Not tail call elimination in the same way as is specified by Scheme. It doe
by strlen 16y ago
I would very much like a very simple, basic, tail recursion to be available in Java. Not tail call elimination in the same way as is specified by Scheme. It doesn't even require the a JVM change, it can be done by the compiler: Scala does this. You could use an annotation (@Tailrecursive) to verify that it happens (or error out).
C does this with "-O2" (factorial is a bad example, unfortunately as there are algorithms that are much more naturally defined in terms of tail recursion):
long factorial_helper(int n, int acc)
{
if (n==1)
return acc;
return factorial_helper(n - 1, n * acc);
}
becomes:
factorial_helper:
.LFB16:
jmp .L8
.p2align 4,,7
.L6:
imull %edi, %esi
subl $1, %edi
.L8:
cmpl $1, %edi
jne .L6
movslq %esi,%rax
ret
Looks very similar to what my own assembly would look like. Now with -O4 the assembly becomes completely unrecognizable (which is why higher level languages may be faster than lower level ones).
Continuations would be another improvement possible at language rather than VM level, with same effect (allowing efficient and elegant implementation of some algorithms).