You have two ways of getting the result. The iterative case (Haskell's) and the recursive case (yours). The Haskell method is very easy to follow, just write the values for $a and $b while it iterates. The recursive solution works as well, but (big but here!) it calls the 'fib' function greater than the fibonacci times for every number you wish to get (except for the first two cases). This number grows VERY quickly. It is similar to a fibonacci sequence, but with different starting points
Call      Number of calls   Calls
fib(1)    0                 Internal
fib(2)    0                 Internal
fib(3)    2                 fib(2), fib(1)
fib(4)    4                 fib(3), [fib(2), fib(1)], fib(2)
fib(5)    6                 fib(4)[4]... fib(3)[2]...
fib(6)   10                 fib(5)[6]... fib(4)[4]...
fib(7)   16                 fib(6)[10]... fib(5)[6]...
fib(8)   26                 fib(7)[16]... fib(6)[10]...
While this DOES demonstrate recursion, it can consume resources. Being a bit of an 'old school' type programmer who grew up with machines with limited resources using an iterative solution is perfered (to me!).

In reply to Re: a better fibonacci subroutine by herby1620
in thread a better fibonacci subroutine by apotheon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.