Lecture 3 - Slide 2 : 27
Lambda expressions

C++11 supports lambda expressions, as known from functional programming languages

  auto f1 = [](int x)->int{return x + 1;};   // a function that returns x + 1. 
  auto f2 = [m](int x){return x + m;};       // can read m from local context.
  auto f3 = [=](int x){return x + m + n;};   // can read all local variables from local context.
  auto f4 = [&m](){++m;};                    // access to m by reference - can write.
  auto f5 = [&](){++m; n--;};                // access to all variables in local context by ref.
  auto f6 = [&]{++m;};                       // the empty parameter list is implicit
forms.cc
The examples from above in context.
more-forms.cc
More forms - mutable and explicit return type.
lambda1.cc
An immediate call of a lambda expression.
lambda2.cc
Lambda expressions and recursive calls.
lambda3.cc
A function that returns a function - closure - problems.
lambda3.scm
Same in Scheme - where this kind of stuff works out of the box.
lambda4.cc
A function that returns a function - OK.
Go to exercise
Get some experience with lambda expressions in C++