【发布时间】:2019-07-24 08:52:38
【问题描述】:
我正在阅读 Graham Hutton "Progamming in Haskell"(剑桥出版社)的(伟大的)书的第二版。
阅读 State Monad 部分时,我偶然发现了一个我给自己的小任务。
您如何使用where 而不是let 重写以下内容?
type State = Int
newtype ST a = S (State -> (a, State))
instance Functor ST where
-- fmap :: (a -> b) -> ST a -> ST b
fmap g st = S (\state -> let (x, s') = app st state in (g x, s'))
我尝试了围绕此代码的变体,但它不起作用:
instance Functor ST where
-- fmap :: (a -> b) -> ST a -> ST b
fmap g st = S (\state -> (g x, newstate))
where (x, newstate) = app st state
我知道它本身没有用,但我想知道它是否以及如何实现。
【问题讨论】: