我正在處理此問題,並且以前曾詢問過相關問題。 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