小编Aar*_*ron的帖子

Haskell ZipList Applicative

我正在尝试为我的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)

haskell typeclass applicative

1
推荐指数
1
解决办法
134
查看次数

标签 统计

applicative ×1

haskell ×1

typeclass ×1