【发布时间】:2011-02-09 05:15:16
【问题描述】:
状态单子“接口”
class MonadState s m where
get :: m s
put :: s -> m ()
(+ return and bind) 允许使用 State monad 构造任何可能的计算,而无需使用 State 构造函数。比如State $ \s -> (s+1, s-1)可以写成
do s <- get
put (s-1)
return (s+1)
同样,我永远不必使用Reader 构造函数,因为我可以使用ask、return 和(>>=) 创建该计算。确切地说:Reader f == ask >>= return . f。
对于 continuation 是否同样如此 - 是否可以使用 callCC(MonadCont 中的唯一函数)编写 Cont r a 的所有实例,返回并绑定,并且从不输入类似 Cont (\c -> ...) 的内容?
【问题讨论】:
标签: haskell monads continuations state-monad