17

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

Oliver Moran
  • 281
  • 1
  • 2
  • 5
  • 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
  • 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 '11 at 14:39
  • @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 '11 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. :-) – 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
  • @Paul Nathan - it's exactly that kind of use of the term that I had heard recently! (But not put to rap obviously!!) – Oliver Moran Apr 12 '11 at 15:47
  • 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 '11 at 15:50
  • possible duplicate of [Can somebody explain me what are lambda things in programming ?](http://programmers.stackexchange.com/questions/25131/can-somebody-explain-me-what-are-lambda-things-in-programming) –  Apr 12 '11 at 22:59
  • Duplicate on sister forum: https://stackoverflow.com/questions/16501/what-is-a-lambda-function – Martin Maat Sep 02 '19 at 19:30

2 Answers2

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.

Timothy Groote
  • 645
  • 3
  • 9
  • 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 '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
  • 4
    Give me an example of a lambda expression that is not a function, please. – Ingo Apr 12 '11 at 12:28
  • @Ingo, what you're referring to is a lambda abstraction. But lambda expression (or lambda term) can be an abstraction, an application or a variable. E.g, `\f x . f x` is a function, `f x` is an application and `x` is a variable, whereas all are lambda terms. – SK-logic Apr 12 '11 at 12:42
  • Whence is this terminology from? You don't call 1+x a "literal expression" just because it **contains** a literal, do you? I am not nitpicking, just want to understand what is the reason to name it so - it would suffice to have \p.x introduced as form of an expression that we call lambda expression (or abstraction, but when we look at it from the syntactic side, expression is ok), where p is the syntactic form for naming the paramter and x is an expression. – Ingo Apr 12 '11 at 12:51
  • 1
    @Ingo, this is a terminology of lambda calculus, of course. http://en.wikipedia.org/wiki/Lambda_calculus#Lambda_terms – SK-logic Apr 12 '11 at 12:54
  • Oh well, I see. Your mileage may vary, but I find it not very helpful to take this terminlogy over to languages where not every expression is by definition a lambda expression. (And certainly, the OP did not ask for code in the lambda calculus.) – Ingo Apr 12 '11 at 12:58
  • Because it's easy to get lost in the scope of defining 'lambda', the initial answer i gave was an easily comprehendable one, which is commonly seen, with an example in code to clarify. I suppose it is a matter of interpreting the question. Did moran ask "what is the exact terminology of a lambda expression in the broadest sense of programming" ? Please also note this question originated from StackOverflow, which is where i started this answer. – Timothy Groote Apr 12 '11 at 13:33
  • @Ingo, it is important to understand lambda calculus prior to trying to take it into the "real" programming languages. Otherwise you'll end up thinking that it is all about "passing short anonymous functions as arguments", and will miss a whole load of useful stuff (currying, combinators, alpha- and eta- transforms, closures, free vs. bound variables, etc.) – SK-logic Apr 12 '11 at 14:09
  • @SK-logic: I couldn't agree more. But as it stands, now the OP might say "This is lambda code." when he sees a variable in a C# program, say, because you told him, that a variable is a lambda expression. That's what I wanted to clarify. Hence: in lambda calculus, there are 3 possible expressions: lambda abstraction, application and variable. – Ingo Apr 12 '11 at 14:39
  • @Ingo, the interesting part is that you can treat a subset of C# as pure lambda calculus. In this sense, C# function calls and variables are also lambda expressions. – SK-logic Apr 12 '11 at 14:45
  • 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
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.

Adam Crossland
  • 9,688
  • 2
  • 35
  • 46