Unable to compile Writer Monad example from "Learn you a Haskell"

The following code, which is verbatim from LYAH , does not compile. The code and compile-time error are given below. On the LYAH page, the code is ~ 15% down the page, yay emacs browser :)

Any ideas why? Can I ignore something completely obvious?

(Despite the similarities in the post headers, I think my question is different from this .)


Here's the code (in the file I called 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 is 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. 
+9
haskell monads
source share
2 answers

LYAH is deprecated in this example. You should use the writer constructor method as a smart constructor instead of the writer (now non-existent) data constructor.

To expand the bit, these data types have been updated to be more compatible with monad transformers. As a result, there is a generic WriterT for use in the monodar transform stack and a synonym for the type writer that makes up WriterT with Identity . Because of this, the data constructor is no longer associated with the type writer (since writer is a type synonym).

Fortunately, despite this complication, the solution is pretty simple: replace writer with writer .

+15
source share

The correct version in GHC 7.10.3 should be like this

 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) 
0
source share

All Articles