Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #93754

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <antoon.pardon@rece.vub.ac.be>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.018
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'needed,': 0.05; 'received:134': 0.05; 'level:': 0.09; 'python': 0.10; 'stack': 0.13; 'def': 0.13; 'received:ac.be': 0.16; 'trivially': 0.16; 'wrote:': 0.16; 'instance,': 0.18; 'tree': 0.18; '2015': 0.20; 'converted': 0.22; 'am,': 0.23; 'seems': 0.23; 'implemented': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'chris': 0.26; 'function': 0.28; 'solution,': 0.29; 'tail': 0.29; 'convert': 0.29; 'subject:/': 0.30; 'code': 0.30; 'received:be': 0.30; "can't": 0.32; 'functional': 0.32; 'problem': 0.33; 'source': 0.33; 'usually': 0.33; 'true.': 0.33; 'languages': 0.34; 'ones': 0.35; 'could': 0.35; 'direction': 0.35; 'sometimes': 0.35; 'but': 0.36; 'cases': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; '12,': 0.37; 'means': 0.39; 'why': 0.39; 'easily': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'your': 0.60; 'avoid': 0.61; 'making': 0.62; 'worth': 0.67; 'jul': 0.72; 'iterative': 0.84; 'comment.': 0.91
X-IronPort-Anti-Spam-Filtered true
X-IronPort-Anti-Spam-Result Ah8HAPOmo1WGuA9G/2dsb2JhbABbhCwBJIMiwA0CggABAQEBAQGFLgEBAQMBI1UGCwsYAgIFFgsCAgkDAgECAQ82EwYCAheHfgMKCLNVkA4NSIUQASuBIokogQKCTYFhXxaCUoFDAQSUMYdHgleBZoE/hnaJE4NEg18mY4MabYEFgUYBAQE
Date Mon, 13 Jul 2015 14:00:19 +0200
From Antoon Pardon <antoon.pardon@rece.vub.ac.be>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Icedove/31.7.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)
References <CAD+URuj-4e62-9hvpiLsrBP1M6kXRyZhZ6XyL01gWWnDoShvNg@mail.gmail.com> <CAPTjJmrnd2cdA75ROZUKsJfYMdYUXkxRbEN50UE_-r0Jg=c5nQ@mail.gmail.com>
In-Reply-To <CAPTjJmrnd2cdA75ROZUKsJfYMdYUXkxRbEN50UE_-r0Jg=c5nQ@mail.gmail.com>
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.463.1436788891.3674.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1436788891 news.xs4all.nl 2850 [2001:888:2000:d::a6]:45522
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:93754

Show key headers only | View raw


On 07/13/2015 01:28 PM, Chris Angelico wrote:
> On Sun, Jul 12, 2015 at 8:20 AM, Ian Burnette <ian.burnette@gmail.com> wrote:
>> [ About tail recursion ]
>>
> When a function is purely tail-recursive like this, it's trivial to
> convert it at the source code level:
>
> def factorial(n):
>     acc = 1
>     while n > 0:
>         acc *= n
>         n -= 1
>     return acc
>
> The cases that _can't_ be converted this trivially are the ones that
> usually can't be converted automatically either - the best case I can
> think of is tree traversal, where one of the calls could be tail-call
> optimized:

This is not true. Tail recursion elimination can always converted
automatically. Otherwise other languages couldn't have implemted it.

> Why is it worth writing your code recursively, only to have it be
> implemented iteratively?

Because sometimes, it is easier to think about the problem recursively.

> Warping your code around a recursive solution
> to make it into a perfect tail call usually means making it look a lot
> less impressive; for instance, 

And sometimes your problem is very easily solved by a number of functions
that tail call each other but in python you will need to warp your code
around an iterative solution, in order to avoid the stack limit.

It seems warping your code is only seen as a problem when going in the
functional direction. Going into the iterative direction may take all
the warping that is needed, without comment.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE) Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-07-13 14:00 +0200

csiph-web