<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Data.Array</title><link href="ocean.css" rel="stylesheet" type="text/css" title="Ocean" /><script src="haddock-util.js" type="text/javascript"></script><script type="text/javascript">//<![CDATA[ window.onload = function () {pageLoad();setSynopsis("mini_Data-Array.html");}; //]]> </script></head><body><div id="package-header"><ul class="links" id="page-menu"><li><a href="index.html">Contents</a></li><li><a href="doc-index.html">Index</a></li></ul><p class="caption">haskell2010-1.1.0.1: Compatibility with Haskell 2010</p></div><div id="content"><div id="module-header"><table class="info"><tr><th>Safe Haskell</th><td>Trustworthy</td></tr></table><p class="caption">Data.Array</p></div><div id="table-of-contents"><p class="caption">Contents</p><ul><li><a href="#g:1">Immutable non-strict arrays </a></li><li><a href="#g:2">Array construction </a></li><li><a href="#g:3">Accessing arrays </a></li><li><a href="#g:4">Incremental array updates </a></li><li><a href="#g:5">Derived arrays </a></li><li><a href="#g:6">Specification </a></li></ul></div><div id="synopsis"><p id="control.syn" class="caption expander" onclick="toggleSection('syn')">Synopsis</p><ul id="section.syn" class="hide" onclick="toggleSection('syn')"><li class="src short">module <a href="Data-Ix.html">Data.Ix</a></li><li class="src short"><span class="keyword">data</span> <a href="#t:Array">Array</a> i e</li><li class="src short"><a href="#v:array">array</a> :: <a href="Data-Ix.html#t:Ix">Ix</a> i => (i, i) -> [(i, e)] -> <a href="Data-Array.html#t:Array">Array</a> i e</li><li class="src short"><a href="#v:listArray">listArray</a> :: <a href="Data-Ix.html#t:Ix">Ix</a> i => (i, i) -> [e] -> <a href="Data-Array.html#t:Array">Array</a> i e</li><li class="src short"><a href="#v:accumArray">accumArray</a> :: <a href="Data-Ix.html#t:Ix">Ix</a> i => (e -> a -> e) -> e -> (i, i) -> [(i, a)] -> <a href="Data-Array.html#t:Array">Array</a> i e</li><li class="src short"><a href="#v:-33-">(!)</a> :: <a href="Data-Ix.html#t:Ix">Ix</a> i => <a href="Data-Array.html#t:Array">Array</a> i e -> i -> e</li><li class="src short"><a href="#v:bounds">bounds</a> :: <a href="Data-Ix.html#t:Ix">Ix</a> i => <a href="Data-Array.html#t:Array">Array</a> i e -> (i, i)</li><li class="src short"><a href="#v:indices">indices</a> :: <a href="Data-Ix.html#t:Ix">Ix</a> i => <a href="Data-Array.html#t:Array">Array</a> i e -> [i]</li><li class="src short"><a href="#v:elems">elems</a> :: <a href="Data-Ix.html#t:Ix">Ix</a> i => <a href="Data-Array.html#t:Array">Array</a> i e -> [e]</li><li class="src short"><a href="#v:assocs">assocs</a> :: <a href="Data-Ix.html#t:Ix">Ix</a> i => <a href="Data-Array.html#t:Array">Array</a> i e -> [(i, e)]</li><li class="src short"><a href="#v:-47--47-">(//)</a> :: <a href="Data-Ix.html#t:Ix">Ix</a> i => <a href="Data-Array.html#t:Array">Array</a> i e -> [(i, e)] -> <a href="Data-Array.html#t:Array">Array</a> i e</li><li class="src short"><a href="#v:accum">accum</a> :: <a href="Data-Ix.html#t:Ix">Ix</a> i => (e -> a -> e) -> <a href="Data-Array.html#t:Array">Array</a> i e -> [(i, a)] -> <a href="Data-Array.html#t:Array">Array</a> i e</li><li class="src short"><a href="#v:ixmap">ixmap</a> :: (<a href="Data-Ix.html#t:Ix">Ix</a> i, <a href="Data-Ix.html#t:Ix">Ix</a> j) => (i, i) -> (i -> j) -> <a href="Data-Array.html#t:Array">Array</a> j e -> <a href="Data-Array.html#t:Array">Array</a> i e</li></ul></div><div id="interface"><h1 id="g:1">Immutable non-strict arrays </h1><div class="doc"><p>Haskell provides indexable <em>arrays</em>, which may be thought of as functions whose domains are isomorphic to contiguous subsets of the integers. Functions restricted in this way can be implemented efficiently; in particular, a programmer may reasonably expect rapid access to the components. To ensure the possibility of such an implementation, arrays are treated as data, not as general functions. </p><p>Since most array functions involve the class <code><a href="Data-Ix.html#t:Ix">Ix</a></code>, the contents of the module <a href="../base-4.5.1.0/Data-Ix.html">Data.Ix</a> are re-exported from <a href="../array-0.4.0.0/Data-Array.html">Data.Array</a> for convenience: </p></div><div class="top"><p class="src">module <a href="Data-Ix.html">Data.Ix</a></p></div><div class="top"><p class="src"><span class="keyword">data</span> <a name="t:Array" class="def">Array</a> i e <a href="../base-4.5.1.0/src/GHC-Arr.html#Array" class="link">Source</a></p><div class="doc"><p>The type of immutable non-strict (boxed) arrays with indices in <code>i</code> and elements in <code>e</code>. </p></div><div class="subs instances"><p id="control.i:Array" class="caption collapser" onclick="toggleSection('i:Array')">Instances</p><div id="section.i:Array" class="show"><table><tr><td class="src"><a href="../base-4.5.1.0/Data-Typeable-Internal.html#t:Typeable2">Typeable2</a> <a href="Data-Array.html#t:Array">Array</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Ix.html#t:Ix">Ix</a> i => <a href="Prelude.html#t:Functor">Functor</a> (<a href="Data-Array.html#t:Array">Array</a> i)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="Data-Ix.html#t:Ix">Ix</a> i, <a href="Prelude.html#t:Eq">Eq</a> e) => <a href="Prelude.html#t:Eq">Eq</a> (<a href="Data-Array.html#t:Array">Array</a> i e)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="Data-Ix.html#t:Ix">Ix</a> i, <a href="Prelude.html#t:Ord">Ord</a> e) => <a href="Prelude.html#t:Ord">Ord</a> (<a href="Data-Array.html#t:Array">Array</a> i e)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="Data-Ix.html#t:Ix">Ix</a> a, <a href="Prelude.html#t:Read">Read</a> a, <a href="Prelude.html#t:Read">Read</a> b) => <a href="Prelude.html#t:Read">Read</a> (<a href="Data-Array.html#t:Array">Array</a> a b)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="Data-Ix.html#t:Ix">Ix</a> a, <a href="Prelude.html#t:Show">Show</a> a, <a href="Prelude.html#t:Show">Show</a> b) => <a href="Prelude.html#t:Show">Show</a> (<a href="Data-Array.html#t:Array">Array</a> a b)</td><td class="doc empty"> </td></tr></table></div></div></div><h1 id="g:2">Array construction </h1><div class="top"><p class="src"><a name="v:array" class="def">array</a></p><div class="subs arguments"><p class="caption">Arguments</p><table><tr><td class="src">:: <a href="Data-Ix.html#t:Ix">Ix</a> i</td><td class="doc empty"> </td></tr><tr><td class="src">=> (i, i)</td><td class="doc"><p>a pair of <em>bounds</em>, each of the index type of the array. These bounds are the lowest and highest indices in the array, in that order. For example, a one-origin vector of length '10' has bounds '(1,10)', and a one-origin '10' by '10' matrix has bounds '((1,1),(10,10))'. </p></td></tr><tr><td class="src">-> [(i, e)]</td><td class="doc"><p>a list of <em>associations</em> of the form (<em>index</em>, <em>value</em>). Typically, this list will be expressed as a comprehension. An association '(i, x)' defines the value of the array at index <code>i</code> to be <code>x</code>. </p></td></tr><tr><td class="src">-> <a href="Data-Array.html#t:Array">Array</a> i e</td><td class="doc empty"> </td></tr></table></div><div class="doc"><p>Construct an array with the specified bounds and containing values for given indices within these bounds. </p><p>The array is undefined (i.e. bottom) if any index in the list is out of bounds. If any two associations in the list have the same index, the value at that index is undefined (i.e. bottom). </p><p>Because the indices must be checked for these errors, <code><a href="Data-Array.html#v:array">array</a></code> is strict in the bounds argument and in the indices of the association list, but non-strict in the values. Thus, recurrences such as the following are possible: </p><pre> a = array (1,100) ((1,1) : [(i, i * a!(i-1)) | i <- [2..100]]) </pre><p>Not every index within the bounds of the array need appear in the association list, but the values associated with indices that do not appear will be undefined (i.e. bottom). </p><p>If, in any dimension, the lower bound is greater than the upper bound, then the array is legal, but empty. Indexing an empty array always gives an array-bounds error, but <code><a href="Data-Array.html#v:bounds">bounds</a></code> still yields the bounds with which the array was constructed. </p></div></div><div class="top"><p class="src"><a name="v:listArray" class="def">listArray</a> :: <a href="Data-Ix.html#t:Ix">Ix</a> i => (i, i) -> [e] -> <a href="Data-Array.html#t:Array">Array</a> i e<a href="../base-4.5.1.0/src/GHC-Arr.html#listArray" class="link">Source</a></p><div class="doc"><p>Construct an array from a pair of bounds and a list of values in index order. </p></div></div><div class="top"><p class="src"><a name="v:accumArray" class="def">accumArray</a><a href="../base-4.5.1.0/src/GHC-Arr.html#accumArray" class="link">Source</a></p><div class="subs arguments"><p class="caption">Arguments</p><table><tr><td class="src">:: <a href="Data-Ix.html#t:Ix">Ix</a> i</td><td class="doc empty"> </td></tr><tr><td class="src">=> (e -> a -> e)</td><td class="doc"><p>accumulating function </p></td></tr><tr><td class="src">-> e</td><td class="doc"><p>initial value </p></td></tr><tr><td class="src">-> (i, i)</td><td class="doc"><p>bounds of the array </p></td></tr><tr><td class="src">-> [(i, a)]</td><td class="doc"><p>association list </p></td></tr><tr><td class="src">-> <a href="Data-Array.html#t:Array">Array</a> i e</td><td class="doc empty"> </td></tr></table></div><div class="doc"><p>The <code><a href="Data-Array.html#v:accumArray">accumArray</a></code> function deals with repeated indices in the association list using an <em>accumulating function</em> which combines the values of associations with the same index. For example, given a list of values of some index type, <code>hist</code> produces a histogram of the number of occurrences of each index within a specified range: </p><pre> hist :: (Ix a, Num b) => (a,a) -> [a] -> Array a b hist bnds is = accumArray (+) 0 bnds [(i, 1) | i<-is, inRange bnds i] </pre><p>If the accumulating function is strict, then <code><a href="Data-Array.html#v:accumArray">accumArray</a></code> is strict in the values, as well as the indices, in the association list. Thus, unlike ordinary arrays built with <code><a href="../array-0.4.0.0/Data-Array.html#v:array">array</a></code>, accumulated arrays should not in general be recursive. </p></div></div><h1 id="g:3">Accessing arrays </h1><div class="top"><p class="src"><a name="v:-33-" class="def">(!)</a> :: <a href="Data-Ix.html#t:Ix">Ix</a> i => <a href="Data-Array.html#t:Array">Array</a> i e -> i -> e<a href="../base-4.5.1.0/src/GHC-Arr.html#%21" class="link">Source</a></p><div class="doc"><p>The value at the given index in an array. </p></div></div><div class="top"><p class="src"><a name="v:bounds" class="def">bounds</a> :: <a href="Data-Ix.html#t:Ix">Ix</a> i => <a href="Data-Array.html#t:Array">Array</a> i e -> (i, i)<a href="../base-4.5.1.0/src/GHC-Arr.html#bounds" class="link">Source</a></p><div class="doc"><p>The bounds with which an array was constructed. </p></div></div><div class="top"><p class="src"><a name="v:indices" class="def">indices</a> :: <a href="Data-Ix.html#t:Ix">Ix</a> i => <a href="Data-Array.html#t:Array">Array</a> i e -> [i]<a href="../base-4.5.1.0/src/GHC-Arr.html#indices" class="link">Source</a></p><div class="doc"><p>The list of indices of an array in ascending order. </p></div></div><div class="top"><p class="src"><a name="v:elems" class="def">elems</a> :: <a href="Data-Ix.html#t:Ix">Ix</a> i => <a href="Data-Array.html#t:Array">Array</a> i e -> [e]<a href="../base-4.5.1.0/src/GHC-Arr.html#elems" class="link">Source</a></p><div class="doc"><p>The list of elements of an array in index order. </p></div></div><div class="top"><p class="src"><a name="v:assocs" class="def">assocs</a> :: <a href="Data-Ix.html#t:Ix">Ix</a> i => <a href="Data-Array.html#t:Array">Array</a> i e -> [(i, e)]<a href="../base-4.5.1.0/src/GHC-Arr.html#assocs" class="link">Source</a></p><div class="doc"><p>The list of associations of an array in index order. </p></div></div><h1 id="g:4">Incremental array updates </h1><div class="top"><p class="src"><a name="v:-47--47-" class="def">(//)</a> :: <a href="Data-Ix.html#t:Ix">Ix</a> i => <a href="Data-Array.html#t:Array">Array</a> i e -> [(i, e)] -> <a href="Data-Array.html#t:Array">Array</a> i e</p><div class="doc"><p>Constructs an array identical to the first argument except that it has been updated by the associations in the right argument. For example, if <code>m</code> is a 1-origin, <code>n</code> by <code>n</code> matrix, then </p><pre> m//[((i,i), 0) | i <- [1..n]] </pre><p>is the same matrix, except with the diagonal zeroed. </p><p>Repeated indices in the association list are handled as for <code><a href="Data-Array.html#v:array">array</a></code>: the resulting array is undefined (i.e. bottom), </p></div></div><div class="top"><p class="src"><a name="v:accum" class="def">accum</a> :: <a href="Data-Ix.html#t:Ix">Ix</a> i => (e -> a -> e) -> <a href="Data-Array.html#t:Array">Array</a> i e -> [(i, a)] -> <a href="Data-Array.html#t:Array">Array</a> i e<a href="../base-4.5.1.0/src/GHC-Arr.html#accum" class="link">Source</a></p><div class="doc"><p><code><code><a href="Data-Array.html#v:accum">accum</a></code> f</code> takes an array and an association list and accumulates pairs from the list into the array with the accumulating function <code>f</code>. Thus <code><a href="Data-Array.html#v:accumArray">accumArray</a></code> can be defined using <code><a href="Data-Array.html#v:accum">accum</a></code>: </p><pre> accumArray f z b = accum f (array b [(i, z) | i <- range b]) </pre></div></div><h1 id="g:5">Derived arrays </h1><div class="top"><p class="src"><a name="v:ixmap" class="def">ixmap</a> :: (<a href="Data-Ix.html#t:Ix">Ix</a> i, <a href="Data-Ix.html#t:Ix">Ix</a> j) => (i, i) -> (i -> j) -> <a href="Data-Array.html#t:Array">Array</a> j e -> <a href="Data-Array.html#t:Array">Array</a> i e<a href="../base-4.5.1.0/src/GHC-Arr.html#ixmap" class="link">Source</a></p><div class="doc"><p><code><a href="Data-Array.html#v:ixmap">ixmap</a></code> allows for transformations on array indices. It may be thought of as providing function composition on the right with the mapping that the original array embodies. </p><p>A similar transformation of array values may be achieved using <code><a href="Prelude.html#v:fmap">fmap</a></code> from the <code><a href="Data-Array.html#t:Array">Array</a></code> instance of the <code><a href="Prelude.html#t:Functor">Functor</a></code> class. </p></div></div><h1 id="g:6">Specification </h1><div class="doc"><pre> module Array ( module Data.Ix, -- export all of Data.Ix Array, array, listArray, (!), bounds, indices, elems, assocs, accumArray, (//), accum, ixmap ) where import Data.Ix import Data.List( (\\) ) infixl 9 !, // data (Ix a) => Array a b = MkArray (a,a) (a -> b) deriving () array :: (Ix a) => (a,a) -> [(a,b)] -> Array a b array b ivs | any (not . inRange b. fst) ivs = error "Data.Array.array: out-of-range array association" | otherwise = MkArray b arr where arr j = case [ v | (i,v) <- ivs, i == j ] of [v] -> v [] -> error "Data.Array.!: undefined array element" _ -> error "Data.Array.!: multiply defined array element" listArray :: (Ix a) => (a,a) -> [b] -> Array a b listArray b vs = array b (zipWith (\ a b -> (a,b)) (range b) vs) (!) :: (Ix a) => Array a b -> a -> b (!) (MkArray _ f) = f bounds :: (Ix a) => Array a b -> (a,a) bounds (MkArray b _) = b indices :: (Ix a) => Array a b -> [a] indices = range . bounds elems :: (Ix a) => Array a b -> [b] elems a = [a!i | i <- indices a] assocs :: (Ix a) => Array a b -> [(a,b)] assocs a = [(i, a!i) | i <- indices a] (//) :: (Ix a) => Array a b -> [(a,b)] -> Array a b a // new_ivs = array (bounds a) (old_ivs ++ new_ivs) where old_ivs = [(i,a!i) | i <- indices a, i `notElem` new_is] new_is = [i | (i,_) <- new_ivs] accum :: (Ix a) => (b -> c -> b) -> Array a b -> [(a,c)] -> Array a b accum f = foldl (\a (i,v) -> a // [(i,f (a!i) v)]) accumArray :: (Ix a) => (b -> c -> b) -> b -> (a,a) -> [(a,c)] -> Array a b accumArray f z b = accum f (array b [(i,z) | i <- range b]) ixmap :: (Ix a, Ix b) => (a,a) -> (a -> b) -> Array b c -> Array a c ixmap b f a = array b [(i, a ! f i) | i <- range b] instance (Ix a) => Functor (Array a) where fmap fn (MkArray b f) = MkArray b (fn . f) instance (Ix a, Eq b) => Eq (Array a b) where a == a' = assocs a == assocs a' instance (Ix a, Ord b) => Ord (Array a b) where a <= a' = assocs a <= assocs a' instance (Ix a, Show a, Show b) => Show (Array a b) where showsPrec p a = showParen (p > arrPrec) ( showString "array " . showsPrec (arrPrec+1) (bounds a) . showChar ' ' . showsPrec (arrPrec+1) (assocs a) ) instance (Ix a, Read a, Read b) => Read (Array a b) where readsPrec p = readParen (p > arrPrec) (\r -> [ (array b as, u) | ("array",s) <- lex r, (b,t) <- readsPrec (arrPrec+1) s, (as,u) <- readsPrec (arrPrec+1) t ]) -- Precedence of the 'array' function is that of application itself arrPrec = 10 </pre></div></div></div><div id="footer"><p>Produced by <a href="http://www.haskell.org/haddock/">Haddock</a> version 2.11.0</p></div></body></html>