我是函数式编程的新手,最近在Learn You 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