A new module for performing tail-call elimination

Terry Reedy tjreedy at udel.edu
Thu Jul 16 21:34:12 CEST 2015


On 7/16/2015 3:45 AM, Antoon Pardon wrote:
> 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.  ;-)

Glad you appreciate it. To me, the warping is no more than, and perhaps 
less than, and certainly less obnoxious than, the warping required when 
using Baruchel's tco module.  (Opinions can vary, of course.)  The 
result will definitely run faster than with B's tco.

-- 
Terry Jan Reedy



More information about the Python-list mailing list