[Tutor] Why lambda could be considered evil

alan.gauld@bt.com alan.gauld@bt.com
Tue, 27 Aug 2002 11:59:43 +0100


> > But what -is- the problem that some people have with lambda?  =
> ... In the case of lambda, it is a Lisp construct 

Actually Lambda is not a Lisp construct but a part 
of the lambda calculus which is a branch of math.
The use of lambda is common in several functional 
languages.

My beef with lambda in Python is that it is only 
partially implemented so that it really is only a thin
bit of syntactic sugar around a def statement. 
Useful for creating lots of similar but not quite 
identical functions programatically but not for much 
else.

However understanding lamda expressions is one of 
those "change your life" type discoveries in programming. 
Once you get them the whole theoretocal basis of programming
kind of pops into focus - or it did for me!

> seems as foreign to Python as an steele [sic] ball shoved up 
> a unicorn's nostril. =

Not at all. Python supports many forms of programming.
The class statement is part of OO and not a rip off 
from C++, similarly lambda is part of functional 
programming. If you want to support a functional style 
as well as an OOP style

> 1. It creates a complexity hot spot. Some people seem to be 
> unable to resist the temptation to be very, very clever 

I agree this is bad but we are seeing the same kind of 
overcomplexity in List Comprehensions. Avoidable complexity
is nearl always a bad thing regardless of the language 
construct, but I agree lambdas do seem to bring out 
the worst in some folks! :-)

> 2. It is hard for newbies to understand why it is needed at 
> all. "Lambda is an expression, not a statement" is fine for computer 
> scientists who spend

Fair point, but for those who do need it its useful.
Of course it would help if lambdas were fully implemented
to allow multi line 'arbitrary' blocks of code rather 
than just single line expressions....

> 3. The name is just plain stupid. ....
> be intuitive if you've spent the last 20 years programming Lisp, 

Or studying lambda calculus!

> it a mnenomic name or use a generic keyword like "function" 
> instead of

If you don't like lambda you can just use def.
In python the two are syntactically equivalent!

def f(x): return expression
f = lambda x: expression

are the same thing.

> clean, honest, and readable def instead of shoving everything 
> in a lambda expression 

No reason except that lambda is the more conventional and 
therefore readable form to anyone trainied in Functional 
Programming. Why use a tuple when you could use a list instead?
(OK Tuples have some slightly different features - immutability...)

> (except in Tkinter, which was ripped off from Tcl 

Tcl doesn't have lambdas per se (although it does have 
arbitrary code blocks). You never need lambdas in Tkinter, 
they are just a convenience.

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld