Possibly Pythonic Tail Call Optimization (TCO/TRE)

Ian Kelly ian.g.kelly at gmail.com
Tue Jul 14 04:13:37 EDT 2015


On Mon, Jul 13, 2015 at 11:57 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
> Ian Kelly <ian.g.kelly at gmail.com>:
>
>> On Mon, Jul 13, 2015 at 11:25 PM, Chris Angelico <rosuav at gmail.com> wrote:
>>> (Also, side point: Python can't actually optimize the above function,
>>> because it actually means "call quicksort, then discard its return
>>> value and return None". A true tail call has to return the result of
>>> the recursive call, and Python lacks a way to say "this function will
>>> always return None". But that's trivial.)
>>
>> Another point in favor of an explicit tail-call keyword. Then one
>> couldn't make that mistake.
>
> How about "return"?

I think you miss my point entirely. "return" doesn't mean tail-call
optimize; it just means to return the result. This is what led to the
confusion responsible for the bug that Chris pointed out in the first
place. With a keyword that explicitly means "perform tail-call
optimization *and* return", the association of the keyword with the
optimization is much clearer, and the programmer is much less likely
to mistakenly omit it.



More information about the Python-list mailing list