Monad挑战:没有MonadState的实例

时间:2016-12-14 02:26:57

标签: haskell monads state-monad

我是Haskell的新手,并试着the monad challenges。以下代码给出了错误消息:

type Rand a = StateT Seed Identity a    

hi :: Rand Integer
hi = get >>= \std -> (put . snd $ rand std) >> return . fst $ rand std
-- hi = state rand

main = print $ product . fst $ runState (replicateM 5 hi) (mkSeed 1)


No instance for (MonadState Seed ((->) (Integer, Seed)))
      arising from a use of ‘put’
    In the first argument of ‘(.)’, namely ‘put’
    In the expression: put . snd
    In the first argument of ‘(>>)’, namely ‘(put . snd $ rand std)’

虽然下面的代码运行正常,但即使它应该是相同的。

hi = get >>= \std -> (put . snd $ rand std) >>= \_ -> return . fst $ rand std

有人可以解释原因吗?提前致谢。

0 个答案:

没有答案