Previous Up Next

25.5.2  Assignments in a program

Recall that the =< operator will change the value of a single entry in a list or matrix by reference (see Section 3.3.3). This make it efficient when changing many values, one at a time, in a list, as might be done by a program.

You must be careful when doing this, since your intent might be changed when a program is compiled. For example, if a program contains

local a; a:=[0,1,2,3,4]; ... a[3]=<33;

then in the compiled program, a:=[0,1,2,3,4] will be replaced by a:=[0,1,2,33,4]. To avoid this, you can assign a copy of the list to a; you could write:

local a; a:=copy([0,1,2,3,4]); ... a[3]=<33;

Alternatively, you could use a command which recreates a list every time the program is run, such as makelist or $, instead of copying a list; a:=makelist(n,n,0,4) or a:=[n$(n=0..4)] can also be used in place of a:=[0,1,2,3,4].


Previous Up Next