I think both solutions are clear and readable.

They have different efficiency tradeoffs - yours trades memory for faster future lookups. It will probably be slower any time you significantly raise the N for which you called fibs, but will become a quick lookup for all values below that. The other solution recalculates each time, but requires small, constant storage.

I've been playing with haskell a little recently, it's an interesting language. There seems to be a greater emphasis on case analysis in the language, as exemplified by the pattern matching approach to function definition. For example, one way (perhaps not efficient) way of defining fibs in haskell is:

fibs 0 = 1 fibs 1 = 1 fibs n = fibs (n - 1) + fibs (n - 2)
which looks a lot like a mathematical description of the function.

As to whether the above is efficient or not, it depends on whether fibs is memoized or not. On the face of it, it doesn't appear to be. Then you recall that Haskell is a functional language, with no assignment (ok, it's not that simple), and so the compiler is perfectly at liberty to memoize any functions it wants...and so it may well be the same as your explicitly memoized solution above.

Whilst we're on the subject of Haskell, you also have to love the ability of lazy evaluation to give you infinite lists of things:

ones = 1 : ones integers = 1 : map (1+) integers
The infinite list of 1s? Oh, that's a 1, followed by the infinite list of ones. The infinite list of integers? That's a 1, followed by the infinite list of integers with 1 added to them all.

Again, it all seems very mathematical - writing down definitions and letting the compiler take care of the details. And again, it probably doesn't stay that simple with real-world programs.

Roll on perl6.


In reply to Re: a better fibonacci subroutine by jbert
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.