Bug in Control.Monad.State

Iavor Diatchki iavor.diatchki at gmail.com
Tue Nov 22 19:37:25 EST 2005

I did not understand the example you posted, as it contains functions
that are not defined, but here is something you can do (this is using
monadLib, but something like that should probably work with the
libraries distributed with GHC).  Let me know if this helps.

import Monad.StateT
import Monad.Id
import Random

type M        = StateT (StdGen,[Int]) Id

randomItem   :: M ()
randomItem    = do (g,xs) <- peek
                   let (x,g') = random g
                   poke_ (g',mod x 5 : xs)

needMoreItems :: M Bool
needMoreItems = do (_,xs) <- peek
                   return (sum xs < 50)

whileM p b    = do x <- p
                   if x then b >> whileM p b else return ()

test          = do g <- newStdGen
                   print $ runId $ evalState (g,[])
                                 $ do whileM needMoreItems randomItem
                                      (_,xs) <- peek
                                      return xs

On 11/22/05, Yitzchak Gale <gale at sefer.org> wrote:
> Hi Iavor,
> > Just to clarify, I wasn't suggesting that the
> > the monad is strict in the state component, but
> > rather that it is strict in the pair containing
> > the result and the state.
> Does this help in the "createItems" examples I
> sent to Udo in the previous post?
> Thanks,
> Yitz

More information about the Libraries mailing list