'while' loop on mutable arrays causes stack overflow

Bulat Ziganshin bulat.ziganshin at gmail.com
Thu Apr 20 10:12:06 EDT 2006

Hello Gunnar,

Thursday, April 20, 2006, 4:29:29 PM, you wrote:

> a friend and I were recently experimenting with mutable arrays. We tried
> to implement a simple dot product on STUArrays using a 'while' loop.
> Unfortunately, every implementation we produced caused a stack overflow.

it seems that you just construct huge unevaluated thunk. making things
stricter is a whole art, in this case try the following:

>>                      writeSTRef b (ev<r)
>>                      writeSTRef e (ev+1)
>>                      writeSTRef a (av+xe*ye))

                      writeSTRef b $! (ev<r)
                      writeSTRef e $! (ev+1)
                      writeSTRef a $! (av+xe*ye))

>> for :: Int -> IO () -> IO ()
>> for 0 c = return ()
>> for n c = c >> for (n-1) c

i recommend you to use your `for` instead of your `while` - it's a much faster
and straightforward

Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com

More information about the Glasgow-haskell-users mailing list