Anonymous recursion: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added comments, added subroutine fence, added DO-END comment label, changed comments, added whitespace. -- ~~~~)
(Added simple D entry)
Line 331: Line 331:


=={{header|D}}==
=={{header|D}}==
<lang d>uint fib(in uint n) pure nothrow {
In D anonymous delegates/functions can't refer themselves. Here an anonymous class is created, and by using opCall member function, the anonymous class object can take arguments and act like an anonymous function. The recursion is done by calling opCall inside itself.
immutable self = &__traits(parent, {});
return (n < 2) ? n : self(n - 1) + self(n - 2);
}

void main() {
import std.stdio;
writeln(fib(39));
}</lang>
{{out}}
<pre>63245986</pre>
===With Anonymous Class===
In this version anonymous class is created, and by using opCall member function, the anonymous class object can take arguments and act like an anonymous function. The recursion is done by calling opCall inside itself.
<lang d>import std.stdio;
<lang d>import std.stdio;


Line 350: Line 362:
writeln(fib(39));
writeln(fib(39));
}</lang>
}</lang>
The output is the same.
{{out}}
<pre>63245986</pre>


=={{header|Dylan}}==
=={{header|Dylan}}==