langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
35
Task/Factorial/Nemerle/factorial.nemerle
Normal file
35
Task/Factorial/Nemerle/factorial.nemerle
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
using System;
|
||||
using System.Console;
|
||||
|
||||
module Program
|
||||
{
|
||||
Main() : void
|
||||
{
|
||||
WriteLine("Factorial of which number?");
|
||||
def number = long.Parse(ReadLine());
|
||||
WriteLine("Using Fold : Factorial of {0} is {1}", number, FactorialFold(number));
|
||||
WriteLine("Using Match: Factorial of {0} is {1}", number, FactorialMatch(number));
|
||||
WriteLine("Iterative : Factorial of {0} is {1}", number, FactorialIter(number));
|
||||
}
|
||||
|
||||
FactorialFold(number : long) : long
|
||||
{
|
||||
$[1L..number].FoldLeft(1L, _ * _ )
|
||||
}
|
||||
|
||||
FactorialMatch(number : long) : long
|
||||
{
|
||||
|0L => 1L
|
||||
|n => n * FactorialMatch(n - 1L)
|
||||
}
|
||||
|
||||
FactorialIter(number : long) : long
|
||||
{
|
||||
mutable accumulator = 1L;
|
||||
for (mutable factor = 1L; factor <= number; factor++)
|
||||
{
|
||||
accumulator *= factor;
|
||||
}
|
||||
accumulator //implicit return
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue