<!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.IArray</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-IArray.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">array-0.4.0.0: Mutable and immutable arrays</p></div><div id="content"><div id="module-header"><table class="info"><tr><th>Portability</th><td>non-portable (uses Data.Array.Base)</td></tr><tr><th>Stability</th><td>experimental</td></tr><tr><th>Maintainer</th><td>libraries@haskell.org</td></tr><tr><th>Safe Haskell</th><td>Trustworthy</td></tr></table><p class="caption">Data.Array.IArray</p></div><div id="table-of-contents"><p class="caption">Contents</p><ul><li><a href="#g:1">Array classes </a></li><li><a href="#g:2">Immutable non-strict (boxed) arrays </a></li><li><a href="#g:3">Array construction </a></li><li><a href="#g:4">Accessing arrays </a></li><li><a href="#g:5">Incremental array updates </a></li><li><a href="#g:6">Derived arrays </a></li></ul></div><div id="description"><p class="caption">Description</p><div class="doc"><p>Immutable arrays, with an overloaded interface. For array types which can be used with this interface, see the <code><a href="Data-Array-IArray.html#t:Array">Array</a></code> type exported by this module and the <a href="Data-Array-Unboxed.html">Data.Array.Unboxed</a> module. Other packages, such as diffarray, also provide arrays using this interface. </p></div></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"><span class="keyword">class</span> <a href="#t:IArray">IArray</a> a e <span class="keyword">where</span><ul class="subs"><li><a href="#v:bounds">bounds</a> :: <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i => a i e -> (i, i)</li></ul></li><li class="src short">module <a href="../base-4.5.1.0/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-Array-IArray.html#t:IArray">IArray</a> a e, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i) => (i, i) -> [(i, e)] -> a i e</li><li class="src short"><a href="#v:listArray">listArray</a> :: (<a href="Data-Array-IArray.html#t:IArray">IArray</a> a e, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i) => (i, i) -> [e] -> a i e</li><li class="src short"><a href="#v:accumArray">accumArray</a> :: (<a href="Data-Array-IArray.html#t:IArray">IArray</a> a e, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i) => (e -> e' -> e) -> e -> (i, i) -> [(i, e')] -> a i e</li><li class="src short"><a href="#v:-33-">(!)</a> :: (<a href="Data-Array-IArray.html#t:IArray">IArray</a> a e, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i) => a i e -> i -> e</li><li class="src short"><a href="#v:indices">indices</a> :: (<a href="Data-Array-IArray.html#t:IArray">IArray</a> a e, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i) => a i e -> [i]</li><li class="src short"><a href="#v:elems">elems</a> :: (<a href="Data-Array-IArray.html#t:IArray">IArray</a> a e, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i) => a i e -> [e]</li><li class="src short"><a href="#v:assocs">assocs</a> :: (<a href="Data-Array-IArray.html#t:IArray">IArray</a> a e, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i) => a i e -> [(i, e)]</li><li class="src short"><a href="#v:-47--47-">(//)</a> :: (<a href="Data-Array-IArray.html#t:IArray">IArray</a> a e, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i) => a i e -> [(i, e)] -> a i e</li><li class="src short"><a href="#v:accum">accum</a> :: (<a href="Data-Array-IArray.html#t:IArray">IArray</a> a e, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i) => (e -> e' -> e) -> a i e -> [(i, e')] -> a i e</li><li class="src short"><a href="#v:amap">amap</a> :: (<a href="Data-Array-IArray.html#t:IArray">IArray</a> a e', <a href="Data-Array-IArray.html#t:IArray">IArray</a> a e, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i) => (e' -> e) -> a i e' -> a i e</li><li class="src short"><a href="#v:ixmap">ixmap</a> :: (<a href="Data-Array-IArray.html#t:IArray">IArray</a> a e, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> j) => (i, i) -> (i -> j) -> a j e -> a i e</li></ul></div><div id="interface"><h1 id="g:1">Array classes </h1><div class="top"><p class="src"><span class="keyword">class</span> <a name="t:IArray" class="def">IArray</a> a e <span class="keyword">where</span></p><div class="doc"><p>Class of immutable array types. </p><p>An array type has the form <code>(a i e)</code> where <code>a</code> is the array type constructor (kind <code>* -> * -> *</code>), <code>i</code> is the index type (a member of the class <code><a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a></code>), and <code>e</code> is the element type. The <code>IArray</code> class is parameterised over both <code>a</code> and <code>e</code>, so that instances specialised to certain element types can be defined. </p></div><div class="subs methods"><p class="caption">Methods</p><p class="src"><a name="v:bounds" class="def">bounds</a> :: <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i => a i e -> (i, i)</p><div class="doc"><p>Extracts the bounds of an immutable array </p></div></div><div class="subs instances"><p id="control.i:IArray" class="caption collapser" onclick="toggleSection('i:IArray')">Instances</p><div id="section.i:IArray" class="show"><table><tr><td class="src"><a href="Data-Array-IArray.html#t:IArray">IArray</a> <a href="Data-Array-IArray.html#t:Array">Array</a> e</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Array-IArray.html#t:IArray">IArray</a> <a href="Data-Array-Unboxed.html#t:UArray">UArray</a> <a href="../base-4.5.1.0/Data-Bool.html#t:Bool">Bool</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Array-IArray.html#t:IArray">IArray</a> <a href="Data-Array-Unboxed.html#t:UArray">UArray</a> <a href="../base-4.5.1.0/Data-Char.html#t:Char">Char</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Array-IArray.html#t:IArray">IArray</a> <a href="Data-Array-Unboxed.html#t:UArray">UArray</a> <a href="../base-4.5.1.0/Prelude.html#t:Double">Double</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Array-IArray.html#t:IArray">IArray</a> <a href="Data-Array-Unboxed.html#t:UArray">UArray</a> <a href="../base-4.5.1.0/Prelude.html#t:Float">Float</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Array-IArray.html#t:IArray">IArray</a> <a href="Data-Array-Unboxed.html#t:UArray">UArray</a> <a href="../base-4.5.1.0/Data-Int.html#t:Int">Int</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Array-IArray.html#t:IArray">IArray</a> <a href="Data-Array-Unboxed.html#t:UArray">UArray</a> <a href="../base-4.5.1.0/Data-Int.html#t:Int8">Int8</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Array-IArray.html#t:IArray">IArray</a> <a href="Data-Array-Unboxed.html#t:UArray">UArray</a> <a href="../base-4.5.1.0/Data-Int.html#t:Int16">Int16</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Array-IArray.html#t:IArray">IArray</a> <a href="Data-Array-Unboxed.html#t:UArray">UArray</a> <a href="../base-4.5.1.0/Data-Int.html#t:Int32">Int32</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Array-IArray.html#t:IArray">IArray</a> <a href="Data-Array-Unboxed.html#t:UArray">UArray</a> <a href="../base-4.5.1.0/Data-Int.html#t:Int64">Int64</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Array-IArray.html#t:IArray">IArray</a> <a href="Data-Array-Unboxed.html#t:UArray">UArray</a> <a href="../base-4.5.1.0/Data-Word.html#t:Word">Word</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Array-IArray.html#t:IArray">IArray</a> <a href="Data-Array-Unboxed.html#t:UArray">UArray</a> <a href="../base-4.5.1.0/Data-Word.html#t:Word8">Word8</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Array-IArray.html#t:IArray">IArray</a> <a href="Data-Array-Unboxed.html#t:UArray">UArray</a> <a href="../base-4.5.1.0/Data-Word.html#t:Word16">Word16</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Array-IArray.html#t:IArray">IArray</a> <a href="Data-Array-Unboxed.html#t:UArray">UArray</a> <a href="../base-4.5.1.0/Data-Word.html#t:Word32">Word32</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Array-IArray.html#t:IArray">IArray</a> <a href="Data-Array-Unboxed.html#t:UArray">UArray</a> <a href="../base-4.5.1.0/Data-Word.html#t:Word64">Word64</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Array-IArray.html#t:IArray">IArray</a> <a href="Data-Array-Unboxed.html#t:UArray">UArray</a> (<a href="../base-4.5.1.0/Foreign-StablePtr.html#t:StablePtr">StablePtr</a> a)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Array-IArray.html#t:IArray">IArray</a> <a href="Data-Array-Unboxed.html#t:UArray">UArray</a> (<a href="../base-4.5.1.0/Foreign-Ptr.html#t:Ptr">Ptr</a> a)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Array-IArray.html#t:IArray">IArray</a> <a href="Data-Array-Unboxed.html#t:UArray">UArray</a> (<a href="../base-4.5.1.0/Foreign-Ptr.html#t:FunPtr">FunPtr</a> a)</td><td class="doc empty"> </td></tr></table></div></div></div><div class="top"><p class="src">module <a href="../base-4.5.1.0/Data-Ix.html">Data.Ix</a></p></div><h1 id="g:2">Immutable non-strict (boxed) arrays </h1><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-IArray.html#t:Array">Array</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Array-IArray.html#t:IArray">IArray</a> <a href="Data-Array-IArray.html#t:Array">Array</a> e</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i => <a href="../base-4.5.1.0/Control-Monad.html#t:Functor">Functor</a> (<a href="Data-Array-IArray.html#t:Array">Array</a> i)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i, <a href="../base-4.5.1.0/Data-Eq.html#t:Eq">Eq</a> e) => <a href="../base-4.5.1.0/Data-Eq.html#t:Eq">Eq</a> (<a href="Data-Array-IArray.html#t:Array">Array</a> i e)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i, <a href="../base-4.5.1.0/Data-Ord.html#t:Ord">Ord</a> e) => <a href="../base-4.5.1.0/Data-Ord.html#t:Ord">Ord</a> (<a href="Data-Array-IArray.html#t:Array">Array</a> i e)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> a, <a href="../base-4.5.1.0/Text-Show.html#t:Show">Show</a> a, <a href="../base-4.5.1.0/Text-Show.html#t:Show">Show</a> b) => <a href="../base-4.5.1.0/Text-Show.html#t:Show">Show</a> (<a href="Data-Array-IArray.html#t:Array">Array</a> a b)</td><td class="doc empty"> </td></tr></table></div></div></div><h1 id="g:3">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-Array-IArray.html#t:IArray">IArray</a> a e, <a href="../base-4.5.1.0/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>bounds of the array: (lowest,highest) </p></td></tr><tr><td class="src">-> [(i, e)]</td><td class="doc"><p>list of associations </p></td></tr><tr><td class="src">-> a i e</td><td class="doc empty"> </td></tr></table></div><div class="doc"><p>Constructs an immutable array from a pair of bounds and a list of initial associations. </p><p>The bounds are specified as a pair of the lowest and highest bounds in the array respectively. 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><p>An association is a pair of the form <code>(i,x)</code>, which defines the value of the array at index <code>i</code> to be <code>x</code>. The array is undefined 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 implementation-dependent. (In GHC, the last value specified for that index is used. Other implementations will also do this for unboxed arrays, but Haskell 98 requires that for <code>Array</code> the value at such indices is bottom.) </p><p>Because the indices must be checked for these errors, <code><a href="Data-Array-IArray.html#v:array">array</a></code> is strict in the bounds argument and in the indices of the association list. Whether <code>array</code> is strict or non-strict in the elements depends on the array type: <code><a href="Data-Array.html#t:Array">Array</a></code> is a non-strict array type, but all of the <code><a href="Data-Array-Unboxed.html#t:UArray">UArray</a></code> arrays are strict. Thus in a non-strict array, 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. </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-IArray.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-Array-IArray.html#t:IArray">IArray</a> a e, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i) => (i, i) -> [e] -> a i e</p><div class="doc"><p>Constructs an immutable array from a list of initial elements. The list gives the elements of the array in ascending order beginning with the lowest index. </p></div></div><div class="top"><p class="src"><a name="v:accumArray" class="def">accumArray</a></p><div class="subs arguments"><p class="caption">Arguments</p><table><tr><td class="src">:: (<a href="Data-Array-IArray.html#t:IArray">IArray</a> a e, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i)</td><td class="doc empty"> </td></tr><tr><td class="src">=> (e -> e' -> e)</td><td class="doc"><p>An accumulating function </p></td></tr><tr><td class="src">-> e</td><td class="doc"><p>A default element </p></td></tr><tr><td class="src">-> (i, i)</td><td class="doc"><p>The bounds of the array </p></td></tr><tr><td class="src">-> [(i, e')]</td><td class="doc"><p>List of associations </p></td></tr><tr><td class="src">-> a i e</td><td class="doc"><p>Returns: the array </p></td></tr></table></div><div class="doc"><p>Constructs an immutable array from a list of associations. Unlike <code><a href="Data-Array-IArray.html#v:array">array</a></code>, the same index is allowed to occur multiple times in the list of associations; an <em>accumulating function</em> is used to combine the values of elements with the same index. </p><p>For example, given a list of values of some index type, hist 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></div></div><h1 id="g:4">Accessing arrays </h1><div class="top"><p class="src"><a name="v:-33-" class="def">(!)</a> :: (<a href="Data-Array-IArray.html#t:IArray">IArray</a> a e, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i) => a i e -> i -> e</p><div class="doc"><p>Returns the element of an immutable array at the specified index. </p></div></div><div class="top"><p class="src"><a name="v:indices" class="def">indices</a> :: (<a href="Data-Array-IArray.html#t:IArray">IArray</a> a e, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i) => a i e -> [i]</p><div class="doc"><p>Returns a list of all the valid indices in an array. </p></div></div><div class="top"><p class="src"><a name="v:elems" class="def">elems</a> :: (<a href="Data-Array-IArray.html#t:IArray">IArray</a> a e, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i) => a i e -> [e]</p><div class="doc"><p>Returns a list of all the elements of an array, in the same order as their indices. </p></div></div><div class="top"><p class="src"><a name="v:assocs" class="def">assocs</a> :: (<a href="Data-Array-IArray.html#t:IArray">IArray</a> a e, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i) => a i e -> [(i, e)]</p><div class="doc"><p>Returns the contents of an array as a list of associations. </p></div></div><h1 id="g:5">Incremental array updates </h1><div class="top"><p class="src"><a name="v:-47--47-" class="def">(//)</a> :: (<a href="Data-Array-IArray.html#t:IArray">IArray</a> a e, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i) => a i e -> [(i, e)] -> a i e</p><div class="doc"><p>Takes an array and a list of pairs and returns an array identical to the left argument except that it has been updated by the associations in the right argument. For example, if m is a 1-origin, n by n matrix, then <code>m//[((i,i), 0) | i <- [1..n]]</code> is the same matrix, except with the diagonal zeroed. </p><p>As with the <code><a href="Data-Array-IArray.html#v:array">array</a></code> function, if any two associations in the list have the same index, the value at that index is implementation-dependent. (In GHC, the last value specified for that index is used. Other implementations will also do this for unboxed arrays, but Haskell 98 requires that for <code>Array</code> the value at such indices is bottom.) </p><p>For most array types, this operation is O(<em>n</em>) where <em>n</em> is the size of the array. However, the diffarray package provides an array type for which this operation has complexity linear in the number of updates. </p></div></div><div class="top"><p class="src"><a name="v:accum" class="def">accum</a> :: (<a href="Data-Array-IArray.html#t:IArray">IArray</a> a e, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i) => (e -> e' -> e) -> a i e -> [(i, e')] -> a i e</p><div class="doc"><p><code>accum 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-IArray.html#v:accumArray">accumArray</a></code> can be defined using <code><a href="Data-Array-IArray.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:6">Derived arrays </h1><div class="top"><p class="src"><a name="v:amap" class="def">amap</a> :: (<a href="Data-Array-IArray.html#t:IArray">IArray</a> a e', <a href="Data-Array-IArray.html#t:IArray">IArray</a> a e, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i) => (e' -> e) -> a i e' -> a i e</p><div class="doc"><p>Returns a new array derived from the original array by applying a function to each of the elements. </p></div></div><div class="top"><p class="src"><a name="v:ixmap" class="def">ixmap</a> :: (<a href="Data-Array-IArray.html#t:IArray">IArray</a> a e, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> i, <a href="../base-4.5.1.0/Data-Ix.html#t:Ix">Ix</a> j) => (i, i) -> (i -> j) -> a j e -> a i e</p><div class="doc"><p>Returns a new array derived from the original array by applying a function to each of the indices. </p></div></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>