RosettaCodeData/Task/Anonymous-recursion/Elena/anonymous-recursion.elena

38 lines
591 B
Text
Raw Permalink Normal View History

2019-09-12 10:33:56 -07:00
import extensions;
2013-04-10 16:57:12 -07:00
2018-08-17 15:15:24 +01:00
fib(n)
2019-09-12 10:33:56 -07:00
{
2018-08-17 15:15:24 +01:00
if (n < 0)
2019-09-12 10:33:56 -07:00
{ InvalidArgumentException.raise() };
2013-04-10 16:57:12 -07:00
2019-09-12 10:33:56 -07:00
^ (n)
{
2018-08-17 15:15:24 +01:00
if (n > 1)
2019-09-12 10:33:56 -07:00
{
^ this self(n - 2) + (this self(n - 1))
}
else
{
^ n
}
}(n)
}
2013-04-10 16:57:12 -07:00
2019-09-12 10:33:56 -07:00
public program()
{
for (int i := -1, i <= 10, i += 1)
{
console.print("fib(",i,")=");
try
{
console.printLine(fib(i))
}
catch(Exception e)
2017-09-23 10:01:46 +02:00
{
2019-09-12 10:33:56 -07:00
console.printLine:"invalid"
2018-08-17 15:15:24 +01:00
}
2019-09-12 10:33:56 -07:00
};
console.readChar()
}