Ser*_*pin 6 monads haskell state-monad
我有一个递归函数,它接收一个包含多个字段的数据对象,例如:
data MyState = {
first :: Int,
second :: String,
third :: Bool,
...
}
type Result = Int
Run Code Online (Sandbox Code Playgroud)
这个递归函数在执行期间改变它并将它传递给自己以备下次执行。它看起来如下:
-- This is a pseudocode, just to give an idea about the workflow.
process :: MyState -> Result
process st = go st
where go st | first == 1 = (go . changeFunc1) st
| first == 2 = (go . changeFunc2) st
| otherwise = generateResult st
changeFunc1 :: MyState -> MyState
changeFunc1 st | third st == True && second st == "abc" = st {first = inc}
| otherwise = st
where inc = first st + 1
...
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我可以使用 State monad 而不是在函数之间传递 MyState 参数,并且我需要一直包装/解包 monad。我对 Haskell 比较陌生,需要推荐,在我的情况下哪种方法更好。谢谢。
| 归档时间: |
|
| 查看次数: |
140 次 |
| 最近记录: |