SPUG: Grep syntax

Joshua ben Jore twists at gmail.com
Sun Jun 17 12:00:58 PDT 2007

On 6/16/07, Yitzchak Scott-Thoennes <sthoenna at efn.org> wrote:
> On Sat, June 16, 2007 12:39 am, Shawn W. wrote:
> > I suspect a lot of perl users don't  have a great deal of familiarity
> > with functional languages where list processing is emphasized than it is in
> > perl -- lisp, scheme, the MLs, and so on.  map, filter/grep, and one that
> > perl's missing as a built-in -- fold (which can be used to create pretty
> > much any other list function) are used all over the place in those
> > languages.
> What would a perl equivalent of fold look like?
> A lot of useful stuff in perl isn't built in.  The core module
> List::Util is invaluable.  From CPAN, there's lots of handy things
> in Algorithm::Loops and List::MoreUtils.

I question slightly the necessity of fold() in Perl 5. During much of
my recent exposure to list based languages, lists all seem to be
constructed of pairs. foldl and foldr are naturals in this case
because the atomic list element is a pair. In perl, lists are either a
single entity on the stack or a single entity in an array. We do not
have the "pair" concept. You can of course implement pairs and do many
pair-wise things in perl but it's against the grain.

Of course, in Perl 5 we still have lots of binary operators which is
why List::Util's reduce() function is spiffy. It's just foldl. For
unary operators the map builtin suffices. In these two cases there's a
tension between the idea of lists as whole entities and lists as
linked pairs. Most of our tools still operate in a pair-wise manner
but in theory... Perl 5 could have Perl 6's reduction metaoperator.

I'd also note that Perl 6 is getting away from a single binary fold
operator. The reduction operator
(http://dev.perl.org/perl6/doc/design/syn/S03.html) allows binary
operators like '+' to be promoted to an N-ary operator.

[+], ELT0, ELT1, ELT2, ELT3


ELT0 + ELT1 + ELT2 + ELT3

Whether this is done as foldl or foldr depends on the associativity of
the operator. Perhaps then this does make foldl and foldr necessary
because you may want to choose one explicitly for memory reasons.

Something to note, Perl 6's reduction operator could in theory be
mixed associative if it's under the influence of strange data and
untoward intent.


More information about the spug-list mailing list