【发布时间】:2020-03-09 18:40:07
【问题描述】:
我正在尝试为这种类型编写 Applicative
data Choice a = ColumnA a | ColumnB a
我写了一个 Functor 实例:
instance Functor Choice where
fmap f (ColumnA a ) = (ColumnA (f a) )
fmap f (ColumnB a ) = (ColumnB (f a) )
现在我想写 Applicative,其中 ColumnB 被认为是“正确的值”,ColumnA 被认为是某种错误。
我试过了
instance Applicative Choice where
pure = ColumnB
ColumnB f <*> r = fmap f r
ColumnA f <*> _ = ColumnA f --- this does not work
我怎样才能让它工作?
【问题讨论】:
-
你需要查看类型:在左边你有一个
Choice (a -> b),在右边你有一个(Choice a),它需要一个Choice b。所以返回Column A确实不是一种选择。 -
“这不起作用”是什么意思?你得到什么错误?
标签: haskell applicative