Last active
December 20, 2015 17:58
-
-
Save aoi0308/6172116 to your computer and use it in GitHub Desktop.
JavaScriptでYコンビネータ。
fact.jsはそれを使って定義した関数。
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
function fact(n) { | |
return Y(fact0)(n); | |
} | |
document.write(fact(6)); |
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
/* こいつが 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