Files
nothing/nothing.js
2018-05-30 10:23:25 +02:00

55 lines
1.8 KiB
JavaScript

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 TIMES = n => f => x => n(f)(x);
const INC = n => f => x => f(n(f)(x));
const ADD = n => m => n(INC)(m);
const MULT = n => m => n(ADD(m))(ZERO);
const POWER = n => m => n(MULT(m))(ONE);
// DEC is stolen from PRED here:
// https://en.wikipedia.org/wiki/Lambda_calculus#Arithmetic_in_lambda_calculus
const DEC = n => f => x => n(g => h => h(g(f)))(y => x)(y => y);
const SUB = n => m => m(DEC)(n);
const TRUE = x => y => x;
const FALSE = x => y => y;
const IF = b => b;
const NOT = b => IF(b)(FALSE)(TRUE);
const AND = a => b => IF(a)(IF(b)(TRUE)(FALSE))(FALSE);
const OR = a => b => IF(a)(TRUE)(IF(b)(TRUE)(FALSE));
const IS_ZERO = n => n(x => FALSE)(TRUE);
const IS_LEQ = n => m => IS_ZERO(SUBTRACT(n)(m)); // we only have positive ints
const IS_EQ = n => m => AND(IS_LEQ(n)(m))(IS_LEQ(m)(n)); // this is stupid
// time to get recursive!
const Y = f => (x => f(x(x)))(x => f(x(x))); // lazy recursion
const Z = f => (x => f(_ => x(x)(_)))(x => f(_ => x(x)(_))); // eager recursion
const FACT = Z(f => n => IF(IS_ZERO(n))(ONE)(_ => MULT(n)(f(DEC(n)))(_)));
const DIV = null;
const MOD = null;
const PAIR = null;
const LEFT = null;
const RIGHT = null;
const EMPTY = null;
const UNSHIFT = null;
const IS_EMPTY = null;
const FIRST = null;
const REST = null;
const INJECT = null;
const FOLD = null;
const MAP = null;
const RANGE = null;
const SUM = null;
const PROD = null;
const CONCAT = null;
const PUSH = null;
const REVERSE = null;
const INC_ALL = null;
const DOUBLE_ALL = null;
const TO_DIGITS = null;
const TO_STRING = null;
const FIZZBUZZ = null;
module.exports = {
ZERO, ONE, TWO, THREE, TIMES, INC, ADD, MULT, POWER, DEC, SUB, TRUE,
FALSE, IF, NOT, AND, OR, IS_ZERO, IS_LEQ, IS_EQ, IS_LEQ, Y, Z, FACT
};