I ran across something earlier today wherein someone held forth on the subject of the syntactic supremacy of Haskell over just about all other languages, in a piece titled on syntax. The thesis of this thing was, in essence, that the "language of the future" should have a syntax like Haskell's, because Haskell's syntax is best.

I'm skeptical, of course. As a means of demonstrating how great Haskell's syntax is, however, he decided to use a simple fibonacci number calculation subroutine, implemented in a number of different languages. His Perl example looked like this:

sub fibo { my ($n, $a, $b) = (shift, 0, 1); ($a, $b) = ($b, $a + $b) while $n-- > 0; $a; }

I find that to be unnecessarily ugly. I edited it a little bit for my own purposes, adding code to actually use the subroutine and reformatting it to eliminate a little aesthetic discomfort thusly:

use strict; use warnings; use Memoize; memoize('fib'); foreach my $x (0..$ARGV[0]) { print fib($x); } sub fib { my ($n, $a, $b) = (shift, 0, 1); ($a, $b) = ($b, $a + $b) while $n-- > 0; $a; }

Even so, I don't really think it's ideal. I wrote another version of it from scratch, in a manner I thought would be much prettier:

use strict; use warnings; use Memoize; memoize('fib'); foreach my $x (0..$ARGV[0]) { print fib($x); } sub fib { my $n = shift; return $n if $n < 2; return fib($n - 1) + fib($n - 2); }

So, I do have a question:

Is there any particular reason the Haskell guy's version is better than my recursive version?

print substr("Just another Perl hacker", 0, -2);
- apotheon
CopyWrite Chad Perrin


In reply to 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.