A new module for performing tail-call elimination
Antoon Pardon
antoon.pardon at rece.vub.ac.be
Thu Jul 16 12:41:06 CEST 2015
On 07/16/2015 10:07 AM, Steven D'Aprano wrote:
> On Wednesday 15 July 2015 19:29, Antoon Pardon wrote:
>
>> Suppose I start with the following:
>>
>> def even(n):
>> True if n == 0 else odd(n - 1)
>>
>> def odd(n):
>> False if n == 0 else even(n - 1)
> Well, both of those always return None, so can be optimized to:
>
> even = odd = lambda x: None
>
> :-)
>
> Fixing the obvious mistake (failing to return anything) leads to the next
> mistake. When all you have is a hammer, everything looks like a nail.
>
> def even(n):
> return n%2 == 0
>
> def odd(n):
> return n%2 != 0
>
>
> are faster, easier to understand, and don't turn into an infinite loop if
> you pass a negative integer.
Nice of you to illustrate how being pedantic about something, can
make a response useless with regard to the intended original question.
Sure your implementation for solving this particular problem is better
if the purpose is to actually solve this problem. But it is useless as
an illustration for the question I'm asking, which was about how to
use a particular module.
> The point is, people keep insisting that there are a vast number of
> algorithms which are best expressed using recursion and which require TCO to
> be practical, and yet when asked for examples they either can't give any
> examples at all, or they give examples that are not well-suited to
> recursion. Or, at best, examples which are equally good when written either
> using recursion or iteration.
So what did you expect? That I should give a real world example here with
lots of details that would only detract from the information I'm looking
for, just so that your curiosity would be satisfied?
I'm not here to satisfy your or anyone else's curiosity. Certainly not
when that curiosity often enough is just a pretext for donning the
role of judge or arbiter and where any simplification that was done
to make the example better understandable is siezed upon as a reason
to dismiss it, because one looks at it litterally, like you do above,
and is unable or unwilling to understand the spirit of the example.
--
Antoon Pardon
More information about the Python-list
mailing list