I'm learning Haskell by working through the Haskell course by Brent Yorgey. I just reached the monad section, and while I think I (finally) have a decent grasp on how to work with monads, I don't understand how to test code that uses them.
The exercise for this section is to write a (simplified) Risk simulation, and it requires heavy use of the Rand StdGen
monad. In particular, we have to write the following function:
type Army = Int
data Battlefield = Battlefield { attackers :: Army, defenders :: Army }
battle :: Battlefield -> Rand StdGen Battlefield
It takes an initial battlefield, and runs a simulation of how this battle would go.
I have an implementation for it, but I don't understand how to test it. I can't "get at" the values inside the Rand StdGen Battlefield
returned by battle
, so I can't print them out at the GHCI interpreter, which is how I've been testing my code so far. I also can't figure out how to print the result of a battle in a Haskell main function, or something. How do people go about testing these sorts of functions?
evalRand
and friends.evalRand
takes a 'starting'RandomGen
and runs the monadic computation deterministically. – Benjamin Hodgson♦ Sep 21 '14 at 15:17