3.5 Ranges

A range is a finite arithmetic progression of integers. This is another special kind of list. A range is described by the first two values and the last value of the arithmetic progression which are given in the form [first,second..last]. In the usual case of an ascending list of consecutive integers the second entry may be omitted.

gap> [1..999999];     #  a range of almost a million numbers
[ 1 .. 999999 ]
gap> [1, 2..999999];  #  this is equivalent
[ 1 .. 999999 ]
gap> [1, 3..999999];  #  here the step is 2
[ 1, 3 .. 999999 ]
gap> Length( last );
500000
gap> [ 999999, 999997 .. 1 ];
[ 999999, 999997 .. 1 ]
This compact printed representation of a fairly long list corresponds to a compact internal representation. The function IsRange tests whether an object is a range, the function ConvertToRangeRep changes the representation of a list that is in fact a range to this compact internal representation.
gap> a:= [-2,-1,0,1,2,3,4,5];
[ -2, -1, 0, 1, 2, 3, 4, 5 ]
gap> IsRange( a );
true
gap> ConvertToRangeRep( a );;  a;
[ -2 .. 5 ]
gap> a[1]:= 0;; IsRange( a );
false
Note that this change of representation does not change the value of the list a. The list a still behaves in any context in the same way as it would have in the long representation.

[Top] [Previous] [Up] [Next] [Index]

GAP 4 manual
February 2000