6.7. Vectors


6.7. Vectors

Vectors are more convenient and efficient than lists for some applications. Whereas accessing an arbitrary element in a list requires a linear traversal of the list up to the selected element, arbitrary vector elements are accessed in constant time. The length of a vector in Scheme is the number of elements it contains. Vectors are indexed by exact nonnegative integers, and the index of the first element of any vector is 0. The highest valid index for a given vector is one less than its length.

As with lists, the elements of a vector may be of any type; a single vector may even hold more than one type of object.

A vector is written as a sequence of objects separated by whitespace, preceded by the prefix #( and followed by ). For example, a vector consisting of the elements a, b, and c would be written #(a b c).

(vector obj ...)

procedure

returns: a vector of the objects obj ...

 (vector)  #() (vector 'a 'b 'c)  #(a b c) 

(make-vector n)

procedure

(make-vector n obj )

procedure

returns: a vector of length n

n must be an exact nonnegative integer. If obj is supplied, each element of the vector is filled with obj ; otherwise, the elements are unspecified.

 (make-vector 0)  #() (make-vector 0 'a)  #() (make-vector 5 'a)  #(a a a a a) 

(vector-length vector)

procedure

returns: the number of elements in vector

The length of a vector is always an exact nonnegative integer.

 (vector-length '#())  0 (vector-length '#(a b c))  3 (vector-length (vector 1 2 3 4))  4 (vector-length (make-vector 300))  300 

(vector-ref vector n)

procedure

returns: the nth element (zero-based) of vector

n must be an exact nonnegative integer strictly less than the length of vector.

 (vector-ref '#(a b c) 0)  a (vector-ref '#(a b c) 1)  b (vector-ref '#(x y z w) 3)  w 

(vector-set! vector n obj )

procedure

returns: unspecified

n must be an exact nonnegative integer strictly less than the length of vector. vector-set! changes the nth element of vector to obj .

 (let ((v (vector 'a 'b 'c 'd 'e)))    (vector-set! v 2 'x)    v)  #(a b x d e) 

(vector-fill! vector obj )

procedure

returns: unspecified

vector-fill! replaces each element of vector with obj . vector-fill! appears in the Revised5 Report but not in the ANSI/IEEE standard. It may be defined as follows.

 (define vector-fill!    (lambda (v x)       (let ((n (vector-length v)))          (do ((i 0 (+ i 1)))              ((= i n))              (vector-set! v i x))))) (let ((v (vector 1 2 3)))    (vector-fill! v 0)    v)  #(0 0 0) 

(vector->list vector)

procedure

returns: a list of the elements of vector

vector->list provides a convenient method for applying list-processing operations to vectors. vector->list appears in the Revised5 Report but not in the ANSI/IEEE standard. It may be defined as follows.

 (define vector->list    (lambda (s)       (do ((i (- (vector-length s) 1) (- i 1))            (ls '() (cons (vector-ref s i) ls)))           ((< i 0) ls)))) (vector->list (vector))  () (vector->list '#(a b c))  (a b c) (let ((v '#(1 2 3 4 5))) (apply * (vector->list v)))  120 

(list->vector list)

procedure

returns: a vector of the elements of list

list->vector is the functional inverse of vector->list. The two procedures are often used in combination to take advantage of a list-processing operation. A vector may be converted to a list with vector->list, this list processed in some manner to produce a new list, and the new list converted back into a vector with list->vector.

list->vector appears in the Revised5 Report but not in the ANSI/IEEE standard. It may be defined as follows.

 (define list->vector    (lambda (ls)       (let ((s (make-vector (length ls))))          (do ((ls ls (cdr ls)) (i 0 (+ i 1)))              ((null? ls) s)              (vector-set! s i (car ls)))))) (list->vector '())  #() (list->vector '(a b c))  #(a b c) (let ((v '#(1 2 3 4 5)))    (let ((ls (vector->list v)))       (list->vector (map * ls ls))))  #(1 4 9 16 25) 




The Scheme Programming Language
The Scheme Programming Language
ISBN: 026251298X
EAN: 2147483647
Year: 2003
Pages: 98

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net