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)
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Zero))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array 0)
> (array #[0 1 2 3])
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Byte))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #[0 1 2 3])
> (array #[#[1 2 3] #[4 5 6]])
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Positive-Byte))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(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) - : (List One Positive-Byte Positive-Byte)
'(1 2 3)
> (array #[1 2 3])
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Positive-Byte))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(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)
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Real))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #[1 2 3])
> (ann (array #[1 2 3]) (Array Real))
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Real))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #[1 2 3])
> #(this is okay) - : (Immutable-Vector 'this 'is 'okay)
'#(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])
- : #(struct:Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes (U 'is 'okay 'this)))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #['this 'is 'okay])
> (array #['#(an) '#(array) '#(of) '#(vectors)])
- : #(struct:Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes
(U (Immutable-Vector 'an)
(Immutable-Vector 'array)
(Immutable-Vector 'of)
(Immutable-Vector 'vectors))))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(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
- : #(struct:Mutable-Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes Integer)
(-> Indexes Integer Void)
(Vectorof Integer))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-mutable-array.rkt:14:13 prop:custom-write>)
(mutable-array #[0 1 2 3])
> (array-set! arr #(0) 10) > arr
- : #(struct:Mutable-Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes Integer)
(-> Indexes Integer Void)
(Vectorof Integer))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-mutable-array.rkt:14:13 prop:custom-write>)
(mutable-array #[10 1 2 3])
> (define arr (mutable-array #[0 1 2 3] : Real)) > arr
- : #(struct:Mutable-Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes Real)
(-> Indexes Real Void)
(Vectorof Real))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-mutable-array.rkt:14:13 prop:custom-write>)
(mutable-array #[0 1 2 3])
> (array-set! arr #(0) 10.0) > arr
- : #(struct:Mutable-Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes Real)
(-> Indexes Real Void)
(Vectorof Real))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-mutable-array.rkt:14:13 prop:custom-write>)
(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)
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Positive-Byte))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array 5)
> (make-array #(1 2) 'sym)
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes 'sym))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #[#['sym 'sym]])
> (make-array #(4 0 2) "Invisible")
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes String))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(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 #())
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Indexes))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array '#())
> (indexes-array #(4))
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Indexes))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #['#(0) '#(1) '#(2) '#(3)])
> (indexes-array #(2 3))
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Indexes))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #[#['#(0 0) '#(0 1) '#(0 2)] #['#(1 0) '#(1 1) '#(1 2)]])
> (indexes-array #(4 0 2))
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Indexes))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #[#[] #[] #[] #[]])
procedure
(index-array ds) → (Array Index)
ds : In-Indexes
> (index-array #(2 3))
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Index))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #[#[0 1 2] #[3 4 5]])
> (array-flatten (index-array #(2 3)))
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Index))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(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)
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Index))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #[#[0 0 0] #[1 1 1] #[2 2 2]])
> (axis-index-array #(3 3) 1)
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Index))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(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)
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes (U One Zero)))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(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]])