Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'else:': 0.03; 'decent': 0.07; 'dictionary': 0.07; 'infinite': 0.07; 'python': 0.07; 'deliberately': 0.09; 'less,': 0.09; 'recursion': 0.09; 'stack,': 0.09; 'stack.': 0.09; 'structure.': 0.09; 'pm,': 0.11; 'subject:error': 0.12; 'say,': 0.14; 'wrote:': 0.14; 'cyclic': 0.16; 'elements,': 0.16; 'iteration,': 0.16; 'partly': 0.16; 'php:': 0.16; 'recursive': 0.16; '\xa0in': 0.16; "wouldn't": 0.18; "hasn't": 0.19; 'temporary': 0.19; 'true,': 0.19; 'work,': 0.20; 'language': 0.20; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'mon,': 0.22; 'calls.': 0.23; 'example': 0.24; 'parameters': 0.26; 'chris': 0.27; 'object': 0.27; 'function': 0.27; 'message- id:@mail.gmail.com': 0.28; 'definition': 0.29; 'variables': 0.29; 'state,': 0.31; 'to:addr:python-list': 0.32; 'fairly': 0.33; '(for': 0.33; 'actually': 0.34; 'change': 0.34; 'describe': 0.35; 'normally': 0.35; 'doing': 0.36; 'problem.': 0.36; 'data': 0.37; 'some': 0.37; 'received:209.85': 0.37; 'element': 0.38; 'references': 0.38; 'skip:\xa0 30': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'to:addr:python.org': 0.39; 'could': 0.39; 'received:209': 0.39; 'similar': 0.40; 'map': 0.40; 'would': 0.40; "it's": 0.40; 'header:Received:5': 0.40; 'give': 0.61; 'order': 0.61; 'further': 0.62; '2011': 0.62; 'nothing.': 0.68; 'designed': 0.69; 'anything.': 0.72; 'soon': 0.76; 'introduce': 0.78; "'em": 0.84; 'difference.': 0.84; 'namely': 0.84; 'received:209.85.210.174': 0.84; 'received:mail- iy0-f174.google.com': 0.84; 'yourself:': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=PMJTIgiKEtD/h+utDxHhk2gRZQlv+HaSp2xKcgx+KYM=; b=Vxh0UhiXj6w/Qb08zovQyTyg1NgM0LHAGe72xv1/7ECqd4HlD4vxjOr3LTihO+0Cnm 7dEHlTfB7bi9C3CfZHh3cVwin26hk7KkAiqzhlqrPgaiLHBLmQRoJkD8vOuPmVI+EjBp DyxcoHJ2p6h6UFRcpcPyiFwz+zTS7HjHJG4fo= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=cUCOi86gBDMlZ31p2W5FJ807dYGw82ytwYpznJKBsnSwUFCSK1UkywiT3Jfyowj6RL yiilK0A4aPen7Onv6FqzEDpjLVaoemvVfsRwued0Q0yI9H09EEzjI6159Dux4rs1umTa GIaJ2Yj2DFu103bUwhT7bNEcAWGJw+oRAj9to= MIME-Version: 1.0 In-Reply-To: <6fs198-8lf.ln1@svn.schaathun.net> References: <87iptw1egh.fsf@rudin.co.uk> <87ei4k0ygz.fsf@rudin.co.uk> <87aaf7246f.fsf@rudin.co.uk> <4dbd221a$0$29991$c3e8da3$5496439d@news.astraweb.com> <4dbd4a88$0$29991$c3e8da3$5496439d@news.astraweb.com> <6fs198-8lf.ln1@svn.schaathun.net> Date: Mon, 2 May 2011 16:28:45 +1000 Subject: Re: Fibonacci series recursion error From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 54 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1304317729 news.xs4all.nl 81481 [::ffff:82.94.164.166]:47143 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:4441 On Mon, May 2, 2011 at 3:36 PM, Hans Georg Schaathun wro= te: > On Mon, 2 May 2011 06:49:41 +1000, Chris Angelico > =A0 wrote: > : =A0Sure. Serialize this Python object in a way that can be given to, sa= y, PHP: > : =A0foo=3D{"asdf":"qwer","zxcv":"1234"}; foo["self"]=3D[1,2,3,foo] > > Wouldn't cyclic references give infinite recursion? =A0And remain > infinitive if you recode it iteratively? Well, I don't know of a decent non-recursive way to process a recursive structure. Incidentally, this example is almost directly from some working code of ours; I have a C function that recurses over a Python dictionary and aborts as soon as it's found "too much" data (for a fairly arbitrary definition of "too much", but one that's deliberately designed to prevent infinite recursion). Since every element in the dictionary can be a list/dict, and every element of those can be too, there's no non-recursive way to do it, other than by doing the recursion yourself: # partly pseudocode searchme=3D[foo] while len(searchme): cur=3Dget_next_elem(searchme[-1]) if cur=3D=3Dend_of_list: searchme[-1:]=3D[] else: if can_be_recursed_into(cur): searchme.append(cur) else: output(cur) This would work, more or less, but it merely trades "true" recursion for the searchme[] stack. Yes, it's iterative. No, it hasn't abolished recursion. > True. =A0There is a place for everything. =A0However, in the example > above, you can map the recursive definition directly into python > without further ado. =A0In order to express the one-liner in python, > as iteration, you need to introduce additional elements, namely > a state (index variable). =A0Hence, recursion is clearer by being > close to the language you would normally use to describe the > problem. True, and you could abolish a lot of temporary variables by turning them into parameters to recursive calls. But you've abolished nothing. The recursive factorial is very similar to: reduce(`*,range(1,n+1)) The iterative is very similar to: reduce(`*,xrange(1,n+1)) One of 'em stacks up all the numbers and the other gets 'em as it needs 'em. Fundamentally, there's really not a lot of difference. By piling up the numbers on your stack, you just change the format of your saved state, you don't actually save anything. Chris Angelico