Skip to content

Instantly share code, notes, and snippets.

@aoi0308
Last active December 20, 2015 17:58
Show Gist options
  • Save aoi0308/6172116 to your computer and use it in GitHub Desktop.
Save aoi0308/6172116 to your computer and use it in GitHub Desktop.
JavaScriptでYコンビネータ。 fact.jsはそれを使って定義した関数。
function fact(n) {
return Y(fact0)(n);
}
document.write(fact(6));
/* こいつが Yコンビネータ */
function Y(f) {
return (function(g) {
return f(function(x) {
return g(g)(x);
});
})(function(g) {
return f(function(x) {
return g(g)(x);
});
});
}
/* Yコンビネータに対応した階乗計算関数 */
function fact0(f) {
return function(n) {
if (n == 0) return 1;
return n * f(n - 1);
}
}
/* 使ってみる */
document.write(Y(fact0)(6));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment