Skip to content

Instantly share code, notes, and snippets.

@sampsyo
Created February 16, 2017 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sampsyo/79c9a92473f438c99dd8c602549974ec to your computer and use it in GitHub Desktop.
Save sampsyo/79c9a92473f438c99dd8c602549974ec to your computer and use it in GitHub Desktop.
a fixed-point combinator in JavaScript
// 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