簡體   English   中英

如何為我的類型制作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 []

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM