Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <functional>
- // Y-combinator for the A type -> B type
- template <typename A, typename B>
- std::function<B(A)> y(std::function<B(std::function<B(A)>, A)> f) {
- return std::bind(f,std::bind(&y<A,B>,f), std::placeholders::_1);
- }
- int fact(std::function<int(int)> f, int v) {
- if(v == 0)
- return 1;
- else
- return v * f(v -1);
- }
- int main(int argc, const char** argv) {
- auto factorial = y<int,int>(fact);
- std::cout << factorial(5) << std::endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment