NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

## All the Perl that's Practical to Extract and Report

### Journal of markjugg (792)

Thursday September 14, 2006
09:59 PM

### Reduction operators

[ #31001 ]

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:

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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.

Full
Abbreviated
Hidden
• #### Let's be fair...(Score:2)

Not only does the Haskell solution look the cleanest to me...

Only because you didn't do it as cleanly as you could have. :-)

A fairer comparison would be:

prod = foldr (*) 1

# Perl 6
sub prod { [*] @_ }

Or, if you prefer the equals sign:

prod  = foldr (*) 1

# Perl 6
&prod := sub{ [*] @_ }

Damian

• #### Re:(Score:1)

Can that last one be written with the `->` op?

• #### Re:(Score:2)

Yes, in this case it can. You could write:
# Perl 6
&prod := -> *@list { [*] @list }
which isn't exactly pretty, but does at least include a smiley face. ;-)

Damian