Created
June 16, 2016 09:02
-
-
Save Mizzlr/51d92f3ec1e4298c33e7e02dff85ae38 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var PLUS = function (n) { | |
return function(m) { | |
return function(f) { | |
return function(z) { | |
return n(f)(m(f)(z)); | |
}; | |
}; | |
}; | |
}; | |
var MULT = function (n) { | |
return function(m) { | |
return function(f) { | |
return function(z) { | |
return n(m(f))(z); | |
}; | |
}; | |
}; | |
}; | |
var PRED = function(n) { | |
return function(f) { | |
return function(z) { | |
return (n(function(g) { | |
return function(h) { | |
return h(g(f)); | |
}}) (function(u) {return z;})) | |
(function(u) {return u;}); | |
}; | |
}; | |
}; | |
var SUB = function(m){ | |
return function(n) { | |
return m(PRED)(n); | |
} | |
} | |
function createChurchNumeral(n) { | |
f = ZERO | |
// apply SUCC repeatedly to ZERO n times | |
for (var i=0; i<n; i++) | |
f = SUCC(f); | |
return f; // f is function | |
}; | |
// example | |
console.log(numerify(createChurchNumeral(5))); // should print 5 | |
console.log(numerify(SUCC(ZERO))); // should print 1 | |
// FOUR is that function which applies incr 4 times to 0 | |
FOUR = createChurchNumeral(4); | |
SIX = createChurchNumeral(6); | |
console.log(numerify(PLUS(FOUR)(SIX))); // prints 10 | |
console.log(numerify(PRED(creatChurchNumeral(2)))); // prints 1 | |
console.log(numerify(PRED(ZERO))); // prints 0 | |
console.log(numerify(SUB(FOUR)(SIX))); // print 0 | |
console.log(numerify(MULT(FOUR)(SIX))); // print 24 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment