Monad Challenges: No instance for MonadState

289 views Asked by At

I'm new to Haskell and trying my hand at the monad challenges. The following code gives an error message:

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)’

While the code below runs fine, even though it should be the same thing.

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

Can someone explain why? Thanks in advance.

0

There are 0 answers