Last active
October 20, 2021 17:18
-
-
Save keller/741c97ae789323d0cb721e5a5d14253c to your computer and use it in GitHub Desktop.
Fizz Buzz in lambda calculus and JS. Also version in Racket: https://gist.github.com/keller/8aa87814c120e725f9ed6c223642e981
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
const ZERO = f => a => a; | |
const ONE = f => a => f(a); | |
const TWO = f => a => f(f(a)); | |
const THREE = f => a => f(f(f(a))); | |
const FIVE = f => a => f(f(f(f(f(a))))); | |
const SUCC = n => f => a => f(n(f)(a)); | |
const MULT = x => y => f => x(y(f)); | |
const TEN = MULT(FIVE)(TWO); | |
const FIFTEEN = MULT(FIVE)(THREE); | |
const ONE_HUNDRED = MULT(TEN)(TEN); | |
const TRUE = x => y => x; | |
const FALSE = x => y => y; | |
const PAIR = x => y => f => f(x)(y); | |
const CAR = p => p(x => y => x); | |
const CDR = p => p(x => y => y); | |
const PHI = p => PAIR(CDR(p))(SUCC(CDR(p))); | |
const PRED = n => CAR(n(PHI)(PAIR(ZERO)(ZERO))); | |
const SUB = x => y => y(PRED)(x); | |
const IS_ZERO = n => n(x => FALSE)(TRUE); | |
const LEQ = x => y => IS_ZERO(SUB(x)(y)); | |
const Y = f => (x => f(y => x(x)(y)))(x => f(y => x(x)(y))); | |
const MOD = Y(f => x => y => LEQ(y)(x)(z => f(SUB(x)(y))(y)(z))(x)); | |
ONE_HUNDRED(x => | |
FALSE( | |
IS_ZERO(MOD(x)(FIFTEEN))(FIZZBUZZ)( | |
IS_ZERO(MOD(x)(THREE))(FIZZ)( | |
IS_ZERO(MOD(x)(FIVE))(BUZZ)(y => PRINT_NUM(x)) | |
) | |
)(x) | |
)(SUCC(x)) | |
)(ONE); | |
// --- JS helpers. not strict lambda calc below | |
function PRINT_NUM(n) { | |
console.log(n(x => x + 1)(0)); | |
} | |
function FIZZBUZZ() { | |
console.log("FIZZBUZZ"); | |
} | |
function FIZZ() { | |
console.log("FIZZ"); | |
} | |
function BUZZ() { | |
console.log("BUZZ"); | |
} |
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
(p => n => i => u => f => | |
(x => y => z => x(y(z)))( | |
(x => y => z => x(y(z)))(x => y => x(x(x(x(x(y))))))(x => y => x(x(y))) | |
)( | |
(x => y => z => x(y(z)))(x => y => x(x(x(x(x(y))))))(x => y => x(x(y))) | |
)(x => | |
(x => y => y)( | |
(x => x(x => x => y => y)(x => y => x))( | |
(x => (y => x(z => y(y)(z)))(y => x(z => y(y)(z))))(w => x => y => | |
(x => y => | |
(x => x(x => x => y => y)(x => y => x))( | |
(x => y => | |
y(x => | |
(x => x(x => y => x))( | |
x(x => | |
(x => y => z => z(x)(y))((x => x(x => y => y))(x))( | |
(x => y => z => y(x(y)(z)))((x => x(x => y => y))(x)) | |
) | |
)((x => y => z => z(x)(y))(x => y => y)(x => y => y)) | |
) | |
)(x))(x)(y) | |
))(y)(x)(z => | |
w( | |
(x => y => | |
y(x => | |
(x => x(x => y => x))( | |
x(x => | |
(x => y => z => z(x)(y))((x => x(x => y => y))(x))( | |
(x => y => z => y(x(y)(z)))((x => x(x => y => y))(x)) | |
) | |
)((x => y => z => z(x)(y))(x => y => y)(x => y => y)) | |
) | |
)(x))(x)(y) | |
)(y)(z) | |
)(x) | |
)(x)( | |
(x => y => z => x(y(z)))(x => y => x(x(x(x(x(y))))))(x => y => | |
x(x(x(y))) | |
) | |
) | |
)(x => p(f))( | |
(x => x(x => x => y => y)(x => y => x))( | |
(x => (y => x(z => y(y)(z)))(y => x(z => y(y)(z))))(w => x => y => | |
(x => y => | |
(x => x(x => x => y => y)(x => y => x))( | |
(x => y => | |
y(x => | |
(x => x(x => y => x))( | |
x(x => | |
(x => y => z => z(x)(y))((x => x(x => y => y))(x))( | |
(x => y => z => y(x(y)(z)))((x => x(x => y => y))(x)) | |
) | |
)((x => y => z => z(x)(y))(x => y => y)(x => y => y)) | |
) | |
)(x))(x)(y) | |
))(y)(x)(z => | |
w( | |
(x => y => | |
y(x => | |
(x => x(x => y => x))( | |
x(x => | |
(x => y => z => z(x)(y))((x => x(x => y => y))(x))( | |
(x => y => z => y(x(y)(z)))((x => x(x => y => y))(x)) | |
) | |
)((x => y => z => z(x)(y))(x => y => y)(x => y => y)) | |
) | |
)(x))(x)(y) | |
)(y)(z) | |
)(x) | |
)(x)(x => y => x(x(x(y)))) | |
)(x => p(i))( | |
(x => x(x => x => y => y)(x => y => x))( | |
(x => (y => x(z => y(y)(z)))(y => x(z => y(y)(z))))(w => x => y => | |
(x => y => | |
(x => x(x => x => y => y)(x => y => x))( | |
(x => y => | |
y(x => | |
(x => x(x => y => x))( | |
x(x => | |
(x => y => z => z(x)(y))((x => x(x => y => y))(x))( | |
(x => y => z => y(x(y)(z)))( | |
(x => x(x => y => y))(x) | |
) | |
) | |
)((x => y => z => z(x)(y))(x => y => y)(x => y => y)) | |
) | |
)(x))(x)(y) | |
))(y)(x)(z => | |
w( | |
(x => y => | |
y(x => | |
(x => x(x => y => x))( | |
x(x => | |
(x => y => z => z(x)(y))((x => x(x => y => y))(x))( | |
(x => y => z => y(x(y)(z)))( | |
(x => x(x => y => y))(x) | |
) | |
) | |
)((x => y => z => z(x)(y))(x => y => y)(x => y => y)) | |
) | |
)(x))(x)(y) | |
)(y)(z) | |
)(x) | |
)(x)(x => y => x(x(x(x(x(y)))))) | |
)(x => p(u))(y => p(n(x))) | |
) | |
)(x) | |
)((x => y => z => y(x(y)(z)))(x)) | |
)(x => y => x(y)))(console.log)(n => n(x => x + 1)(0))("FIZZ")("BUZZ")( | |
"FIZZBUZZ" | |
); |
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
(p=>n=>i=>u=>f=>(x=>y=>z=>x(y(z)))((x=>y=>z=>x(y(z)))(x=>y=>x(x(x(x(x(y))))))(x=>y=>x(x(y))))((x=>y=>z=>x(y(z)))(x=>y=>x(x(x(x(x(y))))))(x=>y=>x(x(y))))(x=>(x=>y=>y)((x=>x(x=>x=>y=>y)(x=>y=>x))((x=>(y=>x(z=>y(y)(z)))(y=>x(z=>y(y)(z))))(w=>x=>y=>(x=>y=>(x=>x(x=>x=>y=>y)(x=>y=>x))((x=>y=>y(x=>(x=>x(x=>y=>x))(x(x=>(x=>y=>z=>z(x)(y))((x=>x(x=>y=>y))(x))((x=>y=>z=>y(x(y)(z)))((x=>x(x=>y=>y))(x))))((x=>y=>z=>z(x)(y))(x=>y=>y)(x=>y=>y))))(x))(x)(y)))(y)(x)(z=>w((x=>y=>y(x=>(x=>x(x=>y=>x))(x(x=>(x=>y=>z=>z(x)(y))((x=>x(x=>y=>y))(x))((x=>y=>z=>y(x(y)(z)))((x=>x(x=>y=>y))(x))))((x=>y=>z=>z(x)(y))(x=>y=>y)(x=>y=>y))))(x))(x)(y))(y)(z))(x))(x)((x=>y=>z=>x(y(z)))(x=>y=>x(x(x(x(x(y))))))(x=>y=>x(x(x(y))))))(x=>p(f))((x=>x(x=>x=>y=>y)(x=>y=>x))((x=>(y=>x(z=>y(y)(z)))(y=>x(z=>y(y)(z))))(w=>x=>y=>(x=>y=>(x=>x(x=>x=>y=>y)(x=>y=>x))((x=>y=>y(x=>(x=>x(x=>y=>x))(x(x=>(x=>y=>z=>z(x)(y))((x=>x(x=>y=>y))(x))((x=>y=>z=>y(x(y)(z)))((x=>x(x=>y=>y))(x))))((x=>y=>z=>z(x)(y))(x=>y=>y)(x=>y=>y))))(x))(x)(y)))(y)(x)(z=>w((x=>y=>y(x=>(x=>x(x=>y=>x))(x(x=>(x=>y=>z=>z(x)(y))((x=>x(x=>y=>y))(x))((x=>y=>z=>y(x(y)(z)))((x=>x(x=>y=>y))(x))))((x=>y=>z=>z(x)(y))(x=>y=>y)(x=>y=>y))))(x))(x)(y))(y)(z))(x))(x)(x=>y=>x(x(x(y)))))(x=>p(i))((x=>x(x=>x=>y=>y)(x=>y=>x))((x=>(y=>x(z=>y(y)(z)))(y=>x(z=>y(y)(z))))(w=>x=>y=>(x=>y=>(x=>x(x=>x=>y=>y)(x=>y=>x))((x=>y=>y(x=>(x=>x(x=>y=>x))(x(x=>(x=>y=>z=>z(x)(y))((x=>x(x=>y=>y))(x))((x=>y=>z=>y(x(y)(z)))((x=>x(x=>y=>y))(x))))((x=>y=>z=>z(x)(y))(x=>y=>y)(x=>y=>y))))(x))(x)(y)))(y)(x)(z=>w((x=>y=>y(x=>(x=>x(x=>y=>x))(x(x=>(x=>y=>z=>z(x)(y))((x=>x(x=>y=>y))(x))((x=>y=>z=>y(x(y)(z)))((x=>x(x=>y=>y))(x))))((x=>y=>z=>z(x)(y))(x=>y=>y)(x=>y=>y))))(x))(x)(y))(y)(z))(x))(x)(x=>y=>x(x(x(x(x(y)))))))(x=>p(u))(y=>p(n(x)))))(x))((x=>y=>z=>y(x(y)(z)))(x)))(x=>y=>x(y)))(console.log)(n=>n(x=>x+1)(0))("FIZZ")("BUZZ")("FIZZBUZZ"); |
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
curl -s https://gist.githubusercontent.com/keller/741c97ae789323d0cb721e5a5d14253c/raw/fizzbuzz_min.js | node - |
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
deno run https://gist.githubusercontent.com/keller/741c97ae789323d0cb721e5a5d14253c/raw/fizzbuzz_min.js |
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
docker run --rm node:alpine sh -c "wget -q -O- https://gist.githubusercontent.com/keller/741c97ae789323d0cb721e5a5d14253c/raw/fizzbuzz_min.js | node" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment