develooper Front page | | Postings from September 2000

RFC 117 (v2) Perl syntax support for ranges

Perl6 RFC Librarian
September 11, 2000 11:50
RFC 117 (v2) Perl syntax support for ranges
Message ID:
This and other RFCs are available on the web at

=head1 TITLE

Perl syntax support for ranges

=head1 VERSION

  Maintainer: pdl-porters team <>
  Date: 16 August 2000
  Last Modified: 11 Spetember 2000
  Mailing List:
  Number: 117
  Version: 2
  Status: Withdrawn
  Superceded-by: RFC 81, v3


This RFC has been withdrawn after all of its principal points
were amalgamated into RFC 81v3 I<Lazily evaluated list generation
functions> which we hereby endorse. At the point in time when all
RFCs will be frozen or withdrawn it should be verified that RFC 81
still mentions the features requested below.


This RFC proposes syntactic support for ranges. Range objects
would be especially useful to specify indexing ranges of compact
numerical arrays (currently implemented by PDL) in a concise
manner, e.g.

  $y = $x->slice(0:$n-1:4);
Note that currently (perl5) we have to say

  $n1 = $n-1;  # since we need to stringify
  $y = $x->slice("0:$n1:4");

This should be contrasted with the less cluttered syntax offered
by numerical Python and commercial systems such as Matlab and IDL:

  y = x[0:n-1:4]; 
In perl we desire to say:

 $y = $x(0:$n-1,4);  
or even 

 @y = @x[0:$n-1,4]; 
If there is a more general unification of PDL arrays and normal
perl arrays. (See L<RFC 203-207>).

This RFC proposes ranges as part of the syntax cleanup, L<RFC 115>
proposes a way top overload C<()> for objects. This RFC is closely
linked to RFCs 81 and 169 (and proposes a subset of the features in
those RFCs) but emphasizes two additional aspects: Motivation by
requirements of PDL and the important implementation aspect of
I<infinitely lazy evaluation>. Ultimately we hope to retire this RFC
when RFCs 81 and 169 have been appropriately updated.


A range would be an object that is different from current perl5
lists but could be interpolated into one if desired. The C<:> operator
would be the range generating operator.

The behaviour would be similar to the '..' operator but generally
no explicit in-memory list would be generated. These ranges would be
very useful to concisely specify subslices into multi-dimensional
numerical arrays.

Perhaps the '..' operator can be recycled as
something else and ':' used for all ranges (or ':' simply be an alias
for '..').

=head2 Examples:

         :               # all the things on this dimension: full span
         5:-1            # 5..last
         5:-1:2          # Every second item, up to the last or second last
         -1:7:3          # Start with last item, then fourth last, etc. until 7

  for (0:7) { ... }
  for (0.1:1.0:0.1) { ... }

  $pdl->slice(-1:$n:3) .= 5;
  $pdl->slice(:,::2) *= 2;


Possible. A range should probably be a lazily evaluated list which
functions can choose to accept without the need to actually generate
the list in memory. Compare the RFC on lazy list generation.

A detail that is crucial from our point of view that is not mentioned
in RFC 81 is the ability for a function to inquire the C<start:stop:step>
parameters of the range and do its own thing without ever generating
a list or list iterator -- an implementation aspect we refer to as
I<infinitely lazy evaluation>.

=head2 Issues

An issue is the treatment of potentially infinite ranges of the form


Those could raise an error if used in a context where actual list
items are generated, e.g.

   for (:) { $a *= $_ }

but be allowed in circumstances where no explicit list item is
ever created, e.g.


which might actually be written in perl6 as

  $a(:); # and/or

=head1 SEE ALSO

L<PDL> (

RFC 24: Semi-finite (lazy) lists

RFC 81: Lazily evaluated list generation functions

RFC 169: Proposed syntax for matrix element access and slicing

RFC 115: Overloadable parentheses for objects Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About