[Haskell-beginners] different function implementations for different values of typevar - is it possible?

Sergey Mironov ierton at gmail.com
Sat Jan 23 17:42:28 EST 2010


2010/1/24 Stephen Tetley <stephen.tetley at gmail.com>:
> Hi Sergey
>
> Are you sure you always want return to write as well?
>
> 'return' for the standard'  MTL writer monad produces _a_  within the
> monad, but does no "writing":
>
> instance (Monoid w) => Monad (Writer w) where
>    return a = Writer (a, mempty)
>    m >>= k  = Writer $ let
>        (a, w)  = runWriter m
>        (b, w') = runWriter (k a)
>        in (b, w `mappend` w')
>
> Text.XHtml.Transitional has the noHtml function which would be the
> analogue to mempty.
>
> Best wishes
>
> Stephen
>

Mmm, yes.. it is not what i want actually. Seems, that my
return-with-writing will break monad laws from "Real world Haskell".
Now its clear, that i should use simple Writer Html (). Now code looks like:

nested :: (Html -> Html) -> (Writer Html ()) -> Html
nested a x = a << ( execWriter $ x )

buildPage :: Writer Html ()
buildPage = do
    tell $ header `nested` do
        tell $ meta ! [ strAttr "http-equiv" "Content-Type", strAttr
"content" "text/html", strAttr "http-equiv" "text/html; charset=UTF-8"
]
        tell $ thetitle << "PageTitle"
        tell $ css_link ! [ href "css/blueprint/screen.css", media
"screen, projection"]
        tell $ css_link ! [ href "css/blueprint/print.css", media "print"]
        tell $ css_link ! [ href "css/custom.css" ]
    tell $ body `nested` do
        tell $ noHtml
        -- ... and so on


Thanks for warnings!
-- 
Sergey


More information about the Beginners mailing list