I am making some functions in GAP and am having weird issues with lists and variables. It seems like whenever two variables have the same value at a given point, any changes made to one will change the other even though I have defined them separately.
Here's a simple example illustrating my main problem. Suppose I write the code:
list:=[1,2,3];
list2:=list;
Add(list2,4);
Using this code, I would want to add 4 to list2, which would make list2=[1,2,3,4]. This part is fine. The unintended consequence here is that this also changes list to list=[1,2,3,4].
In this example, my question is: how do I change
list2without changinglist?
When assigning objects in GAP, the assignment is always a pointer to the objects. For objects that do not have the ability to change stored information that does not matter, but for lists or records it means that a change to the entries of the one object also changes the other.
To avoid this happening, there are two kinds of copy functions"
ShallowCopyduplicates the top-level object, but changes the entries.StructuralCopyduplicates the whole structure (which is often wasting memory). If an object has no further substructures, both functions do the same. For example: