Lambda expressions only exist for convenience.
Typically, you need to write predicates (boolean functions) to handle STL algorithms. For example, to count a vector of int to see how many items are less than ten, you would normally have to write a separate piece of code to do it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
bool less_than_10( const int& value )
{
return value < 10;
}
int main()
{
vector <int> v;
for (int n = 0; n < 20; n++) v.push_back( n );
cout << "There are "
<< count_if( v.begin(), v.end(), less_than_10 )
<< " ints less than 10\n";
return 0;
}
|
That little function is somewhat of an eyesore, particularly if it is only used in one unique case. The STL recognizes that certain expressions (like
x < 10
) are convenient cases for, in essence, a form of lambda expression:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector <int> v;
for (int n = 0; n < 20; n++) v.push_back( n );
cout << "There are "
<< count_if( v.begin(), v.end(), bind2nd( less <int> (), 10 ) )
<< " ints less than 10\n";
return 0;
}
|
Such 'lambdas' are really just another version of the first problem (implemented as
functors -- function-like classes), but they are useful anyway.
However, what if you really wanted to write an actual little function to be used? A true lambda allows you that power. Again, as
helios indicated, a quick perusal of the
Wikipedia Lambda Calculus and Programming Languages page might prove interesting:
http://en.wikipedia.org/wiki/Lambda_calculus#Lambda_calculus_and_programming_languages
You can also look into some functional languages to help out. For example, in
Scheme (see
http://plt-scheme.org/ ) you can create a function on the fly:
1 2 3 4 5 6 7 8 9 10 11
|
(display "There are ")
(display
(length ; returns the number of items in a list -- same as STL count()
(filter ; removes items that don't satisfy the predicate
(lambda x (x < 10)) ; this is the predicate, written as a lambda
'(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19) ; the list, of course
)
)
)
(display " ints less than 10" )
(newline)
|
That there on line 5 is a function created on the fly. It has no name (making it
anonymous) and it is right there with the code that uses it. Alternately, you would have had to give the function a name and used the function name, just as we did above in the first C++ example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
(define less_than_10
(lambda x
(x < 10)
)
)
(display
(length
(filter
less_than_10 ; this is the name of the existing predicate function to use
'(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19)
)
)
)
(display " ints less than 10" )
(newline)
|
C++ really is an
iterative language, not
functional (like Scheme), making
lambdas like these something of a bunch of magic tricks with expressions. It really isn't possible to get true lambdas without embedding some sort of C/C++ interpreter in your program.
However, I hope this helps to understand the purpose and convenience of 'lambdas'.