Skip to content

Instantly share code, notes, and snippets.

@foota
Created May 14, 2012 18:14
Show Gist options
  • Select an option

  • Save foota/2695450 to your computer and use it in GitHub Desktop.

Select an option

Save foota/2695450 to your computer and use it in GitHub Desktop.
Y combinator
#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;
}
@foota
Copy link
Copy Markdown
Author

foota commented May 14, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment