[Haskell-cafe] Mutable arrays

Jeff φ jeff1.61803 at gmail.com
Thu Feb 7 11:34:58 EST 2008


On Feb 2, 2008 12:11 PM, Chaddaï Fouché <chaddai.fouche at gmail.com> wrote:

> To address this I propose this function :
> foldl1MArray' :: (MArray a e m, Ix i) => (e -> e -> e) -> a i e -> m e
> foldl1MArray' f a = do
>  (l,u) <- getBounds a
>  firstElem <- readArray a l
>  foldM (\a mb -> a `seq` mb >>= return . f a)
>        firstElem (map (readArray a) (range (l,u)))
>


I played with your foldl1MArray' last night.  I noticed it can be reduced to
. . .

foldl1MArray' :: (MArray a e m, Ix i) => (e -> e -> e) -> a i e -> m e
foldl1MArray' f a = do
    (l,u) <- getBounds a
    foldl1' (liftM2 f) (map (readArray a) (range (l,u)))

Unfortunately, my new version consumes a lot of stack and heap space.  Why
is this so inefficient?  Is there a simple change that will make it
efficient?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20080207/86340c41/attachment.htm


More information about the Haskell-Cafe mailing list