Tail recursion to while iteration in 2 easy steps

Jussi Piitulainen jpiitula at ling.helsinki.fi
Tue Oct 8 03:25:05 EDT 2013


Steven D'Aprano writes:

> Far more useful would be a high-level description of Scheme's
> programming model. If names can be rebound on the fly, how does
> Scheme even tell whether something is a recursive call or not?
> 
> def foo(arg):
>     do stuff here
>     foo(arg-1)  # how does Scheme know that this is the same foo?

In general, it doesn't know. It just calls whatever function is bound
to foo. It does know that the call is in a tail position.

If the compiler has access to all code that can possibly change the
value of foo, it can know simply by proving that there is no such
assignment statement in the code. This can happen if the compiler is
told to assume that it has the whole program. It often happens in a
local scope. Module systems create such local scopes for unexported
variables, and even for exported variables by forbidding assignments
outside.

(I'm not sure if your question was rhetorical or if you were looking
for this information.)



More information about the Python-list mailing list