1.10 About Identical Lists

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, i.~e., to lists and to records. After reading the section about 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;
    [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,,,,,, 71 ]
    gap> numbers = primes;
    true 

We 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;
    4
    gap> numbers = primes;
    true 

You 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 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.

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 Copy 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;
    5
    gap> primes;
    [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,,,,,, 71 ]
    gap> numbers:= Copy(primes);
    [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,,,,,, 71 ]
    gap> numbers = primes;
    true
    gap> numbers[3]:= 4;
    4
    gap> numbers = primes;
    false 

Now 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.

The only objects that can be changed this way are records and lists, because only 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:= [];
    [  ]
    gap> l:= [l];
    [ [  ] ]
    gap> l[1]:= l;
    [ ~ ] 

Now return to the preceding section About Lists and find out whether the functions Add and Append change their arguments.

In this section you have seen the difference between equal lists and identical lists. Lists are objects that have subobjects and therefore can be changed. Changing an object will change the values of all variables that point to that object. Be careful, since one object can have several names. The function Copy creates a copy of a list which is then a new object.

You will find more about lists in chapter Lists, and more about identical lists in Identical Lists.

Previous Up Top Next
Index

GAP 3.4.4
April 1997