[Haskell-cafe] (state) monad and CPS

Nicolas Pouillard nicolas.pouillard at gmail.com
Wed Nov 11 09:00:33 EST 2009

Excerpts from jean-christophe mincke's message of Tue Nov 10 21:18:34 +0100 2009:
> Hello,

> I would like to get some advice about state monad (or any other monad I
> guess) and CPS.

Here is to remarks somewhat off topic:


> walk Empty acc k = k acc
> walk (Leaf _) acc k = k (acc+1)
> walk (Node (l, _, r)) acc k = let k1 acc = walk r acc k
>                               in
>                               walk l (acc+1) k1

Remember that by default laziness and accumulators does not fits
well together. Here you are probably building a chain of thunks.
Making acc a strict argument (using !acc) or using 'seq' (acc `seq` ...)
will cure this.


> do acc <- get
>    put (acc+1)
>    ...

Since this pattern occurs often 'modify' is a combination of get and put:

do modify (+1)

About your CPS question, you should have a look at the 'transformers' package,
in particular the Control.Monad.Trans.Cont [1] module.

[1]: http://hackage.haskell.org/packages/archive/transformers/

Best regards,

Nicolas Pouillard

More information about the Haskell-Cafe mailing list