A new module for performing tail-call elimination

Ethan Furman ethan at stoneleaf.us
Thu Jul 16 20:02:15 CEST 2015


On 07/16/2015 06:35 AM, Antoon Pardon wrote:
> On 07/16/2015 01:11 PM, Chris Angelico wrote:

>> Unless, of course, *all* TCO examples, even real-world ones, could be
>> trivially reimplemented some other way, a theory which is gaining
>> currency...
>
> Of course they could be rather trivially reimplemented. They would
> also become rather ugly and less easy to comprehend.
>
> Here is one way to do the odd, even example.
>
> def even(n):
>      return odd_even('even', n)
>
> def odd(n):
>      return odd_even('odd', n)
>
> def odd_even(fn, n):
>      while fn is not None:
>          if fn == 'even':
>              fn, n = (None, True) if n == 0 else ('odd', n - 1)
>          elif fn == 'odd':
>              fn, n = (None, False) if n == 0 else ('even', n - 1)
>          else:
>              raise ValueError("Unknown state: %s" % fn)
>      return n

Wow -- definitely uglier and less easy to understand than the original (even if the original had had the error-checking).

--
~Ethan~


More information about the Python-list mailing list