Tomislav Mutak <mutakti@rose-hulman.edu> 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)))
new))
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
-----
Rob Warnock, 31-2-510 rpw3@sgi.com
SGI Network Engineering http://reality.sgi.com/rpw3/
1600 Amphitheatre Pkwy. Phone: 650-933-1673
Mountain View, CA 94043 PP-ASEL-IA