A new module for performing tail-call elimination

Antoon Pardon antoon.pardon at rece.vub.ac.be
Thu Jul 16 03:45:26 EDT 2015


On 07/15/2015 11:19 PM, Terry Reedy wrote:
> On 7/15/2015 5:29 AM, Antoon Pardon wrote:
>>
>> Can you explain how you would do mutual recursive functions?
>> Suppose I start with the following:
>>
>> def even(n):
>>      True if n == 0 else odd(n - 1)
>>
>> def odd(n):
>>      False if n == 0 else even(n - 1)
>>
>> How do I rewrite those with your module?
>
> I will not answer for Baruchel's tco module.  However, combining the
> two bodies and following the general rule of replacing tail recursive
> calls with assignments inside a while loop gives us
>
> def even(n):
>     return not odd(n)
>
> def odd(n):
>     while True:
>         if not n:
>             return False
>         else:
>             n  -= 1
>         if not n:
>             return True
>         else:
>             n -= 1
> [ ... ]
>
> I believe that this pattern should work with any set of mutually
> recursive functions that always call each other in cyclic order.  A
> more elaborate version does not have this limitation.
>

Nice of you to illustrate the warping involved.  ;-) 

-- 
Antoon Pardon.




More information about the Python-list mailing list