
Thanks for the tip, Ozgur. It worked for me. Is this what you had in mind, Ryan?
Michael
==============
import Control.Monad.State.Lazy
import Control.Monad
import System.Random
type GeneratorState = State StdGen
data Craps a = Roll a | Win a | Lose a deriving (Show)
genRandomR :: Random a => (a,a) -> GeneratorState a
genRandomR = state . randomR
rollDie :: GeneratorState Int
rollDie = genRandomR (1,6)
roll2Dice :: GeneratorState Int
roll2Dice = liftM2 (+) rollDie rollDie
f :: Craps [Int] -> GeneratorState (Craps [Int])
f (Roll []) = do g0 <- get
let (throw1,g1) = runState roll2Dice g0
put g1
case throw1 of
2 -> return (Lose [throw1])
3 -> return (Lose [throw1])
7 -> return (Win [throw1])
11 -> return (Win [throw1])
_ -> do g1 <- get
let (throw2,g2) = runState roll2Dice g1
put g2
if throw2 == throw1
then do return (Win [throw1,throw2])
else
if throw2 == 7
then do return (Lose [throw1,throw2])
else do f (Roll [throw1,throw2])
f (Roll z@(throw1:throws)) = do g0 <- get
let (throw,g1) = runState roll2Dice g0
put g1
if throw == throw1
then do return (Win (z ++ [throw]))
else
if throw == 7
then do return (Lose (z ++ [throw]))
else do f (Roll (z ++ [throw]))
--- On Wed, 12/22/10, Ozgur Akgun