[% setvar title Perl syntax support for ranges %]

This file is part of the Perl 6 Archive

Note: these documents may be out of date. Do not use as reference!

To see what is currently happening visit http://www.perl6.org/


Perl syntax support for ranges


  Maintainer: pdl-porters team <pdl-porters@jach.hawaii.edu>
  Date: 16 Aug 2000
  Last Modified: 11 Sep 2000
  Mailing List: perl6-language-data@perl.org
  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 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 "RFC 203-207").

This RFC proposes ranges as part of the syntax cleanup, "RFC 115" proposes a way top overload () 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 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 : 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 '..').


         :               # 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 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 infinitely lazy evaluation.


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


PDL (pdl.sourceforge.net


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