Today I was in a different sort of mood. I decided to play with some Haskell, and ended up with the following. I started with a recursive function in Haskell which computes products:
product [] = 1
product (x:xs) = x * product xs
I then decided to write the same functionality in Perl 5 and Perl 6 to see how they compared. Along the way I ran into reduction operators, which turn out to appear in some form in all three languages, although I have never used them in any. Here's the function looks lik all three languages. I provide the print statement in only one case, since it is basically the same in all three:
-- Haskell
prod = foldr (*) 1
# Perl 5
use List::Util qw(reduce);
sub prod { reduce { $a * $b } 1, @_ }
# Perl 6
sub prod (*@xs) { [*] @xs }
print prod(2,3,4);
Not only does the Haskell solution look the cleanest to me, it was the only language I didn't find a related bug in. I added tests for [*]() and [+](), which are not yet returning 1 and 0 as expected in Pugs.
It turns out perl5 List::Util::sum() was returning "undef" for a empty lists instead of 0. I submitted a patch for that.
Let's be fair... (Score:2)
Only because you didn't do it as cleanly as you could have. :-)
A fairer comparison would be:
Or, if you prefer the equals sign:
Damian
Re: (Score:1)
Can that last one be written with the
->op?Re: (Score:2)
Damian