我想创建自己的 monad。这就是我写的:
data LeafConType a = LeafCon (a,Int,Int)
instance Monad (LeafConType ) where
return = LeafCon
lc@(LeafCon (t,i,n)) >>= f = if i>=n
then lc
else f (t,i,n)
但这不起作用。ghc 说:
leafcon.hs:26:1:
Occurs check: cannot construct the infinite type: a = (a, Int, Int)
When generalising the type(s) for `return'
In the instance declaration for `Monad LeafConType'
leafcon.hs:27:1:
Occurs check: cannot construct the infinite type: a = (a, Int, Int)
When generalising the type(s) for `>>='
In the instance declaration for `Monad LeafConType'
那有什么问题?
我想在 i 小于 n 时进行计算。n 应该是常数,因为我还不知道如何正确地做到这一点。它应该是 State 和 Maybe 的某种混合。如果你有一些建议,请随时与我分享:P