| ?- new_array(X,[2,3])
X = []([](_360,_364,_368),[](_370,_374,_378))
The built-in predicate arg/3 can be used to access array elements, but it requires a temporary variable to store the result, and a chain of calls to access an element of a multi-dimensional array. To facilitate accessing array elements, B-Prolog supports the array subscript notation
[
,...,
], where
is a structure and each
is an integer expression. This common notation for accessing arrays is, however, not part of the standard Prolog syntax. To accommodate this notation, the parser is modified to insert a token
between a variable token and [. So, the notation
[
,...,
] is just a shorthand for ![]()
[
,...,
]. This notation is interpreted as an array access when it occurs in an arithmetic expression, a constraint, or as an argument of a call to @=/2. In any other context, it is treated as the term itself. The array subscript notation can also be used to access elements of lists. For example, the nth/3 and nth0/3 predicates can be defined as follows:
nth(I,L,E) :- E @= L[I].
nth0(I,L,E) :- E @= L[I+1].
In arithmetic expressions and arithmetic constraints, the term
length means the size of the compound term
. Examples:
?-S=f(1,1), Size is S^length.
Size=2
?-L=[1,1], L^length=:=1+1.
yes
The term
?-S=f(1,1), Size @= S^length.
Size=2
In 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 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 be no less than 2.
X^columns @= Cols:
Cols is a list of columns in the array X. The dimension of X must be no less than 2.
X^diagonal1 @= Diag:
Diag is a list of elements in the left-up-diagonal (elements Xn1,...,X1n) of array X, where the dimension of X must be 2 and the number of rows and the number of columns must be equal.
X^diagonal2 @= Diag:
Diag is a list of elements in the left-down-diagonal (elements X11,...,Xnn) of array X, where the dimension of X must be 2 and the number of rows and the number of columns must be equal.
X^[I,J] @= Xij. .
X^[I,J,K] @= Xijk. .
Neng-Fa Zhou 2012-01-03