| ?- 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 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[I1,...,In], where X is a structure, and each Ii is an integer expression. However, this common notation for accessing arrays is not part of the standard Prolog syntax. In order to accommodate this notation, the parser is modified to insert a token, , between a variable token and [. So, the notation X[I1,...,In] is just a shorthand for X
[I1,...,In]. 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 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.
X^[I,J] @= Xij
. .
X^[I,J,K] @= Xijk
. .
Neng-Fa Zhou 2013-01-25