Skip to content

Instantly share code, notes, and snippets.

@gofer
Last active February 17, 2018 08:58
Show Gist options
  • Save gofer/fca5be591792e919aca0a29878380996 to your computer and use it in GitHub Desktop.
Save gofer/fca5be591792e919aca0a29878380996 to your computer and use it in GitHub Desktop.
JavaScriptで(型無し)ラムダ計算
const I = (x) => x;
const K = (x) => (y) => x;
const S = (x) => (y) => (z) => x (z) (y (z));
const SII = (x) => S (I) (I) (x);
const KSII = (x) => K (SII) (x);
const Y = (x) => S (KSII) (S (S (K (S)) (K)) (KSII)) (x);
const T = K;
const F = K (I);
const NOT = (x) => S (S (I) (K (F))) (K (T)) (x);
T ('then') ('else'); // 'then'
F ('then') ('else'); // 'else'
(NOT (F)) ('then') ('else'); // 'then'
(NOT (T)) ('then') ('else'); // 'else'
const OR = (x) => (y) => S (I) (K (T)) (x) (y);
(OR (F) (F)) ('then') ('else'); // 'else'
(OR (T) (F)) ('then') ('else'); // 'then'
(OR (F) (T)) ('then') ('else'); // 'then'
(OR (T) (T)) ('then') ('else'); // 'then'
const AND = (x) => (y) => S (S) (K (K (F))) (x) (y);
(AND (F) (F)) ('then') ('else'); // 'else'
(AND (T) (F)) ('then') ('else'); // 'else'
(AND (F) (T)) ('then') ('else'); // 'else'
(AND (T) (T)) ('then') ('else'); // 'then'
const Zero = (f) => (x) => x;
const Succ = (n) => (f) => (x) => f (n (f) (x));
const One = Succ (Zero);
const Two = Succ (One);
const Three = Succ (Two);
Zero ((n) => n + 1) (0); // 0
One ((n) => n + 1) (0); // 1
Two ((n) => n + 1) (0); // 2
Three ((n) => n + 1) (0); // 3
const Add = (x) => (y) => (p) => (q) => x (p) (y (p) (q));
const Five = Add (Two) (Three);
Five ((n) => n + 1) (0); // 5
const Multiply = (x) => (y) => (z) => x (y (z));
const Four = Multiply (Two) (Two);
Four ((n) => n + 1) (0); // 4
const Exponentiation = (x) => (y) => y (x);
const Eight = Exponentiation (Two) (Three);
Eight ((n) => n + 1) (0); // 8
const Pred = (n) => (f) => (x) => n ((p) => (q) => q (p (f))) ((a) => x) (I);
const Seven = Pred (Eight);
Seven ((n) => n + 1) (0); // 7
const Subtract = (x) => (y) => y (Pred) (x);
const Six = Subtract (Eight) (Two);
Six ((n) => n + 1) (0); // 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment