考虑下一段代码 -
module Main where
data MyTree a = MyLeaf a
| MyNode (MyTree a) (MyTree a)
deriving (Show)
instance Monad MyTree where
return = MyLeaf
(MyLeaf x) >>= f = f x
(MyNode x y) >>= f = MyNode (x >>= f) (y >>= f)
main :: IO ()
main =
do
let tree1 = MyNode (MyLeaf 3) (MyNode (MyLeaf 4) (MyLeaf 5))
let tree2 = MyNode (MyLeaf "ABC") (MyNode (MyLeaf "DEFG") (MyLeaf "HIJKL"))
print (tree1 >>= (\x -> MyLeaf (x+200)))
print (tree2 >>= (\x -> MyLeaf (tail x)))
我尝试简单地实现Tree Monad,但问题是当我编译上面的代码时,我得到以下错误:
* No instance for (Applicative MyTree)
arising from the superclasses of an instance declaration
* In the instance declaration for `Monad MyTree'
我不明白缺少什么。有Applicative
的重点是什么?