如何为我的类型制作Monoid实例?

时间:2022-05-02 22:50:33

I have the following type:

我有以下类型:

data SomeType = Var String
    deriving (Eq,Show)

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

Also, I have a function:

另外,我有一个功能:

fun1 :: TypeA -> TypeA -> TypeA

I can use this function for mappend.

我可以将此功能用于mappend。

I don't understand, how to implement Monoid interface in my case.

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

1 个解决方案

#1


If you already have fun1, just add an instance:

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

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

Probably, you want mempty = TypeA [].

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

#1


If you already have fun1, just add an instance:

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

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

Probably, you want mempty = TypeA [].

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