Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed8.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '16,': 0.03; 'elif': 0.04; 'indicating': 0.05; 'none:': 0.05; 'assignment': 0.07; 'true)': 0.07; 'ugly': 0.07; 'cc:addr:python-list': 0.09; '(none,': 0.09; 'false)': 0.09; 'subject:module': 0.09; 'stack': 0.13; 'def': 0.13; 'thu,': 0.15; 'algorithmic': 0.16; 'fn)': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'trivially': 0.16; 'wrote:': 0.16; 'example.': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'machine': 0.21; '%s"': 0.22; 'header:In-Reply-To:1': 0.24; 'message- id:@mail.gmail.com': 0.27; 'change,': 0.27; 'tail': 0.29; 'raise': 0.29; 'code': 0.30; "can't": 0.32; 'point': 0.33; 'though.': 0.33; 'received:google.com': 0.35; 'next': 0.35; 'could': 0.35; 'replace': 0.35; 'pm,': 0.36; 'subject:: ': 0.37; 'turned': 0.38; 'anything': 0.38; 'subject:-': 0.39; 'rather': 0.39; 'easy': 0.60; 'collection': 0.60; 'maximum': 0.61; 'course': 0.62; 'here': 0.66; 'jul': 0.72; 'chrisa': 0.84; 'improved.': 0.84; 'odd,': 0.84; 'pardon': 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=tDkinIulLQXxmIjeKqWaQRMtzU9UQfM7a2D887gq89U=; b=v2rIJQSgkm5t3DWVdCG9D0PvqromOg6CQRKZIJH5CUV5VZZdNVNzKSIvFHHhnc7DZA 9gcdmMhgEy/V43r/Sfpz63dCK6Dlwjj4Ja6HBRnnNQniUlzOhYS4UdxUjV3kdkAsarPK 7mR3Oxv2w0LRBv1470hdIBaFXVxyb1VaAUHECjaXHfKAA+rXbt9hI7aEwXWXPOOAdxF/ YyVXmlgbxqZBPCOxhwnnIi3NMdX/6jhf/rooPYJ9ovFsGxnyAFjHi5ShjGO7STIQJbz1 fhul2keBluTgCiAyeTsYzX/E0aYSjktUIRXUcVsuJETnGbkJsNZAxJ7wkLKXsnTDpuG2 6HZg== MIME-Version: 1.0 X-Received: by 10.107.4.1 with SMTP id 1mr10825798ioe.10.1437054479916; Thu, 16 Jul 2015 06:47:59 -0700 (PDT) In-Reply-To: <55A7B309.8080903@rece.vub.ac.be> References: <55a3dcd9$0$3024$426a34cc@news.free.fr> <55a76628$0$2846$c3e8da3$76491128@news.astraweb.com> <55A78A42.4090506@rece.vub.ac.be> <55A7B309.8080903@rece.vub.ac.be> Date: Thu, 16 Jul 2015 23:47:59 +1000 Subject: Re: A new module for performing tail-call elimination From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1437054482 news.xs4all.nl 2930 [2001:888:2000:d::a6]:33150 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:93927 On Thu, Jul 16, 2015 at 11:35 PM, Antoon Pardon wrote: > 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 > > Any collection of functions that tail calls each other can rather > trivially be turned into a state machine like the above. You can > just paste in the code of the individual functions and replace > the return call combo's with an assignment indicating which 'function' > is to be 'called' next and its arguments. That's not an algorithmic change, though. That's just a mechanical change, and a poorly-written one. My point was that I have yet to see anything that demands TCO and can't be algorithmically improved. The best so far has been a quicksort that uses TCO to guarantee a maximum on stack usage. ChrisA