6.1 Quick Start

Arrays can be created from expressions denoting each element’s value using the array macro:
> (array #[0 1 2 3 4])

- : (Array Byte)

(array #[0 1 2 3 4])

> (array #[#['first 'row 'data] #['second 'row 'data]])

- : (Array (U 'first 'row 'data 'second))

(array #[#['first 'row 'data] #['second 'row 'data]])

> (array "This array has zero axes and one element")

- : (Array String)

(array "This array has zero axes and one element")

They can also be created using build-array to specify a shape and procedure:
> (define arr
    (build-array #(4 5) (λ: ([js : Indexes])
                          (match-define (vector j0 j1) js)
                          (+ j0 j1))))
> arr

eval:9:0: Type Checker: missing type for top-level

identifier;

 either undefined or missing a type annotation

  identifier: arr1

  in: arr

Other ways to create arrays are to convert them from lists and vectors using list->array, list*->array, vector->array and vector*->array, and to generate them in a loop using for/array: and for*/array:.

Arrays can be indexed using array-ref, and settable arrays can be mutated using array-set!:
> (array-ref arr #(2 3))

eval:10:0: Type Checker: missing type for top-level

identifier;

 either undefined or missing a type annotation

  identifier: arr1

  in: #(2 3)

> (define brr (array->mutable-array arr))

eval:11:0: Type Checker: missing type for top-level

identifier;

 either undefined or missing a type annotation

  identifier: arr1

  in: arr

> (array-set! brr #(2 3) -1000)

eval:12:0: Type Checker: missing type for top-level

identifier;

 either undefined or missing a type annotation

  identifier: brr

  in: -1000

> brr

eval:13:0: Type Checker: missing type for top-level

identifier;

 either undefined or missing a type annotation

  identifier: brr

  in: brr

However, both of these activities are discouraged in favor of functional, whole-array operations.

Arrays can be mapped over and otherwise operated on pointwise:
> (array-map (λ: ([n : Natural]) (* 2 n)) arr)

eval:14:0: Type Checker: missing type for top-level

identifier;

 either undefined or missing a type annotation

  identifier: arr1

  in: arr

> (array+ arr arr)

eval:15:0: Type Checker: missing type for top-level identifier;

 either undefined or missing a type annotation

  identifier: arr1

  in: arr

  context...:

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/utils/tc-utils.rkt:123:0: report-all-errors

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/typecheck/tc-toplevel.rkt:515:0: tc-toplevel-form

   fail-to-succeed

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/tc-setup.rkt:39:0: tc-setup

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/typed-racket.rkt:24:4

   /Users/mflatt/build/macro/racket/collects/racket/private/more-scheme.rkt:148:2: call-with-break-parameterization

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/sandbox-lib/racket/sandbox.rkt:837:5: loop

eval:15:0: Type Checker: missing type for top-level identifier;

 either undefined or missing a type annotation

  identifier: arr1

  in: arr

  context...:

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/utils/tc-utils.rkt:123:0: report-all-errors

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/typecheck/tc-toplevel.rkt:515:0: tc-toplevel-form

   fail-to-succeed

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/tc-setup.rkt:39:0: tc-setup

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/typed-racket.rkt:24:4

   /Users/mflatt/build/macro/racket/collects/racket/private/more-scheme.rkt:148:2: call-with-break-parameterization

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/sandbox-lib/racket/sandbox.rkt:837:5: loop

Type Checker: Summary: 2 errors encountered

When arrays have different shapes, they can often be broadcast, or stretched, to be the same shape before applying the pointwise operation:
> (array* arr (array 2))

eval:16:0: Type Checker: missing type for top-level

identifier;

 either undefined or missing a type annotation

  identifier: arr1

  in: 2

> (array* arr (array #[0 2 0 2 0]))

eval:17:0: Type Checker: missing type for top-level

identifier;

 either undefined or missing a type annotation

  identifier: arr1

  in: #(0 2 0 2 0)

By default, zero-dimensional arrays like (array 2) can be broadcast to any shape. See Broadcasting for details.

Arrays can be sliced to yield sub-arrays, using a list of slice specifications that correspond to array axes. For example, keeping every row of arr and every even-numbered column:
> (array-slice-ref arr (list (::) (:: 0 5 2)))

eval:18:0: Type Checker: missing type for top-level

identifier;

 either undefined or missing a type annotation

  identifier: arr1

  in: 2

Here, :: has semantics almost, but not quite, entirely unlike in-range. See Slicing for details.

Functional code that uses whole-array operations often creates many short-lived, intermediate arrays whose elements are referred to only once. The overhead of allocating and filling storage for these arrays can be removed entirely by using nonstrict arrays, sometimes at the cost of making the code’s performance more difficult to reason about. Another bonus is that computations with nonstrict arrays have fewer synchronization points, meaning that they will be easier to parallelize as Racket’s support for parallel computation improves. See Nonstrict Arrays for details.