Functional Programming and python

Chris Angelico rosuav at gmail.com
Mon Sep 30 13:17:45 EDT 2013


On Tue, Oct 1, 2013 at 3:04 AM, Franck Ditter <nobody at nowhere.org> wrote:
> 1. Tail recursion is not optimized. We are in 2013, why ? This is known technology (since 1960).
> And don't answer with "good programmers don't use recursion", this is bullshit.

I've yet to see any value in having the compiler/interpreter optimize
tail recursion that can't be achieved just as well, and usually more
clearly, with simple iteration. Recursion's important; tail
recursion's important; tail recursion optimization isn't. (But I do
sometimes yearn for a goto.)

> 2. Lambda-expression body is limited to one expression. Why ?
> Why the hell those limitations ? In this aspect, Javascript has a cooler approach.

Since you can just use def in the middle of another function, the
difference between def and lambda isn't all that huge. Yes, def isn't
an expression - but Python's lambda gives an incredibly compact
notation for the common case. Compare these two snippets:

# Python
odd_numbers = filter(lambda x: x%2, numbers)

//Pike
odd_numbers = filter(numbers, lambda(int x) {return x%2;})

Aside from the type declaration and the argument order, these two
snippets are doing exactly the same thing in exactly the same way.
Python's version is way more compact. Pike's version lets you put
multiple statements into the expression... but most of the time you
don't need that. Both approaches have their value.

ChrisA



More information about the Python-list mailing list