6.7 Construction
syntax
(array #[#[...] ...] maybe-type-ann)
maybe-type-ann =
| : type
The vector syntax #[...] delimits rows. These may be nested to any depth, and must have a rectangular shape. Using square parentheses is not required, but is encouraged to help visually distinguish array contents from array indexes and other vectors. (See the examples for indexes-array for an illustration.)
> (array 0) - : (Array Zero)
(array 0)
> (array #[0 1 2 3]) - : (Array Byte)
(array #[0 1 2 3])
> (array #[#[1 2 3] #[4 5 6]]) - : (Array Positive-Byte)
(array #[#[1 2 3] #[4 5 6]])
> (array #[#[1 2 3] #[4 5]]) eval:121:0: array: expected rectangular data
at: #(#(1 2 3) #(4 5))
in: (array/syntax array list unsafe-list->array #(#(1 2 3)
#(4 5)))
> (list 1 2 3) - : (Listof Positive-Byte) [more precisely: (List One Positive-Byte Positive-Byte)]
'(1 2 3)
> (array #[1 2 3]) - : (Array Positive-Byte)
(array #[1 2 3])
> ((inst list Real) 1 2 3) - : (Listof Real)
'(1 2 3)
> ((inst array Real) #[1 2 3]) eval:125:0: array: not allowed as an expression
in: array
> (array #[1 2 3] : Real) - : (Array Real)
(array #[1 2 3])
> (ann (array #[1 2 3]) (Array Real)) - : (Array Real)
(array #[1 2 3])
> #(this is okay) - : (Vector Symbol Symbol Symbol)
'#(this is okay)
> (array #[not okay]) eval:129:0: Type Checker: missing type for top-level
identifier;
either undefined or missing a type annotation
identifier: okay
in: #(not okay)
> (array #['this 'is 'okay]) - : (Array (U 'this 'is 'okay))
(array #['this 'is 'okay])
> (array #['#(an) '#(array) '#(of) '#(vectors)]) - : (Array (Vector Symbol))
(array #['#(an) '#(array) '#(of) '#(vectors)])
Arrays returned by array are strict. Another way to create immutable, strict arrays from literal data is to use list->array.
syntax
(mutable-array #[#[...] ...] maybe-type-ann)
maybe-type-ann =
| : type
> (define arr (mutable-array #[0 1 2 3]))
> arr - : (Mutable-Array Integer)
(mutable-array #[0 1 2 3])
> (array-set! arr #(0) 10)
> arr - : (Mutable-Array Integer)
(mutable-array #[10 1 2 3])
> (define arr (mutable-array #[0 1 2 3] : Real))
> arr - : (Mutable-Array Real)
(mutable-array #[0 1 2 3])
> (array-set! arr #(0) 10.0)
> arr - : (Mutable-Array Real)
(mutable-array #[10.0 1 2 3])
Another way to create mutable arrays from literal data is to use vector->array.
procedure
(make-array ds value) → (Array A)
ds : In-Indexes value : A
> (make-array #() 5) - : (Array Positive-Byte)
(array 5)
> (make-array #(1 2) 'sym) - : (Array 'sym)
(array #[#['sym 'sym]])
> (make-array #(4 0 2) "Invisible") - : (Array String)
(array #[#[] #[] #[] #[]])
procedure
(build-array ds proc) → (Array A)
ds : In-Indexes proc : (Indexes -> A)
procedure
(array->mutable-array arr) → (Mutable-Array A)
arr : (Array A)
procedure
(mutable-array-copy arr) → (Mutable-Array A)
arr : (Mutable-Array A)
procedure
(indexes-array ds) → (Array Indexes)
ds : In-Indexes
> (indexes-array #()) - : (Array Indexes)
(array '#())
> (indexes-array #(4)) - : (Array Indexes)
(array #['#(0) '#(1) '#(2) '#(3)])
> (indexes-array #(2 3)) - : (Array Indexes)
(array #[#['#(0 0) '#(0 1) '#(0 2)] #['#(1 0) '#(1 1) '#(1 2)]])
> (indexes-array #(4 0 2)) - : (Array Indexes)
(array #[#[] #[] #[] #[]])
procedure
(index-array ds) → (Array Index)
ds : In-Indexes
> (index-array #(2 3)) - : (Array Index)
(array #[#[0 1 2] #[3 4 5]])
> (array-flatten (index-array #(2 3))) - : (Array Index)
(array #[0 1 2 3 4 5])
procedure
(axis-index-array ds axis) → (Array Index)
ds : In-Indexes axis : Integer
> (axis-index-array #(3 3) 0) - : (Array Index)
(array #[#[0 0 0] #[1 1 1] #[2 2 2]])
> (axis-index-array #(3 3) 1) - : (Array Index)
(array #[#[0 1 2] #[0 1 2] #[0 1 2]])
> (axis-index-array #() 0) axis-index-array: contract violation
expected: Index < 0
given: 0
argument position: 2nd
other arguments...:
'#()
procedure
(diagonal-array dims axes-length on-value off-value) → (Array A) dims : Integer axes-length : Integer on-value : A off-value : A
> (diagonal-array 2 7 1 0) - : (Array (U Zero One))
(array
#[#[1 0 0 0 0 0 0]
#[0 1 0 0 0 0 0]
#[0 0 1 0 0 0 0]
#[0 0 0 1 0 0 0]
#[0 0 0 0 1 0 0]
#[0 0 0 0 0 1 0]
#[0 0 0 0 0 0 1]])