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).
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 i
th 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 i
th 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 i
th 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 i
th 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:
- Define a class which has two member variables (
x1
and x2
) and an operator()
with the arguments (y1
and y2
) and the body bla
.
- 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.