我是函数编程的新手,并且最近在Learn Has a Haskell中学习,但是当我读完本章时,我陷入了以下程序:
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)
我将这些行保存在.hs文件中,但未能将其导入到我的ghci中,抱怨如下:
more1.hs:4:15:
Not in scope: data constructor `Writer'
Perhaps you meant `WriterT' (imported from Control.Monad.Writer)
Failed, modules loaded: none.
我通过“:info”命令检查了类型:
Prelude Control.Monad.Writer> :info Writer
type Writer w = WriterT w Data.Functor.Identity.Identity
-- Defined in `Control.Monad.Trans.Writer.Lazy'
从我的角度来看,这应该像是“ newtype Writer wa ...”之类的东西,所以我对如何馈送数据构造函数和获取Writer感到困惑。
我猜可能是与版本有关的问题,我的ghci版本是7.4.1