[Tutor] Lambdas

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Dec 31 13:51:50 EST 2003



On Tue, 30 Dec 2003, Conrad Koziol wrote:

> Okay well I guess I chose I bad example for lambdas, but i cant find any
> good tutorials on them on the web. So I guess a better question is.
> Where is an in depth tutorial on lambdas. Even though there not all that
> necessary


Hi Conrad,

A tongue-in-cheek response to this might be something like:

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

*grin*


No, actually, there are a few articles by David Mertz that you might like:

    http://gnosis.cx/publish/tech_index_cp.html

Mertz writes extensively on functional programming in Python, and anything
that has the word 'functional programming' there on that page will most
likely have a lambda.


But lambda's shouldn't cause too many problems:  they're just expressions
that evaluate to functions.  We're basically doing something like 'lambda'
(plus a little more) whenever we define a function:

###
>>> def square(x):
...     return x * x
...
>>> square
<function square at 0x604b0>
###


Here, square is just a variable name for a function value.  And values in
Python can be passed around, just like numbers and strings:

###
>>> def compose(f, g, x):
...     """Returns f(g(x))."""
...     return f(g(x))
...
>>> def double(x):
...     return x * 2
...
>>> compose(double, square, 42)
3528
###


Now, we can get a handle on the same kind of value through 'lambda':

###
>>> square
<function square at 0x604b0>
>>>
>>> lambda x: x * x
<function <lambda> at 0x60ab0>
###


And this value behaves the same way as 'square':

###
>>> square(4)
16
>>>
>>> (lambda x: x * x)(4)
16
>>>
>>> compose(lambda x: 2*x,
...         lambda x: x * x,
...         42)
3528
###

So whenever we're defining a function with 'def', we are actually doing
something like a 'lambda': we are creating a function value, but we're
also binding that function value to a name.  When we use 'lambda'
directly, we don't have to bind that value to a name.


So 'lambda' is simply a way of creating functions that we can pass around,
without having to store them immediately as variable names.  If it helps,
think of the distinction between naming a value with a variable and using
it:

###
>>> x = 17
>>> square(x)
289
###

as opposed to using that value 'on the fly':

###
>>> 17
17
>>> square(17)
289
###


Python's version of 'lambda' is actually designed to be a lot weaker than
the 'lambda' in most 'functional' programming languages.  The rationale, I
think, is that if we're going to be playing with functions, it'll be good
to give them explicit names for human maintainability.  The language
itself encourages us to use 'def' instead of 'lambda' to force us to name
the majority of our functions.


If you really want to play around with the full power of lambdas, perhaps
the "tongue-in-cheek" reference at the very top won't be so
tongue-in-cheek for you.  *grin* It sounds like you're interested, so you
may want to play with the Scheme language if you have some time ---
'lambda' is more powerful in that language.


Good luck to you!




More information about the Tutor mailing list