Y combinator in JS.
git-svn-id: svn://10.0.0.236/trunk@180903 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
parent
424dfdaf82
commit
04c23d8fe6
19
mozilla/js/src/Y.js
Normal file
19
mozilla/js/src/Y.js
Normal file
@ -0,0 +1,19 @@
|
||||
// The Y combinator, applied to the factorial function
|
||||
|
||||
function factorial(proc) {
|
||||
return function (n) {
|
||||
return (n <= 1) ? 1 : n * proc(n-1);
|
||||
}
|
||||
}
|
||||
|
||||
function Y(outer) {
|
||||
function inner(proc) {
|
||||
function apply(arg) {
|
||||
return proc(proc)(arg);
|
||||
}
|
||||
return outer(apply);
|
||||
}
|
||||
return inner(inner);
|
||||
}
|
||||
|
||||
print("5! is " + Y(factorial)(5));
|
||||
Loading…
x
Reference in New Issue
Block a user