我正在尝试为我的ZipList编写一个Applicative实例,我收到了一些令人困惑的结果.
data List a =
Nil
| Cons a (List a)
deriving (Eq, Show)
newtype ZipList' a =
ZipList' (List a)
deriving (Eq, Show)
instance Applicative ZipList' where
pure = ZipList' . flip Cons Nil
(<*>) (ZipList' Nil) _ = ZipList' Nil
(<*>) _ (ZipList' Nil) = ZipList' Nil
(<*>) (ZipList' (Cons f fs)) (ZipList' (Cons x xs)) =
ZipList' $ Cons (f x) (fs <*> xs)
Run Code Online (Sandbox Code Playgroud)
对于长度为1或2的ZipLists,它可以正常工作:
> ZipList' (Cons (*2) (Cons (+9) Nil)) <*> ZipList' (Cons 5 (Cons …Run Code Online (Sandbox Code Playgroud)