[Next] [Up] [Previous] [Contents] [Index]
Next: Hashtables (not in ISO) Up: Data Types and Built-ins Previous: Set manipulation (not in   Contents   Index

Arrays (not in ISO)

An array is a collection of elements. As array elements can be arrays, arrays can be multi-dimensional. Arrays are created by the built-in predicate new_array(X,Ranges) where X must be an uninstantiated variable and Ranges must be a list of positive integers. Let Ranges be [N1,...,Nn]. Then, N1 is the size of the first dimension, N2 is the size of the second dimension, and so on. For example, the call new_array(X,[10,20]) binds X to a two dimensional array where the first dimension has 10 elements and the second dimension has 20 elements. The 200 array elements are free variables when the array is created.

The operator @= is provided for initializing and accessing arrays. The call A^[I] @= Elm unifies the Ith element of A with Elm. The index I must be an expression whose value is from 1 to the size of the array. A type exception will be raised if A is not an array. This notation extends to multi-dimensional arrays. In general, the call A^[I1,...,In] @= Elm is equivalent to:

       A^[I1] @= T, T^[I2,...,In] @= Elm

The operator @= can also be used to initialize arrays. For example,

       new_array(X,[3]), X @= [1,2,3]
creates a one-dimensional array with three integers 1, 2, and 3, and the following
       new_array(X,[2,2]), X @= [[1,2],[3,4]]
creates a two-dimensional array with four integers. Notice that lists or lists of lists are not treated as arrays in other contexts.


[Next] [Up] [Previous] [Contents] [Index]
Next: Hashtables (not in ISO) Up: Data Types and Built-ins Previous: Set manipulation (not in   Contents   Index
Neng-Fa Zhou () 2007-06-05