Created
January 8, 2020 06:02
-
-
Save JustinSDK/23f135ced38809b719ba772ad54fa423 to your computer and use it in GitHub Desktop.
return recursive lambda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <functional> | |
using namespace std; | |
function<int(int)> foo() { | |
return [](int n) -> int { | |
function<int(int)> fac = [&](int n) -> int { | |
return n == 0 ? 1 : n * fac(n-1); | |
}; | |
return fac(n); | |
}; | |
} | |
int main(int, char*[]) { | |
cout << foo()(4) << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment