http://d.puremagic.com/issues/show_bug.cgi?id=7646

           Summary: bug in code sample and unittest
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: trivial
          Priority: P2
         Component: websites
        AssignedTo: nob...@puremagic.com
        ReportedBy: josvanu...@gmail.com


--- Comment #0 from josvanu...@gmail.com 2012-03-04 11:59:48 PST ---
The following sample code on std.functional contains a bug.
http://dlang.org/phobos/std_functional.html#memoize

    ulong fib(ulong n) { 
        alias memoize!fib mfib; 
        return n < 2 ? 1 : mfib(n - 2) + mfib(n - 1); 
    } 

    assert(fib(10) == 89);

fib(10) should be 55. 
http://en.wikipedia.org/wiki/Fibonacci_number#List_of_Fibonacci_numbers

I think the code should be (n <= 2):

    ulong fib(ulong n) { 
        alias memoize!fib mfib; 
        return n <= 2 ? 1 : mfib(n - 2) + mfib(n - 1); 
    }

    assert(fib(10) == 55); 

It's also in the unittest
https://github.com/D-Programming-Language/phobos/blob/master/std/functional.d

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------

Reply via email to