结果状态是提供的参数并且值为单位的状态

State where the resulting state is the argument provided and the value is unit

haskell 书中的练习 23.8.2 要求我构建如下状态:

put' :: s -> State s ()
put' s = undefined
-- should act like:
-- Prelude> runState (put "blah") "woot"
-- ((),"blah")

我唯一能进行类型检查的实现是

import Control.Monad.Trans.State -- Not sure this is the right import
put' :: s -> State s ()
put' s = state $ \s -> ((), s)

但是这个 returns runState 参数中的状态,而不是 put':

λ> runState (put' "blah") "woot"
((),"woot")

我需要什么 haskell 技巧来解决这个问题?看不到我怎么访问"blah".

put' s = state $ \s -> ((), s)
     ^            ^

您为两个不同的绑定重复使用了变量 s。尝试使用不同的名称,解决方案将是显而易见的 ;-)

顺便说一句,您应该在 GHC / GHCi 中使用 -Wall 标志启用警告。这会指出您已经定义了 s twise,并且第二个绑定隐藏了第一个绑定。