initial WIP
This commit is contained in:
4
README.md
Normal file
4
README.md
Normal file
@@ -0,0 +1,4 @@
|
||||
# nothing
|
||||
|
||||
A WIP implementation of Tom Stuart’s [Programming with Nothing](https://www.youtube.com/watch?v=VUhlNx_-wYk)
|
||||
challenge in ES6. I got to factorial, and will try to do the whole thing soon.
|
4
nothing-helper.js
Normal file
4
nothing-helper.js
Normal file
@@ -0,0 +1,4 @@
|
||||
module.exports = {
|
||||
to_int: (f) => f(n => n+1)(0),
|
||||
to_boolean: (f) => f(true)(false),
|
||||
};
|
54
nothing.js
Normal file
54
nothing.js
Normal file
@@ -0,0 +1,54 @@
|
||||
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
|
||||
};
|
Reference in New Issue
Block a user