'while' loop on mutable arrays causes stack overflow

Gunnar Kedenburg gunnar at haquebright.de
Thu Apr 20 14:10:39 EDT 2006


Hello Bulat,

Bulat Ziganshin wrote:

> 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

That is a nice implementation. In fact, creating uninitialized arrays
with newArray_ and then setting all its elements to some value using a
function similar to this one is dramatically faster than creating
initialized arrays with newArray.

Gunnar.


More information about the Glasgow-haskell-users mailing list