# extending sequence functions to arrays

• To: common-lisp@SAIL.STANFORD.EDU
• Subject: extending sequence functions to arrays
• From: Dave.Touretzky@C.CS.CMU.EDU
• Date: Wed 28 Jan 87 19:59:30-EST

```I propose to make sequence functions more useful by extending them (very
slightly) to treat arrays of rank > 1 as if they were one dimensional vectors
:DISPLACED-TO the array.  Presently, programmers are forced to build displaced
vectors by hand in order to do simple operations on matrices.  Consider the
following examples:

(setq a (make-array (list 5 7)))		;the matrix A
(setq b (make-array (list 5 7)))		;the matrix B
(setq av (make-array 35 :displaced-to a))	;hack for accessing A
(setq bv (make-array 35 :displaced-to b))	;hack for accessing B

Current Code				New Code       Equivalent Hack
1.  zero all array elements

(dotimes (i 5)			(fill a 0)  ==  (fill av 0)
(dotimes (j 7)
(setf (aref a i j) 0)))

2.  copy array A into array B
...the obvious nested dotimes...	(replace b a)  == (replace bv av)

3.  check if A contains a 0 anywhere
(block foo				(find 0 a)  == (find 0 av)
(dotimes (i 5)
(dotimes (j 7)
(if (= (aref a i j) 0)
(return-from foo t)))))

4.  find the minimum element of A
(let ((m (aref a 0 0)))		(reduce #'min a) == (reduce #'min av)
(dotimes (i 5)
(dotimes (j 7)
(setq m (min m (aref a i j)))))
m)

This proposal doesn't impose any significant work on implementors since
displaced arrays are already part of the language.  It also does not introduce
any conceptual ambiguities, e.g., in the handling of :START and :END keywords,
values returned by, LENGTH, ELT, and REVERSE, etc., since the behavior of
sequence functions on vectors is already well-defined.

-- Dave Touretzky, CMU Computer Science Dept.
-------

```