[Tutor] Why lambda could be considered evil

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Tue, 27 Aug 2002 10:55:43 -0700 (PDT)


On Tue, 27 Aug 2002, Erik Price wrote:

>
> On Tuesday, August 27, 2002, at 06:59  AM, alan.gauld@bt.com wrote:
>
> > 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!
>
> That's the thing -- I'm not there yet.  I had a 24-hour sense of clarity
> and a "change your life" feeling when I finally understood the
> fundamental concept of OO programming (why was that so hard to
> understand at one time? in retrospect it's so simple), perhaps someday
> I'll have the same experience with lambda.  But until the lightbulb goes
> off in my head, I'm willing to hear any arguments for or against.


If you have some free time, I'd recommend a book called "Structure and
Interpretation of Computer Programs".  It has a big lambda symbol on the
front cover --- surely, with it, we may be able to elucidate this mystery!
*grin*

    http://mitpress.mit.edu/sicp/full-text/book/book.html

Lambda isn't quite effective in Python, but because it's much more
powerful in the Scheme Lisp language, you may find it easier to pick up
the concept in that language.  The book is freely available online, which
is very convenient.



I think of 'lambda' as a constructor of functions --- in languages that
fully support lambda functions, we can take advantage of lambda and
variable name binding to create what we think of as a function.


As a concrete example, let's take a simple function definition:

###
>>> def square(x):
...     return x * x
...
###


What's weird about this simple snippet is that it's doing two separate
things at once:  it's creating this sort of function object that we can
look at:

###
>>> square
<function square at 0x80e2dcc>
###

and it's also "binding" it to a name.



That is, just as we bind variables like numbers and strings by doing
things like arithmetic:

###
>>> gold_ratio = (1+5**0.5)/2
>>> gold_ratio
1.6180339887498949
>>> name = "Erik " + "Price"
>>> name
'Erik Price'
###

the idea behind lambda is that functions themselves are values that are
constructed, and that can be handled just like numbers and strings:

###
>>> some_function = lambda (x): x * x
>>> some_function
<function <lambda> at 0x8115fac>
###



I think that's one of the key ideas behind lambda --- functions aren't any
more "special" than other objects in the language: they just have
different properties.  And what makes a function really different is that
it can be called:

###
>>> some_function(8)
64
###

and that's basically it.


In a language that fully supports lambda, there's no need for a separate
"function definition" syntax, because all we'd need to define functions is
variable assignment and that lambda function constructor.



I hope that made some sort of sense.  *grin*  Good luck!