trouble with state monad composition
Posted
by
user1308560
on Stack Overflow
See other posts from Stack Overflow
or by user1308560
Published on 2012-09-20T15:36:06Z
Indexed on
2012/09/20
15:37 UTC
Read the original article
Hit count: 168
I was trying out the example given at http://www.haskell.org/haskellwiki/State_Monad#Complete_and_Concrete_Example_1
How this makes the solution composible is beyond my understanding. Here is what I tried but I get compile errors as follows:
Couldn't match expected type `GameValue -> StateT GameState Data.Functor.Identity.Identity b0'
with actual type `State GameState GameValue'
In the second argument of `(>>=)', namely `g2'
In the expression: g1 >>= g2
In an equation for `g3': g3 = g1 >>= g2
Failed, modules loaded: none.
Here is the code: See the end lines
module StateGame where
import Control.Monad.State
type GameValue = Int
type GameState = (Bool, Int)
-- suppose I want to play one game after the other
g1 = playGame "abcaaacbbcabbab"
g2 = playGame "abcaaacbbcabb"
g3 = g1 >>= g2
m2 = print $ evalState g3 startState
playGame :: String -> State GameState GameValue
playGame [] = do
(_, score) <- get
return score
playGame (x:xs) = do
(on, score) <- get
case x of
'a' | on -> put (on, score + 1)
'b' | on -> put (on, score - 1)
'c' -> put (not on, score)
_ -> put (on, score)
playGame xs
startState = (False, 0)
main str = print $ evalState (playGame str) startState
© Stack Overflow or respective owner