【发布时间】:2014-06-16 03:30:07
【问题描述】:
我正在尝试了解 Haskell 中的 Monads。 给定数据类型:
data XY a = X a | Y a
我希望 'X a >>= f' 返回 'f a' 和 'Y a >>= f' 以忽略 'f' 并返回 'Y a'。
这是我写的代码:
4 instance Monad XY where
5 return x = X x
6 (X a) >>= f = f a
7 (Y a) >>= f = Y a
这是我得到的编译器错误:
prog.hs:7:25:
Couldn't match expected type `b' with actual type `a'
`b' is a rigid type variable bound by
the type signature for >>= :: XY a -> (a -> XY b) -> XY b
at prog.hs:6:9
`a' is a rigid type variable bound by
the type signature for >>= :: XY a -> (a -> XY b) -> XY b
at prog.hs:6:9
In the first argument of `Y', namely `a'
In the expression: Y a
In an equation for `>>=': (Y a) >>= f = Y a
Failed, modules loaded: none.
你能帮我理解我缺少什么吗?
【问题讨论】: