Hi,<br><br>I have a list of index-value pairs and I want to get a list
which is sorted by the index and which contains index-wise sums of all the values.<br>Here is a function that does this (and a bit more).<br>---<br>
pixelHistogram samples = let<br>&nbsp;&nbsp;&nbsp; index s t = let<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x = round $ (fromIntegral imageWidth) * (1-s)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; y = round $ (fromIntegral imageHeight) * (1-t)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; in y*imageWidth + x<br>&nbsp;&nbsp;&nbsp; indexedSamples = [(index s t, color) | (s, t, color) &lt;- samples]<br>

&nbsp;&nbsp;&nbsp; pixelArray :: Array Int Color<br>&nbsp;&nbsp;&nbsp; pixelArray = accumArray (\+) black (0, imageWidth*imageHeight) indexedSamples <br>&nbsp;&nbsp;&nbsp; in elems pixelArray<br>---<br><br>The
problem is that this function is very inefficient. It seems that the
whole indexedSamples list is stored in memory when creating pixelArray.<br>
When I do some profiling I see that the heap consumption of this function grows linearly. <br>If I just write the samples list to a file instead of first summing them, I get a nice and flat heap profile.<br><br>So how to do this efficiently?<br>
<br>Thanks,<br><font color="#888888">
Lauri</font>