Guest User

Untitled

a guest
Jul 16th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. // Y-combinator for the A type -> B type
  5. template <typename A, typename B>
  6. std::function<B(A)> y(std::function<B(std::function<B(A)>, A)> f) {
  7. return std::bind(f,std::bind(&y<A,B>,f), std::placeholders::_1);
  8. }
  9.  
  10. int fact(std::function<int(int)> f, int v) {
  11. if(v == 0)
  12. return 1;
  13. else
  14. return v * f(v -1);
  15. }
  16.  
  17. int main(int argc, const char** argv) {
  18. auto factorial = y<int,int>(fact);
  19. std::cout << factorial(5) << std::endl;
  20. return 0;
  21. }
Add Comment
Please, Sign In to add comment