[Haskell-beginners] LYAH Control.Monad.Writer tell

TJ Takei tj.takei at gmail.com
Mon Jan 16 02:07:34 CET 2012


I have a trouble to run an example of "Learn Your A Haskell.." Chap 13

import Data.Monoid
--Don't import Control.Monad.Writer

newtype Writer w a = Writer { runWriter :: (a, w) }

instance (Monoid w) => Monad (Writer w) where
    return x = Writer (x, mempty)
    (Writer (x,v)) >>= f = let (Writer (y, v')) = f x in Writer (y, v
`mappend` v')

--Define tell
tell :: [String] -> Writer [String] Int
tell w = Writer (0, w)  -- what'sa hell "0" for ???!!!

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
    tell ["Gonna multiply these two"]
    return (a*b)

main = putStrLn . show $ runWriter multWithLog

I changed two places to run it without error:
[1] Ambiguity error of Writer, uneless I comment out "import
Control.Monad.Writer", and
[2] Define tell function

My questions are:
Why does LYAH sample fail as is?
Do the changes above look reasonable?
I'm not certain about my "tell". Where is the correct instantiation of
"tell" included?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120115/690996e6/attachment.htm>

More information about the Beginners mailing list