[Haskell-cafe] following up on space leak

Marcin Kosiba marcin.kosiba at gmail.com
Sat Jul 4 04:36:37 EDT 2009

On Saturday 04 July 2009, Uwe Hollerbach wrote:
> Good evening, all, following up on my question regarding space leaks,
> I seem to have stumbled across something very promising. I said I was
> using this tiny function "lastOrNil" to get the last value in a list,
> or the empty (scheme) list if the haskell list was empty. The uses of
> it were all of the form
>     lastOrNil (mapM <something> <some list>)
> so I wrote a different function mapML to do this directly:
> > mapML fn lst = mapMLA (List []) fn lst
> >   where mapMLA r _ [] = return r
> >         mapMLA ro fn (x:xs) =
> >            do rn <- fn x
> >               mapMLA rn fn xs
> This isn't an accumulator, it's a replacer (or, if you like, the
> "accumulation" is "drop the old one on the floor"), it starts out with
> the scheme empty list that I want as the default, and it never even
> builds the list which it'll just dump an instant later. Shazam! Memory
> usage dropped by roughly an order of magnitude in my little Collatz
> benchmark, and incidentally runtime improved by 25% or so as well. The
> horror! :-)

	IMHO expressing mapML using StateT would be a bit cleaner ;)

mapML :: (Monad m) => (a -> m List) -> [a] -> m List
mapML fn lst = execStateT mapMLAs (List [])
    mapMLAs  = sequence_ $ map mapMLA lst
    mapMLA x = (lift $ fn x) >>= put

Marcin Kosiba
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20090704/0fc36bb6/attachment.bin

More information about the Haskell-Cafe mailing list