如何为我的类型制作 Monoid 实例?
How to make Monoid instance for my type?
我有以下类型:
data SomeType = Var String
deriving (Eq,Show)
newtype TypeA = TypeA [(String, SomeType)]
deriving (Eq,Show)
另外,我有一个功能:
fun1 :: TypeA -> TypeA -> TypeA
我可以将此功能用于 mappend
。
我不明白,在我的例子中如何实现 Monoid 接口。
如果您已经有 fun1
,只需添加一个实例:
instance Monoid TypeA where
mempty = .... -- fill this with the neutral element
mappend = fun1
可能,您想要 mempty = TypeA []
。
我有以下类型:
data SomeType = Var String
deriving (Eq,Show)
newtype TypeA = TypeA [(String, SomeType)]
deriving (Eq,Show)
另外,我有一个功能:
fun1 :: TypeA -> TypeA -> TypeA
我可以将此功能用于 mappend
。
我不明白,在我的例子中如何实现 Monoid 接口。
如果您已经有 fun1
,只需添加一个实例:
instance Monoid TypeA where
mempty = .... -- fill this with the neutral element
mappend = fun1
可能,您想要 mempty = TypeA []
。