|
Revision 292, 420 bytes
(checked in by JarrettBillingsley, 7 months ago)
|
Closes #65. Also fixed a bug in the regexp.email predefined regexp, and a small bug in dumpVal.
|
| Line | |
|---|
| 1 |
module factorial |
|---|
| 2 |
|
|---|
| 3 |
// See if you can figure out what this means. I sure as hell can't. |
|---|
| 4 |
function Y(g) = (function a(f) = f(f))(function(f) = g(function(x) = f(f)(x))) |
|---|
| 5 |
|
|---|
| 6 |
// factorial without recursion |
|---|
| 7 |
function F(f) = function(n) = n == 0 ? 1 : n * f(n - 1) |
|---|
| 8 |
|
|---|
| 9 |
global factorial = Y(F) // factorial is the fixed point of F |
|---|
| 10 |
|
|---|
| 11 |
function main() |
|---|
| 12 |
{ |
|---|
| 13 |
// now test it |
|---|
| 14 |
for(i: 1 .. 10) |
|---|
| 15 |
writefln(i, "! = ", factorial(i)) |
|---|
| 16 |
} |
|---|