[医]百叶草介绍了以下练习:
Maybe可以很容易地成为Applicative的一个实例;编写这样一个实例是留给读者的练习。
以下是我的实现:
data Option a = Some a
| None deriving Show
instance Functor Option where
fmap _ None = None
fmap f (Some x) = Some (f x)
instance Applicative Option where
pure x = Some x
(Some f) <*> (Some x) = Some (f x)
_ <*> _ = None 这个看上去怎么样?特别是,我很好奇Applicative Option的第二行看起来是否不错。
我应该在右边使用fmap而不是Some (f x)吗?
发布于 2014-12-01 10:54:39
看上去不错,遵循Applicative AFAICT的规则,我会把它放在这里,用fmap你需要写更多。
https://codereview.stackexchange.com/questions/71254
复制相似问题