2.4 Variables versus Objects

The constants described in the last section are specified by certain combinations of digits and minus signs (in the case of integers) or digits, commas and parentheses (in the case of permutations). These sequences of characters always have the same meaning to GAP. On the other hand, there are variables, specified by a sequence of letters and digits (including at least one letter), and their meaning depends on what has been assigned to them. An assignment is done by a GAP command sequence_of_letters_and_digits := meaning, where the sequence on the left hand side is called the identifier of the variable and it serves as its name. The meaning on the right hand side can be a constant like an integer or a permutation, but it can also be almost any other GAP object. From now on, we will use the term object to denote something that can be assigned to a variable.

There must be no whitespace between the : and the = in the assignment operator. Also do not confuse the assignment operator with the single equality sign = which in GAP is only used for the test of equality.

gap> a:= (9 - 7) * (5 + 6);
22
gap> a;
22
gap> a * (a + 1);
506
gap> a = 10;
false
gap> a:= 10;
10
gap> a * (a + 1);
110 
After an assignment the assigned object is echoed on the next line. The printing of the object of a statement may be in every case prevented by typing a double semicolon.
gap> w:= 2;; 
After the assignment the variable evaluates to that object if evaluated. Thus it is possible to refer to that object by the name of the variable in any situation.

This is in fact the whole secret of an assignment. An identifier is bound to an object and from this moment points to that object. Nothing more. This binding is changed by the next assignment to that identifier. An identifier does not denote a block of memory as in some other programming languages. It simply points to an object, which has been given its place in memory by the GAP storage manager. This place may change during a GAP session, but that doesn't bother the identifier. The identifier points to the object, not to a place in the memory.

For the same reason it is not the identifier that has a type but the object. This means on the other hand that the identifier a which now is bound to an integer object may in the same session point to any other object regardless of its type.

Identifiers may be sequences of letters and digits containing at least one letter. For example abc and a0bc1 are valid identifiers. But also 123a is a valid identifier as it cannot be confused with any number. Just 1234 indicates the number 1234 and cannot be at the same time the name of a variable.

Since GAP distinguishes upper and lower case, a1 and A1 are different identifiers. Keywords such as quit must not be used as identifiers. You will see more keywords in the following sections.

In the remaining part of this manual we will ignore the difference between variables, their names (identifiers), and the objects they point to. It may be useful to think from time to time about what is really meant by terms such as ``the integer w''.

There are some predefined variables coming with GAP. Many of them you will find in the remaining chapters of this manual, since functions are also referred to via identifiers.

You can get an overview of all GAP variables by entering NamesGVars(). Many of these are predefined. If you are interested in the variables you have defined yourself in the current GAP session, you can enter NamesUserGVars().

gap> NamesUserGVars();
[ "a", "w" ]
This seems to be the right place to state the following rule: The name of every global variable in the GAP library starts with a capital letter. Thus if you choose only names starting with a small letter for your own variables you will not attempt to overwrite any predefined variable. (Note that most of the predefined variables are read-only, and trying to change their values will result in an error message.)

There are some further interesting variables one of which will be introduced now.

Whenever GAP returns an object by printing it on the next line this object is assigned to the variable last. So if you computed

gap> (9 - 7) * (5 + 6);
22 
and forgot to assign the object to the variable a for further use, you can still do it by the following assignment.
gap> a:= last;
22 
Moreover there are variables last2 and last3, you can guess their values.

In this section you have seen how to assign objects to variables. These objects can later be accessed through the name of the variable, its identifier. You have also encountered the useful concept of the last variables storing the latest returned objects. And you have learned that a double semicolon prevents the result of a statement from being printed.

[Top] [Previous] [Up] [Next] [Index]

GAP 4 manual
February 2000