This second section about lists is dedicated to the subtle difference between equality and identity of lists. It is really important to understand this difference in order to understand how complex data structures are realized in GAP. This section applies to all GAP objects that have subobjects, e.g., to lists and to records. After reading the section Plain Records about records you should return to this section and translate it into the record context.
Two lists are equal if all their entries are equal. This means that the
equality operator = returns true for the comparison of two lists if
and only if these two lists are of the same length and for each position
the values in the respective lists are equal.
gap> numbers := primes;; numbers = primes; trueWe assigned the list
primes to the variable numbers and, of course
they are equal as they have both the same length and the same entries.
Now we will change the third number to 4 and compare the result again
with primes.
gap> numbers[3]:= 4;; numbers = primes; trueYou see that
numbers and primes are still equal, check this by
printing the value of primes. The list primes is no longer a list of
primes! What has happened? The truth is that the lists primes and
numbers are not only equal but they are also identical. primes and
numbers are two variables pointing to the same list. If you change the
value of the subobject numbers[3] of numbers this will also change
primes. Variables do not point to a certain block of storage memory
but they do point to an object that occupies storage memory. So the
assignment numbers := primes did not create a new list in a different
place of memory but only created the new name numbers for the same old
list of primes.
From this we see that the same object can have several names.
If you want to change a list with the contents of primes independently
from primes you will have to make a copy of primes by the function
ShallowCopy which takes an object as its argument and returns a copy of
the argument. (We will first restore the old value of primes.)
gap> primes[3]:= 5;; primes; [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,,,,,, 71 ] gap> numbers:= ShallowCopy(primes);; numbers = primes; true gap> numbers[3]:= 4;; numbers = primes; falseNow
numbers is no longer equal to primes and primes still is a list
of primes. Check this by printing the values of numbers and primes.
Lists and records can be changed this way because GAP objects of these types have subobjects. To clarify this statement consider the following example.
gap> i:= 1;; j:= i;; i:= i+1;;By adding 1 to
i the value of i has changed. What happens to j?
After the second statement j points to the same object as i, namely
to the integer 1. The addition does not change the object 1 but
creates a new object according to the instruction i+1. It is actually
the assignment that changes the value of i. Therefore j still points
to the object 1. Integers (like permutations and booleans) have no
subobjects. Objects of these types cannot be changed but can only be
replaced by other objects. And a replacement does not change the values
of other variables. In the above example an assignment of a new value to
the variable numbers would also not change the value of primes.
Finally try the following examples and explain the results.
gap> l:= [];; l:= [l]; [ [ ] ] gap> l[1]:= l; [ ~ ]Now return to Section Plain Lists and find out whether the functions
Add and Append change their arguments.
[Top] [Previous] [Up] [Next] [Index]
GAP 4 manual