Anonymous recursion: Difference between revisions

Content deleted Content added
No edit summary
Line 640: Line 640:


=={{header|Elena}}==
=={{header|Elena}}==
ELENA 3.4:
ELENA 4.0:
<lang elena>import extensions.
<lang elena>/// Anonymous recursion

import extensions;


fib(n)
fib(n)
{
[
if (n < 0)
if (n < 0)
[ InvalidArgumentException new:"Must be non negative"; raise ].
{ InvalidArgumentException.raise() };
^ (:n)
^ (n)
[
{
if (n > 1)
if (n > 1)
[ ^ @self(n - 2) + @self(n - 1) ];[ ^ n ]
{
^ this self(n - 2) + (this self(n - 1))
](n)
}
]
else
{
^ n
}
}(n)
}


public program
public program()
{
[
-1 to:10 do(:i)
for (int i := -1, i <= 10, i += 1)
[
{
try (console printLine("fib(",i,")=",fib(i)))
console.print("fib(",i,")=");
try
{
console.printLine(fib(i))
}
catch(Exception e)
{
{
on(Exception e)
console.printLine:"invalid"
[
console printLine:"invalid"
]
}
}
].
};
console readChar
console.readChar()
]</lang>
}</lang>
{{out}}
{{out}}
<pre>
<pre>