Title

Array

Author

Aubrey Jaffer

Status

This SRFI is currently in final status. Here is an explanation of each status that a SRFI can hold. To provide input on this SRFI, please send email to srfi-47@nospamsrfi.schemers.org. To subscribe to the list, follow these instructions. You can access previous messages via the mailing list archive.

This SRFI has been superseded by SRFI-63, "Homogeneous and Heterogeneous Arrays".

Abstract

"slib/array.scm" synthesizes array ideas from Common-Lisp and Alan Bawden with homogeneous vector ideas from SRFI-4 and SCM. The result portably integrates homogeneous and heterogeneous arrays into Scheme.

Issues

Rationale

"Array" incorporates all the homogeneous vector types from SRFI-4 "Homogeneous numeric vector datatypes", and adds complex types composed of two 32-bit or two 64-bit floating-point numbers, a uniform character array type, and a uniform boolean array type. Multi-dimensional uniform-arrays subsume homogeneous vectors as the one-dimensional case, obviating the need for SRFI-4.

Implementations are required to define all of the prototype procedures. Those which the platform supports will have platform-dependent definitions; the others will be defined identically to the next larger prototype implemented; defaulting to vector if there are none. All implementations must support the string array type using a string.

This arrangement has platforms which support uniform array types employing them, with less capable platforms using vectors; but all working compatibly.

Conversions

Specification

Function: array? obj

Returns #t if the obj is an array, and #f if not.

Note: Arrays are not disjoint from other Scheme types. Strings and vectors also satisfy array?. A disjoint array predicate can be written:
(define (strict-array? obj)
  (and (array? obj) (not (string? obj)) (not (vector? obj))))

Function: equal? obj1 obj2

`Equal?' recursively compares the contents of pairs, vectors, strings, and arrays, applying `eqv?' on other objects such as numbers and symbols. A rule of thumb is that objects are generally `equal?' if they print the same. `Equal?' may fail to terminate if its arguments are circular data structures.

(equal? 'a 'a)                         ==>  #t
(equal? '(a) '(a))                     ==>  #t
(equal? '(a (b) c)
        '(a (b) c))                    ==>  #t
(equal? "abc" "abc")                   ==>  #t
(equal? 2 2)                           ==>  #t
(equal? (make-vector 5 'a)
        (make-vector 5 'a))            ==>  #t
(equal? (make-array (Au32 4) 5 3)
        (make-array (Au32 4) 5 3))     ==>  #t
(equal? (lambda (x) x)
        (lambda (y) y))                ==>  unspecified

Function: make-array prototype k1 k2 ...

Creates and returns an array of type prototype with dimensions k1, k2, ... and filled with elements from prototype. prototype must be an array, vector, or string. The implementation-dependent type of the returned array will be the same as the type of prototype. except if that would be a vector or string with more than one dimension, in which case some variety of array will be returned.

If the prototype has no elements, then the initial contents of the returned array are unspecified. Otherwise, the returned array will be filled with the element at the origin of prototype.

(make-array '#(foo) 2 3) => #2A((foo foo foo) (foo foo foo))

These functions return a prototypical uniform-array enclosing the optional argument (which must be of the correct type). If the uniform-array type is supported by the implementation, then it is returned; defaulting to the next larger precision type; resorting finally to vector.

Function: ac64 z

Function: ac64
Returns a high-precision complex uniform-array prototype.

Function: ac32 z

Function: ac32
Returns a complex uniform-array prototype.

Function: ar64 x

Function: ar64
Returns a high-precision real uniform-array prototype.

Function: ar32 x

Function: ar32
Returns a real uniform-array prototype.

Function: as64 n

Function: as64
Returns an exact signed integer uniform-array prototype with at least 64 bits of precision.

Function: as32 n

Function: as32
Returns an exact signed integer uniform-array prototype with at least 32 bits of precision.

Function: as16 n

Function: as16
Returns an exact signed integer uniform-array prototype with at least 16 bits of precision.

Function: as8 n

Function: as8
Returns an exact signed integer uniform-array prototype with at least 8 bits of precision.

Function: au64 k

Function: au64
Returns an exact non-negative integer uniform-array prototype with at least 64 bits of precision.

Function: au32 k

Function: au32
Returns an exact non-negative integer uniform-array prototype with at least 32 bits of precision.

Function: au16 k

Function: au16
Returns an exact non-negative integer uniform-array prototype with at least 16 bits of precision.

Function: au8 k

Function: au8
Returns an exact non-negative integer uniform-array prototype with at least 8 bits of precision.

Function: at1 bool

Function: at1
Returns a boolean uniform-array prototype.

Function: make-shared-array array mapper k1 k2 ...

make-shared-array can be used to create shared subarrays of other arrays. The mapper is a function that translates coordinates in the new array into coordinates in the old array. A mapper must be linear, and its range must stay within the bounds of the old array, but it can be otherwise arbitrary. A simple example:

(define fred (make-array '#(#f) 8 8))
(define freds-diagonal
  (make-shared-array fred (lambda (i) (list i i)) 8))
(array-set! freds-diagonal 'foo 3)
(array-ref fred 3 3)
   => FOO
(define freds-center
  (make-shared-array fred (lambda (i j) (list (+ 3 i) (+ 3 j)))
                     2 2))
(array-ref freds-center 0 0)
   => FOO

Function: array-rank obj

Returns the number of dimensions of obj. If obj is not an array, 0 is returned.

Function: array-dimensions array

Returns a list of dimensions.

(array-dimensions (make-array '#() 3 5))
   => (3 5)

Function: array-in-bounds? array index1 index2 ...

Returns #t if its arguments would be acceptable to array-ref.

Function: array-ref array index1 index2 ...

Returns the (index1, index2, ...) element of array.

Procedure: array-set! array obj index1 index2 ...

Stores obj in the (index1, index2, ...) element of array. The value returned by array-set! is unspecified.

Implementation

slib/array.scm implements array procedures for R4RS or R5RS compliant Scheme implementations with records as implemented by slib/record.scm or SRFI-9.

Copyright

Copyright (C) Aubrey Jaffer (2003, 2004). All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Editor: David Rush
Last modified: Sun Jan 28 13:40:35 MET 2007