状态Monad具有`put`功能

Kev*_*ith 4 monads state haskell

纵观国家单子的维基,我试图了解runStateput功能.

据我所知runState,它需要第一个参数State,它有一个"宇宙" s,和一个值,a.它需要宇宙的第二个参数.最后,它返回一个(a, s)地方a是新的价值,s是新的宇宙?

ghci> :t runState
runState :: State s a -> s -> (a, s)
Run Code Online (Sandbox Code Playgroud)

例:

ghci> let s = return "X" :: State Int String
ghci> runState s 100
("X",100)
Run Code Online (Sandbox Code Playgroud)

但是,我不明白put结果:

ghci> runState (put 5) 1
((),5)
Run Code Online (Sandbox Code Playgroud)

既然runState返回了(a, s),为什么是a类型()

我对上面的尝试解释没有信心.请纠正我,并回答我的问题put.

小智 6

putStatemonad 一起使用时,它具有类型 s -> State s ().

put将状态设置为其参数,就是这样.至于它的返回值:它本质上是一个虚拟值,因为返回没有什么用处.

这在其定义中 也很明显put s = state $ \ _ -> ((), s).