Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #4420
| 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 | <python-python-list@m.gmane.org> |
| 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 <tjreedy@udel.edu> |
| Subject | Re: Fibonacci series recursion error |
| Date | Sun, 01 May 2011 18:24:30 -0400 |
| References | <a78a0ffd-03cc-4540-addb-b8e206d165d5@n2g2000prj.googlegroups.com> <KtMup.29280$8U5.7734@newsfe20.iad> <87iptw1egh.fsf@rudin.co.uk> <up6t88-4p7.ln1@svn.schaathun.net> <87ei4k0ygz.fsf@rudin.co.uk> <mvet88-018.ln1@svn.schaathun.net> <87aaf7246f.fsf@rudin.co.uk> <sjhv88-r7a.ln1@svn.schaathun.net> <4dbd221a$0$29991$c3e8da3$5496439d@news.astraweb.com> <iklv88-nba.ln1@svn.schaathun.net> |
| 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 | <iklv88-nba.ln1@svn.schaathun.net> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1048.1304288688.9059.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar
Re: Fibonacci series recursion error Paul Rudin <paul.nospam@rudin.co.uk> - 2011-04-30 15:40 +0100
Re: Fibonacci series recursion error Hans Georg Schaathun <hg@schaathun.net> - 2011-05-01 09:18 +0100
Re: Fibonacci series recursion error Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-01 09:04 +0000
Re: Fibonacci series recursion error Hans Georg Schaathun <hg@schaathun.net> - 2011-05-01 10:27 +0100
Re: Fibonacci series recursion error Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-01 11:56 +0000
Re: Fibonacci series recursion error Hans Georg Schaathun <hg@schaathun.net> - 2011-05-01 14:15 +0100
Re: Fibonacci series recursion error Chris Angelico <rosuav@gmail.com> - 2011-05-02 06:49 +1000
Re: Fibonacci series recursion error Hans Georg Schaathun <hg@schaathun.net> - 2011-05-02 06:36 +0100
Re: Fibonacci series recursion error Chris Angelico <rosuav@gmail.com> - 2011-05-02 16:28 +1000
Re: Fibonacci series recursion error Chris Angelico <rosuav@gmail.com> - 2011-05-02 16:30 +1000
Re: Fibonacci series recursion error (slightly OT) Dave Angel <davea@ieee.org> - 2011-05-02 08:50 -0400
Re: Fibonacci series recursion error Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-02 01:09 +0000
Re: Fibonacci series recursion error Hans Georg Schaathun <hg@schaathun.net> - 2011-05-02 10:41 +0100
Re: Fibonacci series recursion error Terry Reedy <tjreedy@udel.edu> - 2011-05-01 18:24 -0400
Re: Fibonacci series recursion error Hans Georg Schaathun <hg@schaathun.net> - 2011-05-02 14:14 +0100
Re: Fibonacci series recursion error Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-02 16:41 +0000
Re: Fibonacci series recursion error Hans Georg Schaathun <hg@schaathun.net> - 2011-05-02 21:02 +0100
Re: Fibonacci series recursion error Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-03 00:21 +0000
Re: Fibonacci series recursion error rusi <rustompmody@gmail.com> - 2011-05-02 20:42 -0700
Re: Fibonacci series recursion error Hans Georg Schaathun <hg@schaathun.net> - 2011-05-03 06:13 +0100
Re: Fibonacci series recursion error Terry Reedy <tjreedy@udel.edu> - 2011-05-02 14:56 -0400
Re: Fibonacci series recursion error Chris Angelico <rosuav@gmail.com> - 2011-05-03 07:56 +1000
Re: Fibonacci series recursion error Hans Georg Schaathun <hg@schaathun.net> - 2011-05-03 08:01 +0100
csiph-web