[Haskell-cafe] (state) monad and CPS
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:
> 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
> 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  module.
More information about the Haskell-Cafe