# Can somebody explain me what are lambda things in programming?

So far I heard about :

• Lambda calculus
• Lambda programming
• Lambda expressions
• Lambda functions

Which all seems to be related to functional programming...

Apparently it will be integrated into C++1x, so I might better understand it now:

http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions

Can somebody defines briefly what are lambdas things and give an where it can be useful ?

-
you should not use wikipedia as reference. wikipedia is only for 'general reference', not as genuine information. –  RBA Dec 10 '10 at 10:39
RBA: Ah, so finally someone who shares my opinion about Wikipedia. A lot of people do take Wikipedia as the most authentic source. In fact, it has become so much to mean the Word of God. –  Kanini Dec 10 '10 at 10:41
I know, but it was a starting point. In fact one can base his knowledge on wikipedia, but finding something better than wikipedia needs either to ask or to just ask the the web... –  jokoon Dec 10 '10 at 13:08
Note that the terminology historically comes from wanting a good way to talk about the function represented by an expression. The function represented by x+1 is then written lambda x. x+1. –  kasterma Dec 10 '10 at 17:17
I think this would be more suited to StackOverflow. –  Gelatin Dec 11 '10 at 0:15

• Lambda calculus

The lambda calculus is a computation model invented by Alonzo Church in the 30s. The syntax and semantics of most functional programming languages are directly or indirectly inspired by the lambda calculus.

The lambda calculus in its most basic form has two operations: Abstraction (creating an (anonymous) function) and application (apply a function). Abstraction is performed using the λ operator, giving the lambda calculus its name.

• Lambda expressions
• Lambda functions

Anonymous functions are often called "lambdas", "lambda functions" or "lambda expressions" because, as I said above, λ was the symbol to create anonymous functions in the lambda calculus (and the word lambda is used to create anonymous functions in many lisp-based languages for the same reason).

• Lambda programming

This is not a commonly used term, but I assume it means programming using anonymous functions or programming using higher-order functions.

A bit more information about lambdas in C++0x, their motivation and how they relate to function pointers (a lot of this is probably a repeat of what you already know, but I hope it helps explain the motivation of lambdas and how they differ from function pointers):

Function pointers, which already existed in C, are quite useful to e.g. pass a comparison function to a sorting function. However there are limits to their usefulness:

For example if you want to sort a vector of vectors by the ith element of each vector (where i is a run-time parameter), you can't solve this with a function pointer. A function that compares two vectors by their ith element, would need to take three arguments (i and the two vectors), but the sorting function would need a function taking two arguments. What we'd need is a way to somehow supply the argument i to the function before passing it to the sorting function, but we can't do this with plain C functions.

To solve this, C++ introduced the concept of "function objects" or "functors". A functor is basically an object which has an operator() method. Now we can define a class CompareByIthElement, which takes the argument i as a constructor argument and then takes the two vectors to be compared as arguments to the operator() method. To sort a vector of vectors by the ith element we can now create a CompareByIthElement object with i as an argument and then pass that object to the sorting function.

Since function objects are just objects and not technically functions (even though they are meant to behave like them), you can't make a function pointer point to a function object (you can of course have a pointer to a function object, but it would have a type like CompareByIthElement* and thus not be a function pointer).

Most functions in the C++ standard library which take functions as arguments are defined using templates so that they work with function pointers as well as function objects.

Now to lambdas:

Defining a whole class to compare by the ith element is a bit verbose if you're only ever going to use it once to sort a vector. Even in the case where you only need a function pointer, defining a named function is sub-optimal if it's only used once because a) it pollutes the namespace and b) the function is usually going to be very small and there isn't really a good reason to abstract the logic into its own function (other than that you can't have function pointers without defining a function).

So to fix this lambdas were introduced. Lambdas are function objects, not function pointers. If you use a lambda literal like [x1, x2](y1,y2){bla} code is generated which basically does the following:

1. Define a class which has two member variables (x1 and x2) and an operator() with the arguments (y1 and y2) and the body bla.
2. Create an instance of the class, setting the member variables x1 and x2 to the values of the variables x1 and x2 currently in scope.

So lambdas behave like function objects, except that you can't access the class that's generated to implement a lambda in any way other than using the lambda. Consequently any function that accepts functors as arguments (basically meaning any non-C function in the standard library), will accept lambdas, but any function only accepting function pointers will not.

-
Are anonymous functions be used with function pointers ? If not what is the difference ? –  jokoon Dec 10 '10 at 13:09
@jokoon: No, anonymous functions can not be passed as parameters to functions that only take function pointers. However most functions which take functions as arguments are defined using templates so that they can take any kind of function object as argument, not just function pointer. That is to say in most places where you can use function pointers (std::sort for example), you'll be able to use anonymous functions instead. However when defining a function that should take an anonymous function as its argument, you need to either use a template or use std::function as the argument's type. –  sepp2k Dec 10 '10 at 13:24
so a function pointer cannot contain a lambda... –  jokoon Dec 10 '10 at 15:18
@jokoon: Yes, you can't have a function pointer pointing to a lambda. Please see the edit to my answer. I hope it will clear up anything that might still be unclear about how lambdas relate to function pointers. –  sepp2k Dec 10 '10 at 16:10
I'm with Michael on this. This is very comprehensive. +1 from me. –  sbi Dec 10 '10 at 19:27

Basically, lambda functions are functions you create "on the fly". In C++1x they could be used them to improve on its support for functional programming:

std::for_each( begin, end, [](int i){std::cout << i;} );

This will roughly result in code similar to this one:

struct some_functor {
void operator()(int i) {std::cout << i;}
};

std::for_each( begin, end, some_functor() );

If you need some_functor just for this one call to std::for_each(), then that lambda function has several advantages over it:

• what's done in the loop is specified right where the looping function is called
• it relieves you from writing some of the boiler-plate code
• there's no functor lying around at some namespace scope that makes everyone looking at the code wondering what it is needed for
-

A lambda function is another name for an anonymous function - essentially a function without a name.

Usually you use this in languages where you will only need to use the function once. For example instead of