Title

Array Notation

Authors

Aubrey Jaffer and Radey Shouman

Status

This SRFI is currently in ``draft'' status. To see an explanation of each status that a SRFI can hold, see here. It will remain in draft status until 2005/01/26, or as amended. To provide input on this SRFI, please mailto:srfi-58@srfi.schemers.org. See instructions here to subscribe to the list. You can access previous messages via the archive of the mailing list.

Abstract

SRFI-47 provides both uniform-typed and heterogeneous multidimensional arrays which subsume Scheme vectors and strings. The notation presented here builds upon Common-Lisp array sytnax, incorporating type abbreviations from SRFI-4, to represent heterogeneous and uniform-typed arrays.

Issues

Common Lisp has a compact notation for rank 1 boolean arrays: #* followed by a string of ones and zeros; 1 for #t, 0 for #f

Rationale

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

Implementations are required to accept all of the type denotations. Those which the platform supports will have platform-dependent representations; the others will be represented as the next larger uniform-type implemented; defaulting to vector if there are none. All implementations must support the character array type, the rank-1 character arrays being strings.

This arrangement has platforms supporting uniform array types using them, with less capable platforms using vectors; both from the same source code.

Specification

Syntax

A syntax for arrays is the list-decomposition of an array with a prefix according to the type of the array. The rank digit `n' is optional for rank 1 arrays. The case of characters is not significant in the prefix characters.

Rank 1 character arrays which are not subarrays are writen as Scheme strings; display treats rank-1 character arrays which are not subarrays identically with strings.

Rank 1 heterogeneous arrays which are not subarrays are writen and displayed as Scheme vectors.

prototype
procedure
element type prefix
rank = 1
prefix
rank = n
vectorconventional vector #A#nA
ac64 double-precision complex#Ac64#nAc64
ac32 single-precision complex#Ac32#nAc32
ar64 double-precision double #Ar64#nAr64
ar32 single-precision float #Ar32#nAr32
as64 64-bit signed integer #As64#nAs64
as32 32-bit signed integer #As32#nAs32
as16 16-bit signed integer #As16#nAs16
as8 8-bit signed integer #As8#nAs8
au64 64-bit unsigned integer #Au64#nAu64
au32 32-bit unsigned integer #Au32#nAu32
au16 16-bit unsigned integer #Au16#nAu16
au8 8-bit unsigned integer #Au8#nAu8
stringchar (string) #A\#nA\
at1 boolean (bit-vector) #At#nAt

A two-by-two array of unsigned 16-bit integers is written:

#2au16((0 1) (2 3))

Implementation

The following code from the SCM implementation ("Init5d9.scm") implements this array read-syntax. read:sharp is called from read when a #\# is read. Its first argument is the character after #\#; the second argument is the input port; the third argument is the procedure to call for recursive reading.

list->uniform-array converts the list-decomposition returned by read into the uniform array of the specified type (or the next larger compatible type).

(define (read:sharp c port read)
  (case c
    ((#\a #\A) (read:array 1 port read))
    ((#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9)
     (let* ((num (read:try-number port c))
            (c (peek-char port)))
       (cond ((memv c '(#\a #\A))
              (read-char port)
              (read:array num port read))
             (else (error "syntax? #" num c)))))
    (else (error "unknown # object" c))))

(define (read:array rank port reader)   ;ignore reader
  (define (bomb pc wid)
    (error (string-append "array syntax? #"
                          (number->string rank)
                          "A" (string pc)
                          (if wid (number->string wid) ""))))
  (list->uniform-array
   rank
   (case (char-downcase (peek-char port))
     ((#\\) (read-char port) #\a)
     ((#\t) (read-char port) #t)
     ((#\c #\r)
      (let* ((pc (read-char port))
             (wid (read:try-number port)))
        (case wid
          ((64 32) (case pc
                     ((#\c) (* +i wid))
                     (else (exact->inexact wid))))
          (else (bomb pc wid)))))
     ((#\s #\u)
      (let* ((pc (read-char port))
             (wid (read:try-number port)))
        (case (or wid (peek-char port))
          ((32 16 8) (case pc
                       ((#\s) (- wid))
                       (else wid)))
          ((#\s #\f #\d #\l) (read-char port) 32)
          (else (bomb pc wid)))))
     (else #f))
   (read port)))

(define (read:try-number port . ic)
  (define chr0 (char->integer #\0))
  (let loop ((arg (and (not (null? ic)) (- (char->integer (car ic)) chr0))))
    (let ((c (peek-char port)))
      (cond ((eof-object? c) #f)
            ((char-numeric? c)
             (loop (+ (* 10 (or arg 0))
                      (- (char->integer (read-char port)) chr0))))
            (else arg)))))

Copyright

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

This document and translations of it may be copied and furnished to others, and derivative works that comment on or otherwise explain it or assist in its implementation may be prepared, copied, published and distributed, in whole or in part, without restriction of any kind, provided that the above copyright notice and this paragraph are included on all such copies and derivative works. However, this document itself may not be modified in any way, such as by removing the copyright notice or references to the Scheme Request For Implementation process or editors, except as needed for the purpose of developing SRFIs in which case the procedures for copyrights defined in the SRFI process must be followed, or as required to translate it into languages other than English.

The limited permissions granted above are perpetual and will not be revoked by the authors or their successors or assigns.

This document and the information contained herein is provided on an "AS IS" basis and THE AUTHOR AND THE SRFI EDITORS DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.


Editor: David Van Horn
Last modified: Fri Nov 26 10:49:37 EST 2004