6.1 Array Declarations

Array declarations in REDUCE are similar to FORTRAN dimension statements. For example:

        array a(10),b(2,3,4);

Array indices each range from 0 to the value declared. An element of an array is referred to in standard FORTRAN notation, e.g. A(2).

We can also use an expression for defining an array bound, provided the value of the expression is a positive integer. For example, if X has the value 10 and Y the value 7 then array c(5*x+y) is the same as array c(57).

If an array is referenced by an index outside its range, an error occurs. If the array is to be one-dimensional, and the bound a number or a variable (not a more general expression) the parentheses may be omitted:

        array a 10, c 57;

The operator LENGTH applied to an array name returns a list of its dimensions.

All array elements are initialized to 0 at declaration time. In other words, an array element has an instant evaluation property and cannot stand for itself. If this is required, then an operator should be used instead.

Array declarations can appear anywhere in a program. Once a symbol is declared to name an array, it can not also be used as a variable, or to name an operator or a procedure. It can however be re-declared to be an array, and its size may be changed at that time. An array name can also continue to be used as a parameter in a procedure, or a local variable in a compound statement, although this use is not recommended, since it can lead to user confusion over the type of the variable.

Arrays once declared are global in scope, and so can then be referenced anywhere in the program. In other words, unlike arrays in most other languages, a declaration within a block (or a procedure) does not limit the scope of the array to that block, nor does the array go away on exiting the block (use CLEAR instead for this purpose).