Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'received:134': 0.05; 'rewrite': 0.07; 'subject:module': 0.09; 'def': 0.13; '11:19': 0.16; 'assignments': 0.16; 'cyclic': 0.16; 'module?': 0.16; 'received:ac.be': 0.16; 'reedy': 0.16; 'true:': 0.16; 'wrote:': 0.16; ';-)': 0.18; 'suppose': 0.22; 'am,': 0.23; 'replacing': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'order.': 0.27; 'module.': 0.27; 'tail': 0.29; 'received:be': 0.30; 'rule': 0.33; 'gives': 0.35; 'false': 0.35; 'should': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'version': 0.38; 'does': 0.39; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'more': 0.63; 'believe': 0.66; 'combining': 0.66; 'pardon': 0.84; 'involved.': 0.91 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AnUFAOZfp1WGuA9G/2dsb2JhbABbhCsBg0i9QIJUAoINAQEBAQEBhS8BAQQjVRELGAICBRYLAgIJAwIBAgFFEwgCiCq5HJEyhHgBAQgCIIEiiiqFDRaCUoFDAQSURodOhEqBQ4Z4jGODYSaDfoM4AQEB Date: Thu, 16 Jul 2015 09:45:26 +0200 From: Antoon Pardon User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Icedove/31.7.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: A new module for performing tail-call elimination References: <55a3dcd9$0$3024$426a34cc@news.free.fr> <55A6280C.3090602@rece.vub.ac.be> In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1437032728 news.xs4all.nl 2868 [2001:888:2000:d::a6]:57383 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:93913 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.