On Feb 2, 2008 12:11 PM, Chaddaï Fouché <chaddai.fouche@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?