[Haskell-cafe] Incrementially updating an array

Ross Paterson ross at soi.city.ac.uk
Tue Dec 28 14:19:10 CET 2010

On Tue, Dec 28, 2010 at 08:46:24PM +0800, Robert Clausecker wrote:
> I have the following problem. As code is most time easier to understand,
> let give an example what I try to do in C:
> unsigned int i,j;
> unsigned int r[100];
> for (i = 0; i < 1000000; i++) {
>   j = makeResult(); //j is known to be smaller than 100.
>   r[j] = r[j] + 1;
> }
> (My C is poor, so please don't laugh)
> Currently I'm using a Data.Map for this, but as the result is bounded in
> this area, an array seems to be more appropreate. I don't want to mess
> with mutable arrays, but I can't find anything which does the same like
> Data.Map.insertWith for arrays in a reasonable way.
> In my example, the input comes from a lazy list and is than folded into
> the Map by insertWith (+) j 1 r. Is there any nice, quick way to do this
> with Arrays without using mutable ones?

accumArray is designed for situations like this.

More information about the Haskell-Cafe mailing list