Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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; 'skip:p 40': 0.04; 'think,': 0.05; 'linear': 0.07; 'removes': 0.07; 'semantic': 0.07; 'terry': 0.07; 'python': 0.07; '1000.': 0.09; 'collections': 0.09; 'executed': 0.09; 'machines.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'recursion': 0.09; 'tail': 0.09; 'values,': 0.09; 'essentially': 0.12; 'subject:error': 0.12; 'def': 0.13; 'am,': 0.14; 'binary': 0.14; 'wrote:': 0.14; '*can*': 0.16; 'constants': 0.16; 'exponential': 0.16; 'impractical': 0.16; 'iteration.': 0.16; 'overflow': 0.16; 'recursive': 0.16; 'reedy': 0.16; 'case.': 0.16; 'stack': 0.16; 'written,': 0.19; 'jan': 0.22; 'saying': 0.22; 'header:In-Reply-To:1': 0.22; 'fail': 0.22; 'do,': 0.22; 'example.': 0.23; 'replacing': 0.23; 'version.': 0.23; 'example': 0.24; 'memory': 0.24; 'version': 0.25; 'cases': 0.25; 'correct': 0.26; '(as': 0.29; 'opposed': 0.29; 'skip:( 20': 0.31; 'depth': 0.31; 'does': 0.31; 'called': 0.32; 'to:addr:python- list': 0.32; 'using': 0.34; 'header:X-Complaints-To:1': 0.34; 'actually': 0.34; 'starting': 0.34; 'change': 0.34; 'there': 0.35; 'correctly': 0.35; 'print': 0.35; 'header:User-Agent:1': 0.35; 'point': 0.35; 'fails': 0.35; 'saves': 0.35; 'try:': 0.35; 'people,': 0.36; 'usually': 0.36; 'error.': 0.36; 'optimization': 0.36; 'enough': 0.37; 'two': 0.37; 'some': 0.37; 'machine': 0.37; 'case': 0.37; 'useful': 0.37; 'either': 0.37; 'removing': 0.38; 'less': 0.38; 'but': 0.38; 'received:org': 0.38; 'under': 0.39; 'to:addr:python.org': 0.39; 'where': 0.39; 'header:Mime- Version:1': 0.39; 'except': 0.39; 'sets': 0.40; 'header:Received:5': 0.40; 'might': 0.40; 'learning,': 0.60; 'best': 0.60; 'happen': 0.61; 'body': 0.62; 'and,': 0.63; 'below': 0.63; 'collection': 0.71; 'unnecessary': 0.73 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Fibonacci series recursion error Date: Sun, 01 May 2011 18:24:30 -0400 References: <87iptw1egh.fsf@rudin.co.uk> <87ei4k0ygz.fsf@rudin.co.uk> <87aaf7246f.fsf@rudin.co.uk> <4dbd221a$0$29991$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: rain.gmane.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 87 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1304288688 news.xs4all.nl 81475 [::ffff:82.94.164.166]:36414 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:4420 On 5/1/2011 5:27 AM, Hans Georg Schaathun wrote: > Of course you do, but you are still only saying that there might be > an application where this might happen because of excessive although > logically correct recursion. You have not given a single example where > it actually happened. I will. Stack overflow *can* happen with a bad base case. It *will* happen with correct linear recursion* applied to a large enough collection on a finite-memory machine (as opposed to an 'infinite' memory Turing machine). def count_popable_collection(pop_col): try: pop_col.pop() return count_popable_collection(pop_col) + 1 except (KeyError,IndexError): return 0 print(count_popable_collection({1,2,3})) print(count_popable_collection([1,2,3])) Both calls correctly print 3, but will fail for large enough sets or lists. I call the above body recursion*. A tail-recursive version def count_popable_collection2(pop_col, cnt=0): try: pop_col.pop() return count_popable_collection2(pop_col, cnt + 1) except (KeyError,IndexError): return cnt print(count_popable_collection2({1,2,3})) print(count_popable_collection2([1,2,3])) is less clear to most people, I think, and, executed as written, fails at the same point with the same memory error. Try either of the above with list(range(bignum)) (I am using 3.2). This does not make linear recursion 'bad', just impractical for general use on finite-memory machines. While I consider it very useful for learning, it is unnecessary because it is easy to write an iterative version. So called tail-recursion optimization saves memory by REMOVING RECURSION and replacing it with iteration. def count_popable_collection3(pop_col): cnt = 0 while True: try: pop_col.pop() cnt += 1 except (KeyError,IndexError): return cnt print(count_popable_collection3({1,2,3})) print(count_popable_collection3([1,2,3])) Python does not do this automatically because 1) it can be a semantic change under some circumstances; 2) one who wants the iterative version can just as easily write it directly; and 3) Python has a better way to process collections that removes essentially all the boilerplate in the recursive-call and while-loop versions: def count_popable_collection4(pop_col): cnt = 0 for item in pop_col: cnt += 1 return cnt print(count_popable_collection4({1,2,3})) print(count_popable_collection4([1,2,3])) Binary recursion* is a different case because the exponential growth in leaf number and hence time limits the useful depth of recursion to well below the default of 1000. * linear recursion: usually and at most one recursive call per call * binary recursion: usually and at most two recursive calls per call Fib is the best known example. * tail recursion: base cases return completed calculations * body recursion: base cases return starting values, often constants -- Terry Jan Reedy