Bugs in gen_fib examples (pp80-81) #166

ghewson opened this Issue Oct 6, 2011 · 1 comment


None yet

2 participants

ghewson commented Oct 6, 2011

In both versions of gen_fib, the closure unnecessarily re-calculates the last value of @fibs. For instance, if @fibs contains (0, 1, 1), then when called for fib(3) the closure calculates fib(2) then fib(3), then when called for fib(4) it calculates fib(3) then fib(4), etc. So in both versions this line:

            for my $calc ((@fibs - 1) .. $item)

should be this:

            for my $calc (@fibs .. $item)

Also, it's not necessary to initialise the cache with fib(2). Though doing so doesn't hurt the code, it's potentially confusing. So this:

    my @fibs = (0, 1, 1);

should be this:

    my @fibs = (0, 1);

Good catch. Fixed in 0ed4d23. Thank you!

@chromatic chromatic closed this Oct 22, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment