gpt4 book ai didi

haskell:在 haskell 平台 2013 2.0.0 中使用 writer monad 时,没有 (Monoid Int) 的实例

转载 作者:行者123 更新时间:2023-12-02 14:16:29 25 4
gpt4 key购买 nike

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,此代码片段来自 learn you a haskell for Great Good

最佳答案

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

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

<小时/>

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

关于haskell:在 haskell 平台 2013 2.0.0 中使用 writer monad 时,没有 (Monoid Int) 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26037682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com