4.6.3 Vectors
TBoS p.99-112, SD
'(vector n)' creates a (standard) vector with n elements numbered from 1 to n. For standard vectors the zeroth
address holds an integer that gives the size of the vector. Certain special vectors called print vectors can be created
that use this zeroth index for other purposes (see SD reference). The shortest standard vector is created by expression
'(vector 0)' which creates a standard vector whose zeroth address contains the object zero. This called the
empty vector and Shen permits the user to write <> as shorthand for the empty vector.
Vectors are printed out as <...> where ... are the contents of the vector.
'(<-vector v n)' accesses the nth (n >= 1) element of vector v. Note that this will raise an error if no object
has been placed in the nth index.
The function limit accesses the 0th element of a vector v and gives the size of v.
Shen vectors are mutable. '(vector-> v n x)' destructively modifies v by placing x in the nth address of v.
A 2-dimensional array is simply a vector of vectors. The non-destructive operation '(@v x v)' creates a new
vector v' whose tail is the same as v and whose head is x. @v is polyadic '(@v a b ... n v)' adds n elements in order
to a vector V, copying it and creating a new vector.
(0-) (set *myvector* (@v 1 <>))
<1>
(1-) (limit (value *myvector*))
1
(2-) (set *myvector* (@v 0 (value *myvector*)))
<0 1>
(3-) (limit (value *myvector*))
2
(4-) (@v -1 (value *myvector*))
<-1 0 1>
(5-) (limit (value *myvector*)) @v is non-destructive
2
(6-) (<-vector (value *myvector*) 2)
1
(7-) (vector-> (value *myvector*) 2 a)
<0 a>
(8-) (value *myvector*) vector-> is destructive, the global is changed
<0 a> |
|