Vector Append

How to create a vector append procedure in Scheme.

How to create a vector append procedure in Scheme.

Date Created:Sunday October 26th, 2008 05:24 PM
Date Modified:Sunday October 26th, 2008 05:26 PM

(define (vector-append x y)
  (define (loop newvec ref-vec index stopper offset)
   (if (= index stopper)
       (let (
              (new-val (if (= offset 0)
                           (vector-ref ref-vec index)                                            
                           (vector-ref ref-vec (- index offset))
        (vector-set! newvec index new-val)
        (loop newvec ref-vec (- index 1) stopper offset)
  (let* ((x-len (vector-length x))
         (y-len (vector-length y))
         (len (+ x-len y-len))
         (new-vec (make-vector len 'new-vec))
         (y-end (- len 1))
         (y-stop (- y-end y-len))
         (x-end y-stop)
         (x-stop -1)
         (loop new-vec y y-end y-stop x-len)
         (loop new-vec x x-end x-stop 0)

(define v1 (make-vector 5 'five))
(define v2 (make-vector 7 'seven))

(vector-append v1 v2)

(define v1 (vector 1 2 3 4 5 6 7 8 9 10 11 12))
(define v2 (vector 'a 'b 'c 'd 'e 'f 'g))

(print "Append v1 and v2 and then v2 and v1:")
(print (vector-append v1 v2))
(print (vector-append v2 v1))

(print "vector append the last two vectors created:")
(print (vector-append (vector-append v1 v2)  (vector-append v2 v1)))

Download: vector-append.scm 1 KB

Please login or Click Here to register for downloads
Creative Commons License
Vector Append by Dan Lynch
is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
Based on a work at
Permissions beyond the scope of this license may be available at