[Haskell-cafe] writer monad help

Andrea Vezzosi sanzhiyan at gmail.com
Sun May 20 18:54:42 EDT 2007


if by "figure out" you meant to "do what tell does without using it", you
should have written:
foo = Writer ((),"hello")
that would have had the right type, using return you are doing something
different.
in the Writer monad return :: Monoid w => a -> Writer w a
so return ((),"hello") :: Monoid w => Writer w ((),String)
in fact in this case return a = Writer (a,mempty)
so return ((),"hello") == Writer (((),"hello"),mempty)
in general you can't "mess" with the internals of a monad using return, it's
only meant to lift a pure value in that monad.
That's also a reason because tell is a typeclass method, you need to know
how your specific monad data type work to implement it.

full source:
http://darcs.haskell.org/packages/mtl/Control/Monad/Writer/Lazy.hs


2007/5/18, iskaldur <iskaldur at gmail.com>:
>
>
> I'm trying to learn to use the Writer Monad, but I'm having trouble with
> the
> following very simple program:
>
> import Control.Monad.Writer
> foo :: Writer String Int
> foo = tell "hello"
>
> Basically, I was trying to figure out the 'tell' function, so I want foo
> to
> return ((), "hello").
>
> But I get this error:
> Couldn't match `Int' against `()'
>       Expected type: Writer String Int
>       Inferred type: Writer String ()
>     In the application `tell "hello"'
>     In the definition of `foo': foo = tell "hello"
>
> What am I supposed to do? Write my own version of tell for Writer String
> Int? How exactly do I do this?
> --
> View this message in context:
> http://www.nabble.com/writer-monad-help-tf3777133.html#a10680445
> Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20070521/1bae463c/attachment.htm


More information about the Haskell-Cafe mailing list