Code from a Rosetta Code Task

Ali Çehreli acehreli at yahoo.com
Thu Aug 29 16:55:31 PDT 2013


On 08/29/2013 04:37 PM, bearophile wrote:

 > H. S. Teoh:
 >
 >> Also, this is a pretty poor algorithm for generating the Fibonacci
 >> series,
 >
 > I know, but you must do what the tasks asks you:
 >
 > http://rosettacode.org/wiki/Anonymous_recursion
 >
 > Bye,
 > bearophile

Hm... Like some of the other language implementations, D's is not 
correct. There is a misunderstanding. Note that they say "which checks 
for a negative argument before doing the actual recursion."

The problem that is described is this case (note that the parameter is 
negative to make the problem meaningful):

// Actual recursive function
int fib_R(int n) pure nothrow {
     // Please ignore the algorithmic complexity issue. :)
     return (n < 2) ? n : fib_R(n - 1) + fib_R(n - 2);
}

// The non-recursive API function that calls the recursive one
int fib(int n) pure {
     enforce(n >= 0);
     return fib_R(n);
}

So the problem is asking for a solution for the problem of having to 
write two separate functions.

Ali



More information about the Digitalmars-d-learn mailing list