问题描述
以下代码是,不编译。代码和编译时错误包含在下面。在LYAH页面上,代码约15%,yay emacs浏览器:)
任何想法为什么?我忽略了一些完全明显的东西?
(尽管在后期标题中有相似之处,但我认为我的问题不同于。)
以下是代码(在我命名为 testcopy.hs
的文件中)
import Control.Monad.Writer
logNumber :: Int - > Writer [String] Int
logNumber x = Writer(x,[Got number:++ show x])
multWithLog :: Writer [String] Int
multWithLog =
a< - logNumber 3
b< - logNumber 5
return(a * b)
这里是编译时错误:
Prelude> :l testcopy.hs
[1的1]编译主(testcopy.hs,解释)
testcopy.hs:4:15:
不在范围内:数据构造函数`Writer'
也许你的意思是'WriterT'(从Control.Monad.Writer导入)
失败,模块加载:无。
LYAH在这个例子中已经过时了。您应该使用 writer
智能构造函数方法而不是(现在不存在的) Writer
数据构造函数。
扩展一下,这些数据类型被更新为与monad变换器更加兼容。因此,在monad变量堆栈中使用一般 WriterT
,并使用 Writer
类型同义词用标识
编写 WriterT
。因此,不再有与 Writer
类型相关联的数据构造函数(因为 Writer
是一个类型同义词)。幸运的是,尽管存在这种复杂性,解决方案非常简单:用 Writer >作家。
The following code, which is verbatim from LYAH, doesn't compile. Code and compile-time error are included below. On the LYAH page, the code is ~15% down the page, yay emacs browser :)
Any ideas why? Am I overlooking something totally obvious?
(Despite the similarity in post-titles, I think my question is different from this one.)
Here's the code (in a file which I named testcopy.hs
)
import Control.Monad.Writer
logNumber :: Int -> Writer [String] Int
logNumber x = Writer (x, ["Got number: " ++ show x])
multWithLog :: Writer [String] Int
multWithLog = do
a <- logNumber 3
b <- logNumber 5
return (a*b)
And here's the compile-time error:
Prelude> :l testcopy.hs
[1 of 1] Compiling Main ( testcopy.hs, interpreted )
testcopy.hs:4:15:
Not in scope: data constructor `Writer'
Perhaps you meant `WriterT' (imported from Control.Monad.Writer)
Failed, modules loaded: none.
LYAH is outdated in this example. You should use the writer
smart constructor method instead of the (now non-existent) Writer
data constructor.
To expand a bit, these data types were updated to be more compatible with monad transformers. As a result, there is a general WriterT
, for use in a monad transformer stack, and a Writer
type synonym that composes WriterT
with Identity
. Because of this, there is no longer a data constructor associated specifically with the Writer
type (since Writer
is a type synonym).
Luckily, despite this complication, the solution is pretty simple: replace Writer
with writer
.
这篇关于无法从“Learn you a Haskell”中编译Writer Monad示例。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!