我想跟蹤狀態monad中的變化。這不起作用:如何在狀態monad中使用Debug.Trace.trace?
main :: IO()
main = do
print $ snd $ execState compute initialState
traceThis :: (Show a) => a -> a
traceThis x = trace ("test: " ++ show x) x
compute :: State ([Row], Integer) String
compute = liftM traceThis $ get >>= \(rs, result) -> put (rs, result + 3) >> return "foo"
沒有被印刷(除了在主要功能的打印已經正確地被更新的最終結果)。
任何意見或方案來跟蹤狀態?我想用這個來檢查項目euler解決方案的正確性。
非常好!一旦我不再需要記錄,我可以通過刪除單個表達式來關閉它。 – somesoaccount 2012-08-07 13:32:46
@somesoaccount用更一般的解決方案更新了答案。 – 2014-08-30 07:51:13