1

我有以下类型:

data SomeType = Var String
    deriving (Eq,Show)

newtype TypeA = TypeA [(String, SomeType)]
  deriving (Eq,Show)

另外,我有一个功能:

fun1 :: TypeA -> TypeA -> TypeA

我可以将此功能用于mappend.

我不明白,在我的情况下如何实现 Monoid 接口。

4

1 回答 1

1

如果您已经拥有fun1,只需添加一个实例:

instance Monoid TypeA where
   mempty = .... -- fill this with the neutral element
   mappend = fun1

可能,你想要mempty = TypeA [].

于 2015-05-13T17:30:50.060 回答