【发布时间】:2017-03-18 20:12:39
【问题描述】:
与你一起学习 Haskell 函子应用函子和幺半群章节并面临一个新问题,我的代码在这里,即使我试图解决我无法解决的问题,我的代码在这里:
class Monoid m where
mempty :: m
mappend :: m -> m -> m
instance Monoid a => Monoid (Maybe a) where
mempty = Nothing
Nothing `mappend` m = m
m `mappend` Nothing = m
Just m1 `mappend` Just m2 = Just (m1 `mappend` m2)
main = print $ Nothing `mappend` Just "andy"
这是我收到的错误消息:
No instance for (Monoid [Char]) arising from a use of `mappend'
Possible fix: add an instance declaration for (Monoid [Char])
In the second argument of `($)', namely
`Nothing `mappend` Just "andy"'
In the expression: print $ Nothing `mappend` Just "andy"
In an equation for `main':
main = print $ Nothing `mappend` Just "andy")
提前谢谢你,塔马斯
【问题讨论】:
-
这只是说您没有 [Char] 的 Monoid 实例(或者,如果您愿意,也可以是 String)。你有什么?你不显示它。
-
您已经定义了自己的类
Monoid,而不是导入Data.Monoid,其中是Monoid的实例[a]。
标签: haskell