Determines whether math/array
functions return strict arrays.
The default value is #t
See Nonstrict Arrays for a discussion on nonstrict arrays.
Causes arr to compute and store all of its elements. Thereafter, arr
computes its elements by retrieving them from the store.
If arr is already strict, (array-strict! arr) does nothing.
This is a macro so that Typed Racket will preserve arr’s type exactly. If it were a
function, (array-strict arr) would always have the type (Array A), even if
arr were a subtype of (Array A), such as (Mutable-Array A).
Apply one of these to return values from library functions to ensure that users get strict arrays
by default. See Nonstrict Arrays for details.
Use build-simple-array to create arrays that represent simple functions of their indexes.
For example, basic array constructors such as make-array are defined in terms of this or its
Be careful with this function. While it creates arrays that are always memory-efficient,
it is easy to ruin your program’s performance by using it to define arrays for which element lookup
is permanently expensive. In the wrong circumstances, using it instead of build-array can
turn a linear algorithm into an exponential one!
Computing an element is never more expensive than computing a row-major index followed
by applying vector-ref. An example is index-array, which only
computes row-major indexes.
Computing an element is independent of any other array’s elements.
In this circumstance, it is impossible to compose some unbounded number of possibly
expensive array procedures.
You can prove that each element will be computed at most once, throughout the
entire life of your program. This is true, for example, when the result is sent
only to a function that makes a copy of it, such as array-lazy or
for an example of the last circumstance.
Returns an immutable, nonstrict
array with the same elements as arr
, but element
computations are cached.
Perhaps the most natural way to use array-lazy
is for so-called “dynamic programming,”
or memoizing a function that happens to have a rectangular domain.
For example, this computes the first 10 Fibonacci numbers in linear time:
- : (Array Nonnegative-Integer)
(array #[0 1 1 2 3 5 8 13 21 34])
never stores its elements, its procedure argument may refer to
the array it returns.
Wrapping its result with array-lazy
makes each array-ref
take no more than linear
time; further, each takes constant time when the elements of fibs
are computed in order.
, computing the elements of fibs
would take exponential time.
Printing a lazy array computes and caches all of its elements, as does applying
array-strict! or array-strict to it.
Except for arrays returned by build-simple-array, it is useless to apply array-lazy
to a strict array.
Using the lazy copy instead of the original only degrades performance.
While it may seem that array-lazy
should just return arr
strict, this would violate the invariant that array-lazy
returns immutable arrays.