[Haskell-cafe] Why does this program eat RAM?

Ross Paterson ross at soi.city.ac.uk
Tue Sep 5 18:35:57 EDT 2006

On Tue, Sep 05, 2006 at 12:55:48PM +0200, Udo Stenzel wrote:
> IMHO all accumulating functions, especially foldl, State.update,
> Map.insertWith, accumArray, absolutely need a strict version, because
> the strictness cannot be recovered by the library's user.

We already have foldl'.  Here's a strict version of fmap:

import Control.Applicative
import Data.Traversable

newtype Strict a = Strict a
getStrict (Strict x) = x

instance Functor Strict where
        fmap f (Strict x) = Strict (f x)

-- doesn't quite satisfy the Applicative laws
instance Applicative Strict where
        pure x = Strict x
        Strict f <*> Strict x = Strict (f $! x)

fmap' :: Traversable f => (a -> b) -> f a -> f b
fmap' f t = getStrict (traverse (Strict . f) t)

More information about the Haskell-Cafe mailing list