Scheme 48 Manual | Contents | In Chapter: Libraries
Previous: Queues | Next: Records

Arrays

These provide N-dimensional, zero-based arrays and are in the structure arrays. The array interface is derived from one invented by Alan Bawden.

Make-array makes a new array with the given dimensions, each of which must be a non-negative integer. Every element is initially set to value. Array Returns a new array with the given dimensions and elements. Dimensions must be a list of non-negative integers, The number of elements should be the equal to the product of the dimensions. The elements are stored in row-major order.
(make-array 'a 2 3) -> {Array 2 3}

(array '(2 3) 'a 'b 'c 'd 'e 'f)
    -> {Array 2 3}

Copy-array returns a copy of array. The copy is identical to the array but does not share storage with it.

Returns #t if value is an array.

Array-ref returns the specified array element and array-set! replaces the element with value.
(let ((a (array '(2 3) 'a 'b 'c 'd 'e 'f)))
  (let ((x (array-ref a 0 1)))
    (array-set! a 'g 0 1)
    (list x (array-ref a 0 1))))
    -> '(b g)

Array->vector returns a vector containing the elements of array in row-major order. Array-dimensions returns the dimensions of the array as a list.

Make-shared-array makes a new array that shares storage with array and uses linear-map to map indexes to elements. Linear-map must accept as many arguments as the number of dimensions given and must return a list of non-negative integers that are valid indexes into array. <
(array-ref (make-shared-array a f i0 i1 ...)
           j0 j1 ...)
is equivalent to
(apply array-ref a (f j0 j1 ...))

As an example, the following function makes the transpose of a two-dimensional array:

(define (transpose array)
  (let ((dimensions (array-dimensions array)))
    (make-shared-array array
                       (lambda (x y)
                         (list y x))
                       (cadr dimensions)
                       (car dimensions))))

(array->vector
  (transpose
    (array '(2 3) 'a 'b 'c 'd 'e 'f)))
      -> '(a d b e c f)

Previous: Queues | Next: Records