Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- // Church logic
- const If = p => a => b => p(a)(b);
- const And = p => q => p(q)(p);
- const Or = p => q => p(p)(q);
- const Not = p => p(False)(True);
- // Church numerals
- const show = lambda => lambda(x => x + 1)(0);
- const zero = f => x => x;
- const one = f => x => f(x);
- const two = f => x => f(f(x));
- const three = f => x => f(f(f(x)));
- const four = f => x => f(f(f(f(x))));
- const five = f => x => f(f(f(f(f(x)))));
- // Simple arithmetic
- const succ = n => f => x => f(n(f)(x));
- const plus = (m, n) => f => x => m(f)(n(f)(x));
- const mult = (m, n) => f => x => m(n(f))(x);
- const pred = n => f => x => n(g => h => h(g(f)))(u => x)(u => u);
- // y-combinator: canonical definition recur f = f (recur f)
- const recur = f => n => f(recur(f))(n);
- const sumTo = recur(f => n => n == 0 ? 0 : n + f(n - 1));
- const fact = recur(f => n => n <= 1 ? 1 : n * f(n - 1));
- console.log(show(one)); // 1
Add Comment
Please, Sign In to add comment