RFC 82 (v1) Make operators behave consistently in a

[email protected] (Perl6 RFC Librarian)
Newsgroups perl.perl6.language,perl.perl6.announce
Message-ID <[email protected]>
This and other RFCs are available on the web at
  http://dev.perl.org/rfc/

=head1 TITLE

Make operators behave consistently in a list context

=head1 VERSION

  Maintainer: Jeremy Howard <[email protected]>
  Date: 10 August 2000
  Version: 1.00
  Mailing List: [email protected]
  Number: 82

=head1 ABSTRACT

It is proposed that in a list context, operators are applied
component-wise to their arguments. Furthermore, it is proposed that
this behaviour be extended to functions that do not provide a specific
list context.

=head1 DESCRIPTION

Currently, operators applied to lists in a list context behave
counter-intuitively:

  @b = (1,2,3);
  @c = (2,4,6);
  @d = @b * @c;   # Returns (9) == scalar @b * scalar @c

This RFC proposes that operators in a list context should be applied
component-wise to the elements of their arguments:

  @d = @b * @c;   # Returns (2,8,18)

If the lists are not of equal length, the shorter lists should be treated
as if they were padded with undefs at the right-hand end. However, this
implicit padding should not cause these elements in the shorter lists to
auto-vivify.

Functions that do not return a list should be treated in the same way:

  @e = (-1,1,-3);
  @f = abs(@e);   # Returns (1,1,3)

=head1 IMPLEMENTATION

These operators and functions should be evaluated lazily. For instance:

  @b = (1,2,3);
  @c = (2,4,6);
  @d = (-2,-4,-6);
  $sum = reduce ^_+^_, @b * @c + @d;

should be evaluated as if it read:

  $sum = 0;
  $sum += $b[$_] * $c[$_] + $d[_] for (0..$#b-1));

That is, no temporary list is created, and only one loop is required.

The proposal to handle functions is tricky, since there is currently no
obvious way to see whether a function is going to return a list. Either we
need some kind of more advanced prototyping (or other way of creating a
signature), or we need to manually change functions provided with Perl to
check for list context and Do The Right Thing.

=head1 REFERENCES

  The Mathematica Navigator, Heikki Ruskeepää,
    Academic Press, ISBN 0-12-603640-3, p383.

  Expression Templates (C++ Implementation):

http://extreme.indiana.edu/~tveldhui/papers/techniques/techniques01.html#l32

  Implementation in Perl Data Language:
    http://pdl.perl.org
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.