我一直在使用iterate
或repeat
以應用功能n
次看到的解決方案。但是,我沒有設法在一個國家monad中使用它。綁定功能n次單子
此代碼:
-- Stuff an empty game level with obstacles.
generateLevel :: Level -> State StdGen Level
generateLevel lvl =
placeRandomWall lvl >>= placeRandomWall >>= placeRandomWall
這一個工程,以及在意料之中:
generateLevel :: Level -> State StdGen Level
generateLevel lvl =
placeRandomWall =<< placeRandomWall =<< placeRandomWall lvl
然而,這是不一樣的:
generateLevel :: Level -> State StdGen Level
generateLevel lvl =
(placeRandomWall =<< placeRandomWall =<< placeRandomWall) lvl
最新抱怨類型。因此,我不能foldl (=<<) id (repeat 42 placeRandomWall)
,也不能我iterate
。
這是有道理的,因爲迭代採取a -> a
功能,而我已經是a -> m a
之類的東西。所以,我真的不知道該怎麼走。
編輯:我在哪裏寫repeat
,我的意思是replicate
。
**提示:**如果你有一個函數'˚F::一個 - >米A','(>> = F)::米 - >米A'可重複。 – hammar
這可能是他們如何實現(> =>)。我會看看代碼,謝謝! – Niriel