我正在尝试创建Btree
单子,但是我不知道如何为此定义return
,因为我想将值放在Node (Leaf value)
中,而将另一个保留分支为空。
module Tree where
import Control.Monad
data Btree a=Node (Btree a)(Btree a)|Leaf a
如果我想在Monad中插入一个值,则会出现问题:
instance Monad Btree where
(>>=) (Leaf x) f = f x
(>>=) (Node a b) f=Node (a>>=f) (b>>=f)
return t=Node (Leaf t) # i want to partially apply `Node` to not have to specify the other branch . Do i need an additional ADT ? `Leaf/Node/Root` ? for single elements ?
PS 当尝试使用return=Leaf
或return t=Node (Leaf t)(Leaf t)
运行此程序时,我也收到错误消息:
* No instance for (Applicative Btree)
arising from the superclasses of an instance declaration
* In the instance declaration for `Monad Btree'