1.14 About Ranges

A range is a finite sequence of integers. This is another special kind of list. A range is described by its minimum (the first entry), its second entry and its maximum, separated by a comma resp. two dots and enclosed in brackets. 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. If this is true for a list but the list is not yet represented in the compact form of a range this will be done then.

    gap> a:= [-2,-1,0,1,2,3,4,5];
    [ -2, -1, 0, 1, 2, 3, 4, 5 ]
    gap> IsRange(a);
    true
    gap> a;
    [ -2 .. 5 ]
    gap> a[5];
    2
    gap> Length(a);
    8 

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.

In this section you have seen that ascending lists of consecutive integers can be represented in a compact way as ranges.

Chapter Ranges contains a detailed description of ranges. A fundamental application of ranges is introduced in the next section.

Previous Up Top Next
Index

GAP 3.4.4
April 1997