Created
May 14, 2012 18:14
-
-
Save foota/2695450 to your computer and use it in GitHub Desktop.
Y combinator
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; | |
| // Y-combinator for the int type | |
| function<int(int)> y(function<int(function<int(int)>, int)> f) | |
| { return bind(f, bind(&y, f), placeholders::_1); } | |
| int main() | |
| { | |
| // Y-combinator compatible factorial | |
| auto fact = [](function<int(int)> f, int v){ return v == 0 ? 1 : v * f(v - 1); }; | |
| auto factorial = y(fact); | |
| cout << factorial(5) << endl; | |
| return 0; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://handasse.blogspot.com/2010/05/c0xy.html