鉴于Maybe Int
,我尝试mappend
给自己。
$let x = Just 55 :: Maybe Int
$mappend x x
<interactive>:126:1:
No instance for (Monoid Int) arising from a use of `mappend'
In the expression: mappend x x
In an equation for `it': it = mappend x x
看Maybe,我看到了:
Monoid a =&gt; Monoid(可能是a)
由于Int没有实现Monoid
类型类,这就解释了为什么我无法将mappend
与Maybe Int
一起使用。
但是,我记得LYAH我可以使用Sum
:
ghci> let x = Sum 55
ghci> mappend x x
Sum {getSum = 110}
但是,为什么不是Int
一个Monoid?