Created
January 22, 2016 15:23
-
-
Save ivan-demchenko/ab62c81cf2844fe989c1 to your computer and use it in GitHub Desktop.
This file contains 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
// This is the implmenetation of some of the lamdas from lamda calculus | |
// https://en.wikipedia.org/wiki/Lambda_calculus | |
console.clear(); | |
const log = function(x) { | |
console.log('log: ' + x); | |
return x; | |
} | |
// Boolean: | |
const T = t => f => t | |
const F = t => f => f | |
const IF = P => x => y => P(x)(y) | |
const OR = x => y => IF(T)(x)(y) | |
const AND = x => y => IF(F)(x)(y) | |
// Church numbers | |
const SUCC = n => f => x => f(n(f)(x)) | |
const _0 = f => x => x | |
const _1 = SUCC(_0) // f(x) === f(_0) | |
const _2 = SUCC(_1) // f(f(x)) === f(_1) | |
const _3 = SUCC(_2) // f(f(f(x))) === f(_2) | |
const _4 = SUCC(_3) // ... | |
const _5 = SUCC(_4) | |
const _6 = SUCC(_5) | |
_5(log)('123123') | |
const IsZero = n => n(x=>F)(T) | |
console.log( 'IsZero: ' + IsZero(_0)(1)(0) ); | |
console.log( 'IF.IsZero: ' + IF(IsZero(_0))(1)(0) ); | |
// Data structures | |
const PAIR = x => y => f => f(x)(y) | |
const NIL = x => T | |
const FST = p => p(x => y => x) | |
const SND = p => p(x => y => y) | |
const NULL = p => p( x => y => F ) | |
let pair = PAIR(1)(2); | |
console.log( 'FST: ' + FST(pair) ); | |
console.log( 'SND: ' + SND(pair) ); | |
console.log( NULL(NIL)('Y')('N') ); | |
let list = PAIR(3)(PAIR(2)(PAIR(1)(NIL))) | |
console.log( NULL(list)('Y')('N') ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment