Monad transformers: IO and state

haskell

Solution

import Control.Monad
import Control.Monad.Trans.State
import Control.Monad.IO.Class
import Data.Random.RVar
import Data.Random.Source.DevRandom
import Data.Random.List

myFun :: StateT [Int] IO ()
myFun = do
  lst <- get
  r <- liftIO $ runRVar (randomElement lst) DevRandom
  put $ if r > 3 then (r:lst) else lst
  return ()


main :: IO ()
main = evalStateT myFun [1..10] >>= print

Problem

This question is close to ground covered elsewhere, but I haven't found anything that addresses it specifically (at least not in a way that I'm able to understand). I'd like to update state in a way that depends on various random choices. Because of the instance of the RandomSource typeclass that I'm using, all of these random choices live in the IO monad, as below: ``` main :: IO Int main = do a <- pickRand [1..7] return a where pickRand lst = runRVar (choice lst) DevRandom ``` What I'd like to do is something like the following: store a state of type [Int], and if the randomly chosen list element a is greater than 3 , push it onto the state. Any tips?

Original source