Subject: Re: copying a vector?
From: (Rob Warnock)
Date: 24 Dec 2000 03:46:04 GMT
Newsgroups: comp.lang.scheme
Message-ID: <923rhs$eve1d$>
Tomislav Mutak <> wrote:
| I'm trying to make a distinct copy of a vector...
|    (list->vector (vector->list original))
| I just know there is a better/more efficient way to do this...?

Well, rather than spread that idiom throughout your code, I'd at least say:

	(define (vector-copy v)
	  (list->vector (vector->list v)))

But, yes, that probably *is* the simplest/best way using the primitives
provided in the R5RS spec, since there's no "vector-copy" there. I suppose
you could that version with this one:

	(define (vector-copy old)
	  (let* ((len (vector-length old))
	         (new (make-vector len)))
	    (do ((i 0 (+ i 1)))
		((>= i len))
	      (vector-set! new i (vector-ref old i)))

However, this may or may not be faster and/or more space-efficient,
depending on the details of your system's garbage collector (e.g., is
it a generational/copying collector? -- in which case using mutation
might slow down future collections) or compiler (which might recognize
that idiom) or library (which might already have a "vector-copy" in it).


Rob Warnock, 31-2-510
SGI Network Engineering
1600 Amphitheatre Pkwy.		Phone: 650-933-1673
Mountain View, CA  94043	PP-ASEL-IA