'while' loop on mutable arrays causes stack overflow
Bulat Ziganshin
bulat.ziganshin at gmail.com
Thu Apr 20 12:48:09 EDT 2006
Hello Gunnar,
Thursday, April 20, 2006, 7:34:33 PM, you wrote:
> Thanks a lot, it works now. I believed that writeSTRef would always
> evaluate its arguments because ST is strict, but apparently I was
> mistaken :)
strict ST monad means that all operations inside it are performed
immediately and not deferred until their side-effect is really
required. but when you write something to STRef, this reference just
saves closure that will calculate expression. to force evaluation of
expression you need also to use '$!' or use unbozed references (that
is not yet available, i plan to announce this lib several days later).
the fastest way to calculate dot product is:
dot :: STUArray s Int Double -> STUArray s Int Double -> ST s Double
dot x y = do
let (l,r) = bounds x
cycle sum i | sum `seq` i>r = return sum
cycle sum i = do xi <- readArray x i
yi <- readArray y i
cycle (sum+xi*yi) (i+1)
cycle 0 l
--
Best regards,
Bulat mailto:Bulat.Ziganshin at gmail.com
More information about the Glasgow-haskell-users
mailing list