The DynamicArrayFn functor
Author: John Reppy
Keywords: Standard ML, SML, SML/NJ Library

The SML of NJ Library
The `DynamicArrayFn` functor

The `DynamicArrayFn` functor provides dynamically sized monomorphic arrays. Each array has an associated default value, which is covers those elements that have not been explicitly initialized (conceptually, one can view an array as having an infinite size). Thus, reads from indices above the bound will return the default value. The bound of an array is the highest index of an initialized element (or `~1` if there are no initialized elements). The defined range of the array are the elements in the positions indexed from zero to the bound.

## Synopsis

    signature MONO_DYNAMIC_ARRAY
    functor DynamicArrayFn (A : MONO_ARRAY) : MONO_DYNAMIC_ARRAY

## Functor Argument Interface

    A : MONO_ARRAY

## Functor Argument Description

`A : MONO_ARRAY`

  A structure that implements the `MONO_ARRAY` signature from the SML Basis Library.

## Interface

    type elem
    type array

    val array : (int * elem) -> array

    val subArray : array * int * int -> array

    val fromList : elem list * elem -> array
    val toList : array -> elem list

    val tabulate: int * (int -> elem) * elem -> array

    val default : array -> elem

    val sub : array * int -> elem

    val update : array * int * elem -> unit

    val bound : array -> int

    val truncate : array * int -> unit

## Interface Description

`type elem`

  The type of array elements.

`type array`

  The type of dynamic arrays.

`val array : (int * elem) -> array`

  `array (sz, dflt)` returns a new array with bound `~1` and default value `dflt`. The `sz` argument, which must be non-negative, is used as a hint of the potential range of indices. This function raises the `Size` exception if `sz < 0`.

`val subArray : array * int * int -> array`

  `subArray (arr, lo, hi)` returns a new array with the same default as `arr`, and whose values in the range `[0, hi-lo]` are equal to the values in `arr` in the range `[lo, hi]`. This function raises the `Size` exception if `lo < 0` or `hi < lo-1`.

`val fromList : elem list * elem -> array`

  `fromList (lst, dflt)` returns a new array created from the elements of `lst` and with default value `dflt`. The bound of the array will be `length lst - 1`.

`val toList : 'a array -> 'a list`

  `toList arr` returns a list of the array’s contents. The resulting list will have the array’s bound plus one elements.

`val tabulate: int * (int -> elem) * elem -> array`

  `tabulate (sz, init, dflt)` returns a new array with the first `sz` elements initialized using the function `init` and the default value `dflt`. This function raises the `Size` exception if `sz < 0`.

`val default : array -> elem`

  `default arr` returns the default value for the array.

`val sub : array * int -> elem`

  `sub (arr, ix)` returns the value of the array at index `ix`. If that value has not been explicitly set, then it returns the array’s default value. This function raises the `Subscript` exception if `ix < 0`.

`val update : array * int * elem -> unit`

  `update (arr, ix, v)` sets the value at index `ix` of the array to `v`. If `ix` is greater than the current bound of the array, then the bound is set to `ix`. This function raises the `Subscript` exception if `ix < 0`.

`val bound : array -> int`

  `bound arr` returns the current bound of the array, which is the highest index that has been explicitly set (e.g., by `update`).

`val truncate : array * int -> unit`

  `truncate (arr, sz)` sets every entry with index greater or equal to `sz` to the array’s default value.

## See Also

`DynamicArray`, `MONO_ARRAY`, `MonoArrayFn`, The Util Library

SML/NJ Version 110.99.9 (November 4, 2025)
Last updated 2025-11-04 15:48:25 UTC