Vectors
Vectors are heterogenous structures whose elements are indexed by integers. A
vector typically occupies less space than a list of the same length, and the
average time required to access a randomly chosen element is typically less for
the vector than for the list.
The length of a vector is the number of elements that it contains. This number
is a non-negative integer that is fixed when the vector is created. The valid
indexes of a vector are the exact non-negative integers less than the length of
the vector. The first element in a vector is indexed by zero, and the last
element is indexed by one less than the length of the vector.
Vectors are written using the notation #(obj ...). For example, a vector
of length 3 containing the number zero in element 0, the list (2 2 2 2) in
element 1, and the string "Anna" in element 2 can be written as following:
#(0 (2 2 2 2) "Anna")
Note: In STKLOS, vectors constants don't need to be quoted.
Returns #t if obj is a vector, otherwise returns #f.
|
| make-vector k
|
R5RS
|
| make-vector k fill
|
R5RS
|
Returns a newly allocated vector of k elements. If a second argument is
given, then each element is initialized to fill. Otherwise the initial
contents of each element is unspecified.
|
Returns a newly allocated vector whose elements contain the given arguments.
Analogous to list.
(vector 'a 'b 'c) => #(a b c)
|
| vector-length vector
|
R5RS
|
Returns the number of elements in vector as an exact integer.
|
k must be a valid index of vector. Vector-ref returns the contents of
element k of vector.
(vector-ref '#(1 1 2 3 5 8 13 21)
5) => 8
(vector-ref '#(1 1 2 3 5 8 13 21)
(let ((i (round (* 2 (acos -1)))))
(if (inexact? i)
(inexact->exact i)
i))) => 13
|
| vector-set! vector k obj
|
R5RS
|
k must be a valid index of vector. Vector-set! stores obj in element
k of vector. The value returned by vector-set! is void.
(let ((vec (vector 0 '(2 2 2 2) "Anna")))
(vector-set! vec 1 '("Sue" "Sue"))
vec) => #(0 ("Sue" "Sue") "Anna")
(vector-set! '#(0 1 2) 1 "doe") => error ; constant vector
|
| vector->list vector
|
R5RS
|
| list->vector list
|
R5RS
|
Vector->list returns a newly allocated list of the objects contained in
the elements of vector. List->vector returns a newly created vector
initialized to the elements of the list list.
(vector->list '#(dah dah didah)) => (dah dah didah)
(list->vector '(dididit dah)) => #(dididit dah)
|
| vector-fill! vector fill
|
R5RS
|
Stores fill in every element of vector. The value returned by
vector-fill! is void.
|
| vector-copy v
|
STKLOS Procedure |
Return a copy of vectot v. Note that, if v is a constant vector,
its copy is not constant.
|
| vector-resize v size
|
STKLOS Procedure |
| vector-resize v size fill
|
STKLOS Procedure |
Returns a copy of v of the given size. If size is greater
than the vector size of v, the contents of the newly allocated vector cells
is set to the value of fill. If fill is omitted the content of the
new cells is void.
|
| vector-mutable? obj
|
STKLOS Procedure |
Returns #t if obj is a mutable vector, otherwise returns #f.
(vector-mutable? '#(1 2 a b)) => #f
(vector-mutable? (vector-copy '#(1 2))) => #t
(vector-mutable? (vector 1 2 3)) => #t
(vector-mutable? 12) => #f
|
| sort obj predicate
|
STKLOS Procedure |
Obj must be a list or a vector. Sort returns a copy of obj sorted
according to predicate. Predicate must be a procedure which takes
two arguments and returns a true value if the first argument is strictly
"before" the second.
(sort '(1 2 -4 12 9 -1 2 3) <)
=> (-4 -1 1 2 2 3 9 12)
(sort '#("one" "two" "three" "four")
(lambda (x y) (> (string-length x) (string-length y))))
=> '#("three" "four" "one" "two")
|