Guest User

Untitled

a guest
Nov 16th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. 'use strict';
  2.  
  3. // Church logic
  4. const If = p => a => b => p(a)(b);
  5. const And = p => q => p(q)(p);
  6. const Or = p => q => p(p)(q);
  7. const Not = p => p(False)(True);
  8.  
  9. // Church numerals
  10. const show = lambda => lambda(x => x + 1)(0);
  11. const zero = f => x => x;
  12. const one = f => x => f(x);
  13. const two = f => x => f(f(x));
  14. const three = f => x => f(f(f(x)));
  15. const four = f => x => f(f(f(f(x))));
  16. const five = f => x => f(f(f(f(f(x)))));
  17.  
  18. // Simple arithmetic
  19. const succ = n => f => x => f(n(f)(x));
  20. const plus = (m, n) => f => x => m(f)(n(f)(x));
  21. const mult = (m, n) => f => x => m(n(f))(x);
  22. const pred = n => f => x => n(g => h => h(g(f)))(u => x)(u => u);
  23.  
  24. // y-combinator: canonical definition recur f = f (recur f)
  25. const recur = f => n => f(recur(f))(n);
  26. const sumTo = recur(f => n => n == 0 ? 0 : n + f(n - 1));
  27. const fact = recur(f => n => n <= 1 ? 1 : n * f(n - 1));
  28.  
  29. console.log(show(one)); // 1
Add Comment
Please, Sign In to add comment