简体   繁体   English

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

[英]How to make Monoid instance for my type?

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接口。

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 []

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM