[Tutor] Lambda

Sean 'Shaleh' Perry shalehperry@home.com
Fri, 20 Jul 2001 11:29:33 -0700 (PDT)


On 20-Jul-2001 Tobin, Mark wrote:
> Is it fair to say that Lambda is just a shorthand for defining a function?
> I really haven't figured it out, and don't know where one might use it...
> anybody willing to write a paragraph on Lambdas or point me to an
> explanation?
> 

yes, lamda just defines a function.  lambda is a big thing in the functional
programming world.  Languages like lisp practically require it.  But you are
wondering "how does this affect me?".

lambda lets you write code quickly, then change it later.

For instance: map(lambda x: x + x, range(0, 10)) outputs [0, 2, 4, 6, 8, 10,
12, 14, 16, 18].  Sure there are a few ways you could implement this.  You
could do:

def x2(x):
  return x + x

map(x2, range(0,10))

However, when you typed in the call to map you said "oh, i need a function
here" and had to go write one.  lambda lets you just do it, and make it
pretty/efficient/whatever later.  Plus who wants to declare silly functions
like x2?