Typeclassopedia呈現以下運動:實施Monoidal在應用型方面
實現純和(< *>)的單元和方面(**),並且反之亦然。
這裏的Monoidal
和MyApplicative
:
class Functor f => Monoidal f where
u :: f() -- using `u` rather than `unit`
dotdot :: f a -> f b -> f (a,b) -- using instead of `(**)`
class Functor f => MyApplicative f where
p :: a -> f a -- using instead of `pure`
apply :: f (a -> b) -> f a -> f b -- using instead of `(<**>)`
首先,讓我展現Maybe
式的數據類型:
data Option a = Some a
| None deriving Show
然後,我定義instance MyApplicative Option
:
instance MyApplicative Option where
p = Some
apply None _ = None
apply _ None = None
apply (Some g) f = fmap g f
最後,我在執行中的p
方面Monoidal Option
和apply
的MyApplicative
嘗試:
instance Monoidal Option where
u = p()
dotdot None _ = None
dotdot _ None = None
dotdot (Some x) (Some y) = Some id <*> Some (x, y)
這是正確的?我的dotdot
與apply
實現似乎並不
instance Monoidal Option where
u = p()
dotdot None _ = None
dotdot _ None = None
dotdot (Some x) (Some y) = apply (Some id) (Some (x, y))
特別,我很好奇如何正確實現dotdot :: f a -> f b -> f (a, b)
與應用型的(<*>)
- 在我的情況下,它是apply
。
大部分是在下面的答案http://stackoverflow.com/a/23320391/414413 – Cirdec 2014-12-03 01:44:23
這一解釋之初連忙解釋是相當不錯的 - 感謝。謹慎張貼在這裏信用? – 2014-12-03 01:54:46