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]; 17This 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 one 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. The
next position after the end of a list is not the only position capable
of taking a new value. If you know that 71 is the 20th prime, you can
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 now has length
20.
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); 20The 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; Variable: 'lll' must have a value
gap> lll:= []; lll[1]:= 2; [ ] 2Of 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.
If the element is not contained in the list then Position will return
the special object fail.
gap> Position(primes, 17); 7 gap> Position(primes, 20); failIn 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. 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, where
dense means that the list has no holes. 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> s1 := ['H','a','l','l','o',' ','w','o','r','l','d','.']; "Hallo world." gap> s1 = "Hallo world."; true gap> s1[7]; 'w'
Sublists of lists can easily be extracted and assigned using the operator
list{ positions }.
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 whose ith entry is that element of the
original list whose position is the ith entry of the argument in the
curly braces.
GAP 4 manual