[Haskell-cafe] (state) monad and CPS
Nicolas Pouillard
nicolas.pouillard at gmail.com
Thu Nov 12 04:27:58 EST 2009
Excerpts from wren ng thornton's message of Thu Nov 12 08:17:41 +0100 2009:
> Nicolas Pouillard wrote:
> > Excerpts from jean-christophe mincke's message of Tue Nov 10 21:18:34 +0100 2009:
> >> do acc <- get
> >> put (acc+1)
> >> ...
> >
> > Since this pattern occurs often 'modify' is a combination of get and put:
> >
> > do modify (+1)
> > ...
>
> Though the caveat about laziness applies here as well. modify is
> famously lazy which can lead to space leaks and stack overflows. Better
> would be to define and use your own strict version:
>
> modify' f = get >>= \x -> put $! f x
However if you want a strict state you should better use
Control.Monad.State.Strict [1].
Finally I'm wondering if [1] is strict enough...
[1]: http://www.haskell.org/ghc/docs/latest/html/libraries/mtl/Control-Monad-State-Strict.html
--
Nicolas Pouillard
http://nicolaspouillard.fr
More information about the Haskell-Cafe
mailing list