How can we print current state value, for debugging purposes? E.g., in code from concrete-example-1 of http://www.haskell.org/haskellwiki/State_Monad , how can we print the current state value after each input character is read?
module StateGame where
import Control.Monad.State
type GameValue = Int
type GameState = (Bool, Int)
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 = print $ evalState (playGame "abcaaacbbcabbab") startState
For quick and dirty debugging, use trace
from Debug.Trace
. I often find it useful to flip the argument order and define
infixl 0 `debug`
debug :: a -> String -> a
debug = flip trace
Then you can tack the debugging to the end of the line and it's easier to comment out (and at the end remove).
For more principled logging, combine the State
with a Writer
and tell
the logging messages or use StateT s IO
if you directly want to log to a file or stderr.