我正在处理此问题,并且以前曾询问过相关问题。 Implementation of State Monad为了进一步优化我的代码,我试图只使用一个增量函数来实现它。State Monad示例问题
module StateExample where
import Control.Monad.State
data GlobState = GlobState { c1 :: Int, c2:: Int, c3:: Int} deriving (Show)
newGlobState:: GlobState
newGlobState = GlobState { c1=0,c2=0,c3=0 }
incr :: String-> State GlobState()
incr x = do
modify(\g -> g {x =x g + 1})
main:: IO()
main = do
let a1= flip execState newGlobState $ do
incr c1
incr c2
incr c1
print a
但是在这里我得到一个错误
`x' is not a (visible) constructor field name
如何删除这个错误?
模块Data.Label.PureM是不存在的库。我如何安装它? – 2012-01-31 12:01:11
更新了我的答案,并附上一些说明如何安装'fclabels',它可以为cabal提供'Data.Label.PureM'。 – danr 2012-01-31 13:08:51
+1镜片(或“标签”或任何你喜欢称之为的)的完美卖点, – 2012-01-31 15:36:18