Previous Up Next

4.3  Working with Arrays of Items

For convenience, ECLiPSe has some features for facilitating working with arrays of items. Arrays can be of any dimension, and can be declared with the dim/2 predicate:

?- dim(M,[3,4]).
M = []([](_131, _132, _133, _134),
       [](_126, _127, _128, _129),
       [](_121, _122, _123, _124))
yes.

dim/2 can also be used to query the dimensions of an array:

?- dim(M,[3,4]), dim(M,D).
...
D = [3, 4]
yes.
Note that arrays are just structures, and that the functor is not important.

To access a specific element of an array in an expression, specify the index list of the desired element, e.g.

?- M = []([](2, 3, 5),
          [](1, 4, 7)),  X is M[1, 2] + M[2, 3].
X = 10
M = []([](2, 3, 5), [](1, 4, 7))
yes.


  • Arrays are just structures
  • The functor is not important
  • Declare or query array size with dim/2
  • Access elements in expressions by specifying their index list (e.g. A[7], M[2,3])
  • Indices start at 1
Figure 4.2: Array notation

For further details see the Array Notation section of the User Manual.

Previous Up Next