Skip to content

Instantly share code, notes, and snippets.

View Mizzlr's full-sized avatar

Mushtaque Ahamed A Mizzlr

View GitHub Profile
function numerify(churchNumeral) {
return churchNumeral(function (x) {
return x + 1;
})(0); // f is a function that increments the value by 1; z=0
}
// example
numerify(ZERO) // returns 0
var ONE = SUCC(ZERO)
numerify(ONE) // returns 1
var ZERO = function (f) {
return function(z) {
return z;
};
};
var SUCC = function (n) {
return function (f) {
return function (z) {
return f(n(f)(z));
# The above functions in python
NULL = lambda x: x
TRUE = lambda onTrue: lambda onFalse: onTrue
FALSE = lambda onTrue: lambda onFalse: onFalse
IF = lambda test: lambda onTrue: lambda onFalse: test(onTrue)(onFalse)
AND = lambda bool1: lambda bool2: bool1(bool2)(bool1)
OR = lambda bool1: lambda bool2: bool1(bool1)(bool2)
NOT = lambda booly: booly(FALSE)(TRUE)
# Very readable and terse indeed!
var AND = function(bool1){
return function(bool2){
bool1(bool2)(bool1);
};
};
// consider what happens for all combination of bool1 and bool2
// bool1(bool2)(bool1) = AND(bool1)(bool2)
// TRUE(FALSE)(TRUE) = FALSE ; TRUE returns the first argument
// TRUE(TRUE)(TRUE) = TRUE
// FALSE(TRUE)(FALSE) = FALSE ; FALSE returns the second argument
function boolify (churchBoolean) {
return churchBoolean (function (_) { return true; })
( function (_) { return false; });
// function had to have one input argument
// because that is what is expected by TRUE or FALSE
// example
boolify(TRUE) // returns true
boolify(FALSE) // return false
var IF = function (test) {
return function (onTrue) {
return function (onFalse) {
return test(onTrue)(onFalse);
};
};
};
// example use
IF(TRUE)(trueBlock)(falseBlock);
// onTrue and onFalse are also some function
var TRUE = function (onTrue) {
return function (onFalse) {
return onTrue(VOID);
};
};
var FALSE = function (onTrue) {
return function (onFalse) {
return onFalse(VOID);
function curry(f) {
return function (x) {
return function (y) {
return f(x,y);
};
};
};
function uncurry(f) {
return function (x,y) {
var f1 = function(a,b){
// do something with a and b
return a + b;
};
var f2 = function(a){
// can do something with a here
return function(b){
// can do something with a and b here
return a + b;
};
@Mizzlr
Mizzlr / NULL.js
Created June 16, 2016 08:41
Church Encoding of Null
var NULL = function(x) { return x;}