Hi everyone,<br><br>I have the following code whose purpose is to add dither (noise) to a given array. The code looks very straightforward but apparently it has a memory leak somewhere. Here I try to run the algorithm for an array of 10,000,000 integers. Ten million unboxed strict integers should equal to 40MB which should pose no problems to any modern system. However, the program fails with a stack overflow error. I'm using GHC 
6.6 on Windows with 1 GB of RAM.<br><br>I've tried applying seq and some other strictness tricks (such as x == x) pretty much everywhere on the code with no results. Could you please help me understand what is going on here? Have I misunderstood something critical in how Haskell works? Here is the relevant portion of the code:
<br><br>module Main where<br><br>import <a href="http://Data.Array.IO">Data.Array.IO</a><br>import System.Random<br><br>type Buffer = IOUArray Int Int<br><br>-- | Triangular Probability Density Function, equivalent to a roll of two dice.
<br>-- The number sums have different probabilities of surfacing.<br>tpdf :: (Int, Int) -&gt; IO Int<br>tpdf (low, high) = do<br>&nbsp;&nbsp;&nbsp; first &lt;- getStdRandom (randomR (low, high))<br>&nbsp;&nbsp;&nbsp; second &lt;- getStdRandom (randomR (low, high))
<br>&nbsp;&nbsp;&nbsp; return ((first + second) `div` 2)<br><br>-- | Fills an array with dither generated by the specified function.<br>genSeries :: Buffer -&gt; ((Int, Int) -&gt; IO Int) -&gt; (Int, Int) -&gt; IO ()<br>genSeries buf denfun lims =
<br>&nbsp;&nbsp;&nbsp; let worker low i<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | i &gt;= low = do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r &lt;- denfun lims<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; writeArray buf i r<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; worker low (i - 1)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | otherwise = return ()<br>&nbsp;&nbsp;&nbsp; in do
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (lo, hi) &lt;- getBounds buf<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; worker lo hi<br clear="all"><br>main = do<br>&nbsp;&nbsp;&nbsp; -- This should allocate a 40 MB array<br>&nbsp;&nbsp;&nbsp; buf &lt;- newArray_ (0, 10000000) :: IO Buffer<br>&nbsp;&nbsp;&nbsp; -- Fill the array with dither
<br>&nbsp;&nbsp;&nbsp; genSeries buf tpdf (2, 12)<br><br>-- <br><a href="mailto:niko.korhonen@gmail.com">niko.korhonen@gmail.com</a>