You are looking at historical revision 8240 of this page. It may differ significantly from its current revision.

This is a very simple two dimensional sparse array using sparse-vector. I find I occasionally want large arrays but very seldom need more than two dimensions. For multi-dimension arrays see: array-lib

(use sparse-vectors)

; this is a simple two dimensional sparse array
; ONLY TWO DIMENSIONS!!! SEE ARRAY-LIB IF YOUR NEEDS ARE GREATER!!
;

(define (make-sparse-array)

 (let ((a (make-sparse-vector)))
   (sparse-vector-set! a 0 (make-sparse-vector))
   a))

(define (sparse-array? a)

 (and (sparse-vector? a)
      (sparse-vector? (sparse-vector-ref a 0))))

(define (sparse-array-ref a x y)

 (let ((row (sparse-vector-ref a x)))
   (if row
(sparse-vector-ref row y)
#f)))

(define (sparse-array-set! a x y val)

 (let ((row (sparse-vector-ref a x)))
   (if row
(sparse-vector-set! a y val)
(let ((new-row (make-sparse-vector)))
  (sparse-vector-set! a x new-row)
  (sparse-vector-set! new-row y val)))))