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 |
In reply to a better fibonacci subroutine by apotheon
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |