Functional Programming and python
Neil Cerutti
neilc at norwich.edu
Mon Sep 30 14:36:28 EDT 2013
On 2013-09-30, Franck Ditter <nobody at nowhere.org> wrote:
> In article <ba94102b-18b6-4850-ac85-032b0fe2fa75 at googlegroups.com>,
> rusi <rustompmody at gmail.com> wrote:
>> I touched upon these in two blog-posts:
>> 1. http://blog.languager.org/2013/06/functional-programming-invades.html
>> 2. http://blog.languager.org/2012/10/functional-programming-lost-booty.html
>>
>> Also most programmers without an FP background have a poor
>> appreciation of the centrality of recursion in CS; see
>> http://blog.languager.org/2012/05/recursion-pervasive-in-cs.html
>
> Good approach of FP in Python, but two points make me crazy :
> 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.
If you've got a set of recursive functions with recursive calls
in tail-call position it's pretty easy to convert to a solution
that doesn't blow the stack. Converting to a while loop is
usually straightforward, or you try trampolining.
If the calls aren't in tail-call position then you wouldn't get
tail-call optimization from a language like scheme, either.
Getting calls in tail position is often the sticking point, and
tail-call optimization doesn't help with that.
I think the Python rationale also discusses how it would make
tracebacks harder to understand if stackframes could clobber each
other. Personally I think that's more a theoretical than a
practical problem. Languages with tail-call optimization aren't
impossible to debug.
> 2. Lambda-expression body is limited to one expression. Why ?
> Why the hell those limitations ? In this aspect, Javascript has
> a cooler approach.
It's in the Design and History FAQ.
http://docs.python.org/3/faq/design.html
Why cant lambda forms contain statements?
Python lambda forms cannot contain statements because
Pythons syntactic framework cant handle statements nested
inside expressions. However, in Python, this is not a serious
problem. Unlike lambda forms in other languages, where they
add functionality, Python lambdas are only a shorthand
notation if youre too lazy to define a function.
Functions are already first class objects in Python, and can
be declared in a local scope. Therefore the only advantage of
using a lambda form instead of a locally-defined function is
that you dont need to invent a name for the function but
thats just a local variable to which the function object
(which is exactly the same type of object that a lambda form
yields) is assigned!
What I usually end up with is a dictionary of callbacks, with the
simple functions defined in-line and the more complex functions
defined just before that and referenced instead.
--
Neil Cerutti
More information about the Python-list
mailing list