Mozilla/mozilla/js/ref/ycomb.js
fur dffbf084ac (This code is not built by any flavor of Navigator)
Initial check-in to mozilla tree: JSRef development is migrating from
JSFUN13_BRANCH of /m/src repository to /m/pub


git-svn-id: svn://10.0.0.236/trunk@582 18797224-902f-48f8-a5cc-f745e15eee43
1998-04-24 01:35:13 +00:00

22 lines
370 B
JavaScript

// The Y combinator, applied to the factorial function
function factwrap(proc) {
function factproc(n) {
if (n <= 1) return 1
return n * proc(n-1)
}
return factproc
}
function Y(outer) {
function inner(proc) {
function apply(arg) {
return proc(proc)(arg)
}
return outer(apply)
}
return inner(inner)
}
print("5! is " + Y(factwrap)(5))