`new_array(X,Dims)`: Bind`X`to a new array of the dimensions that are specified by`Dims`, which is a list of positive integers. An array of`n`elements is represented as a structure with the functor`'[]'/n`. All of the array elements are initialized to be free variables. For example,| ?- new_array(X,[2,3]) X = []([](_360,_364,_368),[](_370,_374,_378))

`a2_new(X,N1,N2)`: This is the same as`new_array(X,[N1,N2]).``a3_new(X,N1,N2,N3)`: This is the same as`new_array(X,[N1,N2,N3]).``is_array(A)`: This succeeds if`A`is a structure whose functor is`'[]'/n`.

The built-in predicate `arg/3` can be used to access array elements, but it requires a temporary variable to store the result, and also requires a chain of calls to access an element of a multi-dimensional array. In order to facilitate the access of array elements, B-Prolog supports the array subscript notation ` X[I_{1},...,I_{n}]`, where

nth(I,L,E) :- E @= L[I]. nth0(I,L,E) :- E @= L[I+1].

In arithmetic expressions and arithmetic constraints, the term `length` indicates the size of the compound term *X*. Examples:

?-S=f(1,1), Size is S^length. Size=2 ?-L=[1,1], L^length=:=1+1. yesThe term

?-S=f(1,1), Size @= S^length. Size=2In any other context, the term

The operator `@:=` is provided for destructively updating an argument of a structure or an element of a list. For example:

?-S=f(1,1), S[1] @:= 2. S=f(2,1)The update is undone upon backtracking.

The following built-ins on arrays have become obsolete, since they can be easily implemented by using `foreach` and list comprehension (see Chapter 4).

`X^rows @= Rows`

:`Rows`is a list of rows in the array`X`. The dimension of`X`must not be less than 2.`X^columns @= Cols`

:`Cols`is a list of columns in the array`X`. The dimension of`X`must not be less than 2.`X^diagonal1 @= Diag`

:`Diag`is a list of elements in the left-up-diagonal Xn1,...,X1n of array`X`. The dimension of`X`must be 2, and the number of rows and the number of columns in`X`must be equal.`X^diagonal2 @= Diag`

:`Diag`is a list of elements in the left-down-diagonal X11,...,Xnn of array`X`. The dimension of`X`must be 2, and the number of rows and the number of columns in`X`must be equal.`a2_get(X,I,J,Xij)`: This is the same as`X^[I,J] @= Xij`

. .`a3_get(X,I,J,K,Xijk)`: This is the same as`X^[I,J,K] @= Xijk`

. .`array_to_list(X,List)`: The term`List`is a list of all of the elements in array`X`. Suppose that`X`is an`n`-dimensional array, and that the sizes of the dimensions are`N1`,`N2`, ..., and`Nn`. Then,`List`contains the elements with indices from`[1,...,1]`,`[1,...,2]`, to`[N1,N2,...,Nn]`.

Neng-Fa Zhou 2013-01-25