views:

1415

answers:

7

I don't really get lambda expressions. While they've been around since the days of ALGOL, I didn't start hearing about them until fairly recently, when Python and Ruby became very popular. Now that C# has the => syntax, people in my world (.NET) are talking about lamdba expressions more and more.

I've read the Wikipedia article on the lambda calculus, but I'm not really a math guy. I don't really understand it from a practical perspective. When would I use lambda expressions? Why? How would I know that it's what I should be doing?

Can you show examples of how you would solve problems with lambda expressions, in a before-and-after format? Any imperative language is fine, but C# would be easiest for me to understand.

+3  A: 

The answers to this question might be useful to you.

Chris Young
+14  A: 

Basically as far as C# is concerned, lambda expressions are an easy way to create a delegate (or an expression tree, but let's leave those aside for now).

In C# 1 we could only create delegate instances from normal methods. In C# 2 we gained anonymous methods. In C# 3 we gained lambda expressions, which are like more concise anonymous methods.

They're particularly concise when you want to express some logic which takes one value and returns a value. For instance, in the context of LINQ:

                       // Only include children - a predicate
var query = dataSource.Where(person => person.Age < 18) 
                       // Transform to sequence of names - a projection
                      .Select(person => person.Name);

There's a fuller discussion of this - along with other aspects - in my article on closures.

Jon Skeet
I saw this question and I thought, "Ooo! Maybe I can beat Jon Skeet to the punch!" But then I didn't. :) +1
Greg D
that code is looking pretty nice, even though I am pondering if Select is the right name. if you used Map, you'd arrive at relational algebra in code. :)
Tetha
To me, Map suggests the creation of a map from key to value, where you can look up by key.Personally I'd prefer Project().
Jon Skeet
There was a brief, tangentially related discussion on Lippert's blog earlier this year about reading the "=>" operator: http://blogs.msdn.com/ericlippert/archive/2008/05/16/reading-code-over-the-telephone.aspx
Greg D
A: 

My main use of lambda expressions in .NET has been when working with lists. Using a lambda expression you can build up a query on a list in a similar way as you would build an SQL statement to search a database table.

Jimmeh
+1  A: 

lambda functions are just anonymous functions.

For example, in python, you want to double all elements in a list. There are pretty much three ways to do so:

List-expressions:

[2*x for x in list]

explicit function:

def double(x):
    return 2*x
map(double, list) # iirc

with lambdas:

double = lambda x : 2*x
map(double, list)

So, a lambda in most languages is just a way to avoid the syntactic overhead of creating a new function.

Tetha
Your final comment isn't necessarily true. lambdas provide the ability to create closures over variables as well.
Nicholas Mancuso
A: 

This is one of the better explanations I've seen of how to understand the big ideas in using lamda expressions in C#:

http://www.developingfor.net/c-30/upgrade-your-c-skills-part-3-lambda-expressions.html

orlando calresian
A: 

This article should be relevant to your interests. Basic Instincts: Lambda Expressions

John Chuckran
A: 

A lambda expression is a functional value (in the same sense that 'int' and 'string' are values). This means that it is possible to

  • define a variable that holds a lambda expression (this is the same as defining a function)
  • return a lambda expression from a method or function
  • send a lambda expression as an argument to a function or method

Take a look at functions like map, filter or fold (in any functional language) to see how lambda expressions are used as arguments to other functions.

Jonas