为了帮助我学习 Applicative Functors 和 Functors,我认为看看如何Either
使用 typeclassesFunctor
和Applicative
. 显然我可以继续阅读代码,但我发现自己尝试实现事物以更好地理解事物更有用。
仅供参考,我正在尝试实现此演示文稿结果的 Haskell 版本http://applicative-errors-scala.googlecode.com/svn/artifacts/0.6/chunk-html/index.html
无论如何,这就是我到目前为止所拥有的
data Validation a b = Success a | Failure b deriving (Show, Eq)
instance Functor (Validation a) where
fmap f (Failure x) = Failure x
fmap f (Success x) = Success (f x)
但是每当我尝试运行它时,ghci
我都会收到以下错误消息:-
[1 of 1] Compiling Main ( t.hs, interpreted )
t.hs:5:35:
Couldn't match type `b' with `a1'
`b' is a rigid type variable bound by
the type signature for
fmap :: (a1 -> b) -> Validation a a1 -> Validation a b
at t.hs:4:5
`a1' is a rigid type variable bound by
the type signature for
fmap :: (a1 -> b) -> Validation a a1 -> Validation a b
at t.hs:4:5
Expected type: a
Actual type: b
In the return type of a call of `f'
In the first argument of `Success', namely `(f x)'
In the expression: Success (f x)
t.hs:5:37:
Couldn't match type `a' with `a1'
`a' is a rigid type variable bound by
the instance declaration at t.hs:3:30
`a1' is a rigid type variable bound by
the type signature for
fmap :: (a1 -> b) -> Validation a a1 -> Validation a b
at t.hs:4:5
In the first argument of `f', namely `x'
In the first argument of `Success', namely `(f x)'
In the expression: Success
我不确定这是为什么,有人可以帮忙吗?