Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
7
Task/Factorial/Dylan/factorial-1.dylan
Normal file
7
Task/Factorial/Dylan/factorial-1.dylan
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
define method factorial (n)
|
||||
if (n < 1)
|
||||
error("invalid argument");
|
||||
else
|
||||
reduce1(\*, range(from: 1, to: n))
|
||||
end
|
||||
end method;
|
||||
11
Task/Factorial/Dylan/factorial-2.dylan
Normal file
11
Task/Factorial/Dylan/factorial-2.dylan
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
define method factorial (n)
|
||||
if (n < 1)
|
||||
error("invalid argument");
|
||||
else
|
||||
let total = 1;
|
||||
for (i from n to 2 by -1)
|
||||
total := total * i;
|
||||
end;
|
||||
total
|
||||
end
|
||||
end method;
|
||||
13
Task/Factorial/Dylan/factorial-3.dylan
Normal file
13
Task/Factorial/Dylan/factorial-3.dylan
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
define method factorial (n)
|
||||
if (n < 1)
|
||||
error("invalid argument");
|
||||
end;
|
||||
local method loop (n)
|
||||
if (n <= 2)
|
||||
n
|
||||
else
|
||||
n * loop(n - 1)
|
||||
end
|
||||
end;
|
||||
loop(n)
|
||||
end method;
|
||||
16
Task/Factorial/Dylan/factorial-4.dylan
Normal file
16
Task/Factorial/Dylan/factorial-4.dylan
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
define method factorial (n)
|
||||
if (n < 1)
|
||||
error("invalid argument");
|
||||
end;
|
||||
// Dylan implementations are required to perform tail call optimization so
|
||||
// this is equivalent to iteration.
|
||||
local method loop (n, total)
|
||||
if (n <= 2)
|
||||
total
|
||||
else
|
||||
let next = n - 1;
|
||||
loop(next, total * next)
|
||||
end
|
||||
end;
|
||||
loop(n, n)
|
||||
end method;
|
||||
Loading…
Add table
Add a link
Reference in a new issue