Created
February 16, 2017 02:49
-
-
Save sampsyo/79c9a92473f438c99dd8c602549974ec to your computer and use it in GitHub Desktop.
a fixed-point combinator in JavaScript
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
// Z = \t. (\f. t (\z. f f z)) (\f. t (\z. f f z)) | |
function Z(t) { | |
return ( (f) => t((z) => (f(f))(z)) ) ( (f) => t((z) => (f(f))(z)) ); | |
} | |
function fact_template(f) { | |
return function(n) { | |
if (n <= 1) { | |
return 1; | |
} else { | |
return n * f(n - 1); | |
} | |
} | |
} | |
var fact = Z(fact_template); | |
console.log(fact(3)); | |
console.log(fact(5)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment