Vectors


 

 

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.

(set *myvector* (@v 1 <>))
<1> 

(limit (value *myvector*))
1

(set *myvector* (@v 0 (value *myvector*)))
<0 1>

(limit (value *myvector*))
2

(@v -1 (value *myvector*))
<-1 0 1>

(limit (value *myvector*))  @v is non-destructive 
2

(<-vector (value *myvector*) 2)
1

(vector-> (value *myvector*) 2 a)
<0 a>

(value *myvector*)  vector-> is destructive, the global is changed
<0 a>
1. Introduction

2. License

3. History

4. The Core Language

4.1 Base Types
4.1.1 Symbols
4.1.2 Strings
4.1.3 Numbers
4.2 Function Applications
4.3 The Top Level
4.4 Arithmetic
4.5 Comments

4.6 Sequences

4.6.1 Lists
4.6.2 Tuples
4.6.3 Vectors

4.7 lambda and let
4.8 Global Assignments
4.9 Higher Order Functions
4.10 Lazy Evaluation
4.11 I/O
4.12 Loading Files
4.13 Streams
4.14 Exceptions
4.15 Hashing
4.16 Property Lists
4.17 Eval

5 Defining Functions

5.1 Partial Functions
5.2 List Handling Functions
5.3 String Handling Functions
5.4 Tuple Handling Functions
5.5 Vector Handling Functions
5.6 Guards
5.7 Backtracking
5.8 Writing in Kλ
5.9 Macros

6. Packages

7. Shen-YACC

7.1 Recognisor Generator
7.2 Semantic Actions

8. Shen Prolog

8.1 Sample Programs

9. Types

9.1 Types and Constructors
9.2 Functions and Types
9.3 Synonyms

10 Sequent Calculus

10.1 Recursive Types

10.2 Exotic Types

10.2.1 Dependent Types
10.2.2 Negative Types
10.2.3 Subtypes
10.2.4 The Type of All Sets

11 Glossary of Functions

12 The Syntax of Shen

Built by Shen Technology (c) Mark Tarver, September 2021