haskell:使用带有haskell平台2013 2.0.0的编写器monad时没有(Monoid Int)的实例

时间:2014-09-25 11:38:53

标签: haskell types functional-programming ghc

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) }

我使用eclipse 4.4 juno和最新的eclipsefp,haskell平台2013 2.0.0包括ghc 7.6.3,这个代码片段来自于学习你的haskell非常好

1 个答案:

答案 0 :(得分:4)

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

< S>

根据您的需要使用其中任何一种。


Oopsie。只需调换[String]Int即可。 Monad的最后一个类型参数始终是它产生的值。