2014-09-25 17 views
3
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個錯誤:哈斯克爾:沒有實例(含半幺羣智力)時使用作家單子哈斯克爾平臺2013 2.0.0

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,哈斯克爾平臺2013 2.0.0包括GHC 7.6.3,這個代碼片斷是從學習你Haskell的偉大的良好

回答

4

Int不是Monoid本身具有因有兩種可能的實現:SumProduct

根據您的需要使用它們中的任何一個。


Oopsie。只需交換[String]Int即可。 Monad的最後一個類型參數始終是它生成的值。

+0

它的工作!重量::內部 - >作家[字符串]詮釋 重量X =作家(X,[ 「民:」 ++表明X]) ADDW ::作家[字符串]詮釋 ADDW =做 一個< - 重量2 b < - wt 3 return(a * b) – doofin 2014-09-25 12:02:33