haskell:当将作家monad与Haskell平台2013 2.0.0一起使用时,没有实例(Monoid Int)


module Main where
import Data.Char
import Control.Monad
import Control.Monad.Trans.Writer
import Control.Applicative
import Data.Monoid
wt :: Int -> Writer Int [String]
wt x=writer(["num:"++ show x],x)

addw::Writer Int [String]
addw = do
        a <- wt 2
        b <- wt 3
        return (a*b)

有 2 个错误:

No instance for (Monoid Int) arising from a do statement
    Possible fix: add an instance declaration for (Monoid Int)
    In a stmt of a 'do' block: a <- wt 2
    In the expression:
      do { a <- wt 2;
           b <- wt 3;
           return (a * b) }
    In an equation for `addw':
        addw
          = do { a <- wt 2;
                 b <- wt 3;
                 return (a * b) }

No instance for (Num [String]) arising from a use of `*'
    Possible fix: add an instance declaration for (Num [String])
    In the first argument of `return', namely `(a * b)'
    In a stmt of a 'do' block: return (a * b)
    In the expression:
      do { a <- wt 2;
           b <- wt 3;
           return (a * b) }

我使用日食 4.4 朱诺与最新的 eclipsefp ,Haskell平台 2013 2.0.0 包括 ghc 7.6.3,此代码片段来自 学习你一个哈斯克尔 为了好

Int本身并不是一个Monoid,因为有两种可能的实现:SumProduct

根据您的需要使用它们中的任何一个。


哎呀。只需交换[String]Int.Monad 的最后一个类型参数始终是它生成的值。

最新更新