I have a Haskell function that returns a monad, declared as follows:
data Options = Options {
optGames :: Int,
optSuits :: Int,
optVerbose :: Bool
} deriving Show
playGame :: Options -> StateT StdGen (WriterT [String] IO)) Bool
This function plays a single game of solitaire, then returns a boolean indicating a win or loss, along with a log in the WriterT monad.
I would like to call this function a set number of times, each time using the “next” value of the random generator (StdGen), and concatenating the Bool return values into a list.
I tried creating a recursive function to do the calls, but can’t figure out how to pass the monad into each next iteration.
I would like to emulate
initial state >>= playGame >>= playGame ... -- repeat N times
and collect all of the resulting Bool values, as well as the log entries from the WriterT monad.
What is the best way to do this?
I think you’re looking for
replicateM. This repeats the given action a specified number of times, returning the result as a list. SoreplicateM n playGamecorresponds to playing the gamentimes and getting a list of the results back.