Subject: Re: vector-range ?
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 23 Oct 2000 01:14:49 GMT
Newsgroups: comp.lang.scheme
Message-ID: <8t03e9$vd01$1@fido.engr.sgi.com>
Michael Livshin  <mlivshin@yahoo.com> wrote:
+---------------
| davidw@linuxcare.com (David N. Welton) writes:
| > A vector-range function would be useful.  Something like
| > (vector-range vector start end), returning either another vector
| > or a list.
...
| write a nice clean proposal and submit it as SRFI
| (see <http://srfi.schemers.org/>).
...
| actual _standard_ _library_ for Scheme looks like an impossible
| target.  if you want a useful Lispy language, use Common Lisp -- it's
| OK, really.
+---------------

Yup. For example, what [I suspect] David's asking for is already
provided for (indeed, required) by the Common Lisp standard:

	> (defvar v1 #(1 2 3 4 5 6 7 8 9))
	> (defvar v2 (make-array 5 :displaced-to v1
			           :displaced-index-offset 2))
	> v2
	#(3 4 5 6 7)
	>

Of course, this *shares* structure with the original array:

	> (setf (aref v2 3) 666)
	666
	> v2
	#(3 4 5 666 7)
	> v1
	#(1 2 3 4 5 666 7 8 9)
	> 

If sharing wasn't your intent, you can always make a copy instead.
Common Lisp gives you a large number of ways to do this, depending
on your style or intention, but the simplest is probably "subseq":

	> (defvar v3 #(1 2 3 4 5 6 7 8 9))    
	> (defvar v4 (subseq v3 2 7))
	> v4
	#(3 4 5 6 7)
	> (setf (aref v4 3) 666)
	666
	> v4
	#(3 4 5 666 7)
	> v3 
	#(1 2 3 4 5 6 7 8 9)
	>

Other ways include using "copy-seq" or "make-array" with an
":initial-contents" argument on a displaced array:

	> (defvar v5 (make-array 5 :displaced-to v3
			           :displaced-index-offset 2))
	> (defvar v6 (copy-seq v5))
	> (defvar v7 (make-array 5 :initial-contents v5))
	> (setf (aref v7 2) 555)
	555
	> v7
	#(3 4 555 6 7)
	> v3
	#(1 2 3 4 5 6 7 8 9)
	> 


-Rob

-----
Rob Warnock, 31-2-510		rpw3@sgi.com
Network Engineering		http://reality.sgi.com/rpw3/
Silicon Graphics, Inc.		Phone: 650-933-1673
1600 Amphitheatre Pkwy.		PP-ASEL-IA
Mountain View, CA  94043