apotheon has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
- apotheon
CopyWrite Chad Perrin |
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: a better fibonacci subroutine
by shmem (Chancellor) on Dec 15, 2006 at 08:49 UTC | |
by apotheon (Deacon) on Dec 15, 2006 at 09:04 UTC | |
Re: a better fibonacci subroutine
by jbert (Priest) on Dec 15, 2006 at 09:55 UTC | |
by Tanktalus (Canon) on Dec 15, 2006 at 15:31 UTC | |
by jbert (Priest) on Dec 15, 2006 at 16:15 UTC | |
by TimToady (Parson) on Dec 15, 2006 at 18:28 UTC | |
Re: a better fibonacci subroutine
by herby1620 (Monk) on Dec 15, 2006 at 21:28 UTC |