16

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

9
  • 10
    @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, 2011 at 14:39
  • 1
    @Oliver Moran: We don't know you. We don't know what you tried. We don't know much about your background, either. We could repeat all the things you already saw in a hopeless guessing game. Or. You could provide us the useless, confusing and worthless things you saw so we could understand your background.
    – S.Lott
    Apr 12, 2011 at 14:56
  • 7
    I sense a rap could be made out of this - "my code so lambda". But I can't rhythm it worth anything. :-) Apr 12, 2011 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. Apr 12, 2011 at 15:43
  • 2
    @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, 2011 at 15:50

2 Answers 2

21

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.

12
  • 2
    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, 2011 at 12:14
  • You're right, i will ammend the answer for the sake of clarity. Apr 12, 2011 at 12:17
  • 4
    Give me an example of a lambda expression that is not a function, please.
    – Ingo
    Apr 12, 2011 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, 2011 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, 2011 at 14:57
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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