Code from a Rosetta Code Task
H. S. Teoh
hsteoh at quickfur.ath.cx
Thu Aug 29 15:00:09 PDT 2013
On Thu, Aug 29, 2013 at 11:00:49PM +0200, Robik wrote:
> On Thursday, 29 August 2013 at 18:57:58 UTC, Meta wrote:
> >uint fib(in uint n) pure nothrow {
> > immutable self = &__traits(parent, {});
> > return (n < 2) ? n : self(n - 1) + self(n - 2);
> >}
> >
> >I came across this while browsing Rosetta Code. It's really cool
> >how you can do recursion without anonymous functions (and this
> >will actually not work if you make fib a delegate). However, I'm
> >confused about the __traits(parent, {}) part, specifically , the
> >{} passed to parent. Does {} just mean the outer scope?
>
> {} in this case is just empty delegate function.
That's clever, but I'm not sure I understand the bit about anonymous
functions? You don't need anonymous functions to have recursion.
Also, this is a pretty poor algorithm for generating the Fibonacci
series, as it has exponential time complexity in addition to linear
space complexity. It's even worse than the recursive factorial function,
which at least doesn't have exponential time complexity.
T
--
Leather is waterproof. Ever see a cow with an umbrella?
More information about the Digitalmars-d-learn
mailing list