Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have recently heard people talk about code being "lambda". I have never heard of this phrase before. What does it mean?

share|improve this question

migrated from stackoverflow.com Apr 12 '11 at 12:08

This question came from our site for professional and enthusiast programmers.

    
It did. Several times. I even tried it a few times - but sadly with no success. Maybe you'll have more luck? Thanks to those who repsonded. –  Oliver Moran Apr 12 '11 at 12:25
9  
@Oliver, part of asking a question here is to also show what have you done to answer that question so that people aren't trying the same things you did and get stuck. By specifying where in the Wikipedia or Google links you found you were confused this ensures your question gets better answered as at a general level the answer may not be what you wanted since it is just referencing what you didn't get previously. Just consider trying what someone else is posting and see what happens. You may be surprised at what results. –  JB King Apr 12 '11 at 14:39
6  
I sense a rap could be made out of this - "my code so lambda". But I can't rhythm it worth anything. :-) –  Paul Nathan Apr 12 '11 at 15:39
1  
@JB King + JB King - Thank you for your courteous comments. In broad terms, what you both say is sensible. However, depending on the query, sometimes a brief and open question is better at eliciting the kinds of replies that are not found elsewhere. My question elicited precisely the kind of answers that I was looking for. I phrased it so that it would. I hope the answers below are useful to others with the same question. It's ironic that I am satisfied with the answers at the same time that some are disappointed with the question. –  Oliver Moran Apr 12 '11 at 15:43
1  
@Paul Nathan - you're code so lambda you clearly never planned ta work to any standard; it's rougher than a sander! –  glenatron Apr 12 '11 at 15:50
show 3 more comments

4 Answers

up vote 8 down vote accepted

I mentally filed it under: a function you can pass on to another function as argument.

The scientific answer: http://en.wikipedia.org/wiki/Lambda_calculus

share|improve this answer
    
I'm going to tick this as my "accepted answer". It's probably not as accurate as others (which I have up voted also) but it is the answer that I'll use to remember what the phrase means. –  Oliver Moran Apr 12 '11 at 12:29
19  
Only that its not quite correct. This is like to say: An integer literal is an integer value that you can pass to functions. Yet, one may also pass other interger values to functions, so it's no distinctive feature of literals. I think the shortest answer is: a lambda expression is a function literal. –  Ingo Apr 12 '11 at 12:36
    
@Ingo: I think calling it a function literal might be the clearest simple answer I've seen to explaining lambdas in a practical sense. –  Cercerilla Apr 12 '11 at 14:26
    
After my chat with SK-logic, I'd say now it would be more exact to say a lambda abstraction is a function literal. –  Ingo Apr 12 '11 at 14:44
    
At least you should mention the "function" here is not the function in C, which is what most programmers would think when newly coming to this topic. –  Codism Apr 12 '11 at 15:44
add comment

Lambda expressions are either an abstraction (sometimes referred to as anonymous function), an application or a variable (most languages also add constants to this list). Lambda terms are not necessarily functions, and not necessarily passed as parameters, though this is a common practice.

A common example of lambda expressions in C#

For example:

List<int> items = new List<int>();
items.add(1);
items.add(2);
items.add(1);
items.add(3);

int CountofOnes = items.FindAll(item => item == 1).Count();

Console.Out.WriteLine(CountofOnes);

will output: 2

In this code, I pass a lambda construction to the FindAll function of .NET's List object.

items.FindAll(item => item == 1)

The lambda in this call executes a simple equation and returns a boolean, telling FindAll what to do.

share|improve this answer
1  
That's not quite true. Lambda expression is either an abstraction (not necessarily small), an application or a variable (most languages also add constants to this list). Lambda terms are not necessarily functions, and not necessarily passed as parameters. –  SK-logic Apr 12 '11 at 12:14
    
You're right, i will ammend the answer for the sake of clarity. –  Timothy Groote Apr 12 '11 at 12:17
3  
Give me an example of a lambda expression that is not a function, please. –  Ingo Apr 12 '11 at 12:28
1  
@Ingo, this is a terminology of lambda calculus, of course. en.wikipedia.org/wiki/Lambda_calculus#Lambda_terms –  SK-logic Apr 12 '11 at 12:54
1  
@SK-logic I prefer to see it differently. Languages like Haskell and C# make it possible to bind functions to names in a let (rec) construct or in the form of "super-combinators" (i.e. top level function bindings), and this I see as such a grave difference that I do not feel it is right to apply the original lambda terminology here. Because no such thing is possible in LC (thats precisely why you need an Y combinator for recursion). The result of ((\x y -> x) a) and (const a) in Haskell is the same and both are applications, but I would only the first term as "lambda application". –  Ingo Apr 12 '11 at 14:57
show 7 more comments

Lambda usually refers to a function expression in a functional programming context.

This is a lambda expression in python:

lambda x: x + 1

Represents a function that increments its parameter x by 1.

share|improve this answer
add comment

Anonymous (unnamed) function or an object that is usually inlined as an argument to another function.

Hence, the namespace is less polluted.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.