Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'modified': 0.05; 'assignment': 0.07; 'variable,': 0.07; 'dict': 0.09; 'skip:= 70': 0.10; '-o2': 0.16; 'foo()': 0.16; 'wrote:': 0.16; 'char': 0.18; "shouldn't": 0.18; 'programmer': 0.18; '>>>': 0.20; '2015': 0.20; 'controlling': 0.22; 'gcc': 0.22; 'am,': 0.23; 'header:In-Reply- To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; '14,': 0.27; 'function': 0.28; 'away.': 0.29; 'if,': 0.29; 'skip:i 60': 0.29; 'tail': 0.29; 'subject:/': 0.30; 'code': 0.30; "can't": 0.32; 'controls': 0.33; 'int': 0.33; 'though.': 0.33; 'tue,': 0.34; 'received:google.com': 0.35; 'done': 0.35; 'instead': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'itself': 0.38; 'to:addr:python.org': 0.40; "you'll": 0.61; 'jul': 0.72; 'to:name:python': 0.84; 'url:wh': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=erntWNkSotrbzIxz9PQfpYQ2KMo1OFTf3MOU0KQf1GQ=; b=l0gWU8ttnr0YqId26jldInI2HKunk5Ad+PmgDio3T231ix9SYA9hh+hvaUokEptczh ysZ5FZkyIFUNCB2S3F0r4GeebOs47dH0x2R6DzB78g3clGJMuIfOi4lQpXgyCml14MAy caJP1dBambO1wKxh+EBJHMvUUufsFKybUCfQAK2GL/swgYRaijK1Rp4oVkCC1oOs7CpM kywdFPYK1p7kKaR395AOfjogl/kxboKxzrGb71EE0Owqe920G+nXPYF1gHDuQrwyQdwM 04J06pp3Kb7wjY2+8VQhLDKMl00MYx0Xjyu1kZG8pr2yODhstlMfMhJYb5iM2lkUHtjh 8qZQ== X-Received: by 10.129.88.86 with SMTP id m83mr4469282ywb.147.1436970551266; Wed, 15 Jul 2015 07:29:11 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <87d1zuejk2.fsf@elektro.pacujo.net> References: <55A3A853.4040006@rece.vub.ac.be> <55A3C366.6060602@rece.vub.ac.be> <87fv4r1fre.fsf@jester.gateway.sonic.net> <877fq3nuwo.fsf@elektro.pacujo.net> <87pp3vm93f.fsf@elektro.pacujo.net> <87d1zuejk2.fsf@elektro.pacujo.net> From: Ian Kelly Date: Wed, 15 Jul 2015 08:28:31 -0600 Subject: Re: Possibly Pythonic Tail Call Optimization (TCO/TRE) To: Python 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: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1436970560 news.xs4all.nl 2836 [2001:888:2000:d::a6]:50241 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:93873 On Tue, Jul 14, 2015 at 11:27 AM, Marko Rauhamaa wrote: > Ian Kelly : > >> On Tue, Jul 14, 2015 at 2:33 AM, Marko Rauhamaa wrote: >>> The programmer shouldn't be controlling tail call optimizations. >> >> The programmer controls it regardless. >> >> return foo() # TCO >> >> result = foo() # No TCO >> return result # No TCO > > Not necessarily. Compile this function with gcc ("gcc -S -O2 atoi.c"): > > ======================================================================== > int atoi(const char *str, int n) > { > if (str && *str) > n = atoi(str + 1, n * 10 + *str - '0'); > return n; > } > ======================================================================== > (Example modified from ich-if-any-c-compilers-do-tail-recursion-optimization#220660>.) > > You'll see that the generated code is tail-call-optimized. This can't be done generally, though. What if, instead of a local variable, the assignment were to a dict item? Even if the dict itself is a local variable, the assignment can't be optimized away.