gecko-dev/js/ref/ycomb.js
fur 6b433caaaa (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
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))