1.9 About Lists

A list is a collection of objects separated by commas and enclosed in brackets. Let us for example construct the list primes of the first 10 prime numbers.

    gap> primes:= [2, 3, 5, 7, 11, 13, 17, 19, 23, 29];
    [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ] 

The next two primes are 31 and 37. They may be appended to the existing list by the function Append which takes the existing list as its first and another list as a second argument. The second argument is appended to the list primes and no value is returned. Note that by appending another list the object primes is changed.

    gap> Append(primes, [31, 37]);
    gap> primes;
    [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 ] 

You can as well add single new elements to existing lists by the function Add which takes the existing list as its first argument and a new element as its second argument. The new element is added to the list primes and again no value is returned but the list primes is changed.

    gap> Add(primes, 41);
    gap> primes;
    [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41 ] 

Single elements of a list are referred to by their position in the list. To get the value of the seventh prime, that is the seventh entry in our list primes, you simply type

    gap> primes[7];
    17 

and you will get the value of the seventh prime. This value can be handled like any other value, for example multiplied by 2 or assigned to a variable. On the other hand this mechanism allows to assign a value to a position in a list. So the next prime 43 may be inserted in the list directly after the last occupied position of primes. This last occupied position is returned by the function Length.

    gap> Length(primes);
    13
    gap> primes[14]:= 43;
    43
    gap> primes;
    [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43 ] 

Note that this operation again has changed the object primes. Not only the next position of a list is capable of taking a new value. If you know that 71 is the 20th prime, you can as well enter it right now in the 20th position of primes. This will result in a list with holes which is however still a list and has length 20 now.

    gap> primes[20]:= 71;
    71
    gap> primes;
    [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,,,,,, 71 ]
    gap> Length(primes);
    20 

The list itself however must exist before a value can be assigned to a position of the list. This list may be the empty list [ ].

    gap> lll[1]:= 2;
    Error, Variable: 'lll' must have a value
    gap> lll:= [];
    [  ]
    gap> lll[1]:= 2;
    2 

Of course existing entries of a list can be changed by this mechanism, too. We will not do it here because primes then may no longer be a list of primes. Try for yourself to change the 17 in the list into a 9.

To get the position of 17 in the list primes use the function Position which takes the list as its first argument and the element as its second argument and returns the position of the first occurrence of the element 17 in the list primes. Position will return false if the element is not contained in the list.

    gap> Position(primes, 17);
    7
    gap> Position(primes, 20);
    false 

In all of the above changes to the list primes, the list has been automatically resized. There is no need for you to tell GAP how big you want a list to be. This is all done dynamically.

It is not necessary for the objects collected in a list to be of the same type.

    gap> lll:= [true, "This is a String",,, 3];
    [ true, "This is a String",,, 3 ] 

In the same way a list may be part of another list. A list may even be part of itself.

    gap> lll[3]:= [4,5,6];; lll;
    [ true, "This is a String", [ 4, 5, 6 ],, 3 ]
    gap> lll[4]:= lll;
    [ true, "This is a String", [ 4, 5, 6 ], ~, 3 ] 

Now the tilde ~ in the fourth position of lll denotes the object that is currently printed. Note that the result of the last operation is the actual value of the object lll on the right hand side of the assignment. But in fact it is identical to the value of the whole list lll on the left hand side of the assignment.

A string is a very special type of list, which is printed in a different way. A string is simply a dense list of characters. Strings are used mainly in filenames and error messages. A string literal can either be entered simply as the list of characters or by writing the characters between doublequotes ". GAP will always output strings in the latter format.

    gap> s1 := ['H','a','l','l','o',' ','w','o','r','l','d','.'];
    "Hallo world."
    gap> s2 := "Hallo world.";
    "Hallo world."
    gap> s1 := ['H','a','l','l','o',' ','w','o','r','l','d','.'];
    "Hallo world."
    gap> s1 = s2;
    true
    gap> s2[7];
    'w' 

Sublists of lists can easily be extracted and assigned using the operator { }.

    gap> sl := lll{ [ 1, 2, 3 ] };
    [ true, "This is a String", [ 4, 5, 6 ] ]
    gap> sl{ [ 2, 3 ] } := [ "New String", false ];
    [ "New String", false ]
    gap> sl;
    [ true, "New String", false ] 

This way you get a new list that contains at position i that element whose position is the ith entry of the argument of { }.

In this long section you have encountered the fundamental concept of a list. You have seen how to construct lists, how to extend them and how to refer to single elements of a list. Moreover you have seen that lists may contain elements of different types, even holes (unbound entries). But this is still not all we have to tell you about lists.

You will find a discussion about identity and equality of lists in the next section. Moreover you will see special kinds of lists like sets (in About Sets), vectors and matrices (in About Vectors and Matrices) and Strings and Characters.

Previous Up Top Next
Index

GAP 3.4.4
April 1997