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

37 lines
501 B
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
import extensions;
fib(n)
{
2024-03-06 22:25:12 -08:00
if (n < 0)
{ InvalidArgumentException.raise() };
2023-07-01 11:58:00 -04:00
2024-03-06 22:25:12 -08:00
^ (n) {
if (n > 1)
{
^ this self(n - 2) + (this self(n - 1))
}
else
{
^ n
}
}(n)
2023-07-01 11:58:00 -04:00
}
2026-02-01 16:33:20 -08:00
public Program()
2023-07-01 11:58:00 -04:00
{
2024-03-06 22:25:12 -08:00
for (int i := -1; i <= 10; i += 1)
{
2025-06-11 20:16:52 -04:00
Console.print("fib(",i,")=");
2024-03-06 22:25:12 -08:00
try
{
2025-06-11 20:16:52 -04:00
Console.printLine(fib(i))
2024-03-06 22:25:12 -08:00
}
catch(Exception e)
{
2025-06-11 20:16:52 -04:00
Console.printLine("invalid")
2024-03-06 22:25:12 -08:00
}
};
2023-07-01 11:58:00 -04:00
2025-06-11 20:16:52 -04:00
Console.readChar()
2023-07-01 11:58:00 -04:00
}