Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'else:': 0.03; 'assignment': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'assume': 0.11; 'jan': 0.11; 'index': 0.13; 'stack': 0.13; 'def': 0.13; '(assuming': 0.16; '(ie,': 0.16; 'expression.': 0.16; 'feasible': 0.16; 'pivot': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wrote:': 0.16; 'element': 0.18; 'example.': 0.18; 'programmer': 0.18; 'explicit': 0.22; 'parameter': 0.22; 'am,': 0.23; 'comment:': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.24; 'paul': 0.24; 'sort': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'followed': 0.27; 'least': 0.27; 'start,': 0.27; 'function': 0.28; 'idea': 0.28; 'this.': 0.28; 'actual': 0.28; 'depth': 0.29; 'tail': 0.29; 'array': 0.29; 'subject:/': 0.30; 'work.': 0.30; 'branch': 0.30; 'minimal': 0.30; 'functional': 0.32; 'call,': 0.33; 'optimize': 0.33; 'programming,': 0.33; 'gets': 0.35; 'step': 0.36; 'should': 0.36; 'instead': 0.36; 'needed': 0.36; 'there': 0.36; 'depends': 0.36; 'smaller': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'doing': 0.38; 'version': 0.38; 'someone': 0.38; 'thank': 0.38; 'end': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'ever': 0.60; 'body': 0.61; 'above,': 0.63; 'potentially': 0.67; "'if": 0.84; "'while'": 0.84; 'calls,': 0.84; 'received:fios.verizon.net': 0.91; 'sensibly': 0.91; 'forever.': 0.93; 'hand,': 0.97 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Possibly Pythonic Tail Call Optimization (TCO/TRE) Date: Tue, 14 Jul 2015 20:23:27 -0400 References: <55A3A853.4040006@rece.vub.ac.be> <55A3C366.6060602@rece.vub.ac.be> <87fv4r1fre.fsf@jester.gateway.sonic.net> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-98-114-97-173.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 In-Reply-To: <87fv4r1fre.fsf@jester.gateway.sonic.net> 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: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1436919834 news.xs4all.nl 2914 [2001:888:2000:d::a6]:38597 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!news.stben.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:93838 On 7/14/2015 1:15 AM, Paul Rubin wrote: > def quicksort(array, start, end): > midp = partition(array, start, end) > if midp <= (start+end)//2: > quicksort(array, start, midp) > quicksort(array, midp+1, end) > else: > quicksort(array, midp+1, end) > quicksort(array, start, midp) > > I assume you know how quicksort and its partition step work. The idea > is you partition the array around the pivot element (midp is the index > of that element), then recursively sort the two partitions, doing the > smaller partition as a recursive call and the larger one as a tail call, > so that you use O(log n) stack depth instead of potentially O(n). Thank you for the example. It I have ever seen how to minimize the max stack needed for first calls, I have forgotten. First some comment: 1. There is no terminal condition, so the above will loop forever. The body should start with 'if not terminal(start, end):' where terminal is the actual expression. I did not write it because it depends on whether 'end' is the highest index or one past it. 2. There are no tail calls (call followed by return), so a tail-call optimizer will not optimize this. A recur() function might be able to. 3. Mutation is anathema to functional programming, so a functional programmer would never write and version of this, at least not without holding his nose. The tail-like calls in each branch can be avoided with assignment and gobacks. In Python, we go-back with while loops. (IE, 'while' = 'label' + 'if' + 'goto'.) With minimal change to the above, we get (untested) def quicksort(array, start, end): while not terminal(start, end): midp = partition(array, start, end) if midp <= (start+end) // 2: quicksort(array, start, midp) start = midp+1 else: quicksort(array, midp+1, end) end = midp I can understand that someone might prefer to break the symmetry of the paired calls by wrapping the second with recur() (assuming that such a function is sensibly feasible on *all* implementations). In the other hand, I prefer the reduced-noise explicit assignment that highlights the one parameter that gets rebound. -- Terry Jan Reedy