When an array is created, the range of each dimension must be specified, e.g., to create a 2x3 array with a zero-based index:
(make-array 'ho 2 3) ⇒ #2((ho ho ho) (ho ho ho))
The range of each dimension can also be given explicitly, e.g., another way to create the same array:
(make-array 'ho '(0 1) '(0 2)) ⇒ #2((ho ho ho) (ho ho ho))
The following procedures can be used with arrays (or vectors). An argument shown as idx… means one parameter for each dimension in the array. A idxlist argument means a list of such values, one for each dimension.
#t if the obj is an array, and
The second argument to scm_array_p is there for historical reasons,
but it is not used. You should always pass
#t if the obj is an array of type type, and
#f if not.
1 if the obj is an array and
0 if not.
0 if the obj is an array of type type, and
1 if not.
(make-typed-array #t fill bound ...).
Create and return an array that has as many dimensions as there are bounds and (maybe) fill it with fill.
The underlying storage vector is created according to type,
which must be a symbol whose name is the ‘vectag’ of the array as
explained above, or
#t for ordinary, non-specialized arrays.
For example, using the symbol
f64 for type will create an
array that uses a
f64vector for storing its elements, and
a will use a string.
When fill is not the special unspecified value, the new
array is filled with fill. Otherwise, the initial contents of
the array is unspecified. The special unspecified value is
stored in the variable
*unspecified* so that for example
(make-typed-array 'u32 *unspecified* 4) creates a uninitialized
u32 vector of length 4.
Each bound may be a positive non-zero integer n, in which
case the index for that dimension can range from 0 through n-1; or
an explicit index range specifier in the form
where both lower and upper are integers, possibly less than
zero, and possibly the same number (however, lower cannot be
greater than upper).
(list->typed-array #t dimspec
Return an array of the type indicated by type with elements the same as those of list.
The argument dimspec determines the number of dimensions of the array and their lower bounds. When dimspec is an exact integer, it gives the number of dimensions directly and all lower bounds are zero. When it is a list of exact integers, then each element is the lower index bound of a dimension, and there will be as many dimensions as elements in the list.
Return the type of array. This is the ‘vectag’ used for
printing array (or
#t for ordinary arrays) and can be
make-typed-array to create an array of the same kind
Return the element at
(idx …) in array.
(define a (make-array 999 '(1 2) '(3 4))) (array-ref a 2 4) ⇒ 999
#t if the given indices would be acceptable to
(define a (make-array #f '(1 2) '(3 4))) (array-in-bounds? a 2 3) ⇒ #t (array-in-bounds? a 0 0) ⇒ #f
Set the element at
(idx …) in array to obj.
The return value is unspecified.
(define a (make-array #f '(0 1) '(0 1))) (array-set! a #t 1 1) a ⇒ #2((#f #f) (#f #t))
Return a list of the bounds for each dimension of array.
(lower upper) for each
array-dimensions instead returns just
upper+1 for dimensions with a 0 lower bound. Both are
suitable as input to
(define a (make-array 'foo '(-1 3) 5)) (array-shape a) ⇒ ((-1 3) (0 4)) (array-dimensions a) ⇒ ((-1 3) 5)
Return the length of an array: its first dimension. It is an error to ask for the length of an array of rank 0.
Return the rank of array.
Return the rank of array as a
Return a list consisting of all the elements, in order, of array.
Copy every element from vector or array src to the corresponding element of dst. dst must have the same rank as src, and be at least as large in each dimension. The return value is unspecified.
Store fill in every element of array. The value returned is unspecified.
#t if all arguments are arrays with the same shape, the
same type, and have corresponding elements which are either
array-equal?. This function differs from
equal? (see Equality) in that all arguments must be arrays.
Set each element of the dst array to values obtained from calls to proc. The list of src arguments may be empty. The value returned is unspecified.
Each call is
(proc elem …), where each
elem is from the corresponding src array, at the
array-map-in-order! makes the calls in
array-map! makes them in an unspecified order.
The src arrays must have the same number of dimensions as dst, and must have a range for each dimension which covers the range in dst. This ensures all dst indices are valid in each src.
Apply proc to each tuple of elements of src1 src2 …, in row-major order. The value returned is unspecified.
Set each element of the dst array to values returned by calls to proc. The value returned is unspecified.
Each call is
(proc i1 … iN), where
i1…iN is the destination index, one parameter for
each dimension. The order in which the calls are made is unspecified.
For example, to create a 4x4 matrix representing a cyclic group,
/ 0 1 2 3 | 1 2 3 0 | | 2 3 0 1 | \ 3 0 1 2 /
(define a (make-array #f 4 4)) (array-index-map! a (lambda (i j) (modulo (+ i j) 4)))
An additional array function is available in the module
(ice-9 arrays). It can be used with:
(use-modules (ice-9 arrays))
Return a new array with the same elements, type and shape as
src. However, the array increments may not be the same as those of
src. In the current implementation, the returned array will be in
row-major order, but that might change in the future. Use
array-copy! on an array of known order if that is a concern.