Brain Dump

Tail Call Optimisation

Tags
comp-arch

Is an optimisation technique for recursive functions where you can collapse multiple stack frames into a single stack frame that's reused for each recursive call.

def fac(i):
    if i <= 0:
	return 1
    return i * fac(i - 1)
Code Snippet 1: Example of a recursive function that could use tail-call optimisation. tail-call

In we have a function defined in terms of itself. Depending on the argument it returns 1 or the multiple of the current value and the function with the current argument take one. This could all be done in a single stack-frame by allocating a result variable, and then modifying it by the result of the function within the same stack-frame.