home *** CD-ROM | disk | FTP | other *** search
Lisp/Scheme | 1988-04-07 | 4.1 KB | 137 lines | [TEXT/ttxt] |
- ;; Larry Mulcahy 1988
- ;; array functions
-
- (provide 'array)
- (require 'iteration "iter")
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; vector-equal
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun vector-equal (v1 v2)
- (and (arrayp v1)
- (arrayp v2)
- (let
- ((big-v1 (length v1))
- (big-v2 (length v2)))
- (and (= big-v1 big-v2)
- (let ((done nil)
- (mismatch nil)
- (index 0))
- (while (and (not done) (not mismatch))
- (setq mismatch (not (equal (aref v1 index)
- (aref v2 index)))
- index (1+ index)
- done (>= index big-v1)))
- (not mismatch))))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; mapvector
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun mapvector (f v)
- (let*
- ((big (length v))
- (result (make-array big)))
- (dotimes (i big)
- (setf (aref result i) (funcall f (aref v i))))
- result))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; list-to-vector
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun list-to-vector (l) (apply #'vector l))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; vector-to-list
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun vector-to-list (v)
- (let ((big (length v))
- (result nil))
- (for i 1 big (push (aref v (- big i)) result))
- result))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; concatenate-two-vectors
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun concatenate-two-vectors (v1 v2)
- (let*
- ((big-v1 (length v1))
- (big-v2 (length v2))
- (big (+ big-v1 big-v2))
- (result (make-array big)))
- (dotimes (i big-v1)
- (setf (aref result i) (aref v1 i)))
- (dotimes (i big-v2)
- (setf (aref result (+ i big-v1)) (aref v2 i)))
- result))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; concatenate-vectors
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun concatenate-vectors (&rest vs)
- (case (length vs)
- (0 nil)
- (1 (car vs))
- (2 (concatenate-two-vectors (car vs) (cadr vs)))
- (t (concatenate-two-vectors (car vs)
- (apply #'concatenate-vectors
- (cdr vs))))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; exchange-vector-elements
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defmacro exchange-vector-elements (v i j)
- (let ((temp-label (gensym)))
- `(let
- ((,temp-label (aref ,v ,i)))
- (setf (aref ,v ,i) (aref ,v ,j))
- (setf (aref ,v ,j) ,temp-label))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; copy-vector
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun copy-vector (v)
- (let* ((big (length v))
- (result (make-array big)))
- (dotimes (i big)
- (setf (aref result i) (aref v i)))
- result))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; vector:position
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; return the 0-origin position of the first occurrence of the
- ; element e in the list l.
- ; If not found, return nil.
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun vector:position (e v)
- (dotimes (i (length v) nil)
- (if (equal e (aref v i))
- (return i))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; vector:position-if
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun vector:position-if (test v)
- (dotimes (i (length v) nil)
- (if (funcall test (aref v i)) (return i))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; vector:position-if-not
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defun vector:position-if-not (test v)
- (dotimes (i (length v) nil)
- (if (not (funcall test (aref v i))) (return i))))
-
-
-