[Haskell-cafe] Properties of monads
Benja Fallenstein
benja.fallenstein at gmail.com
Sun Nov 18 13:43:14 EST 2007
On 11/18/07, Benja Fallenstein <benja.fallenstein at gmail.com> wrote:
> Hi Radosław,
>
> You should be able to write this with MaybeT as follows:
Correction, sorry. The code in my original mail doesn't take care of
converting the 'Maybe's returned by the functions you're calling into
'MaybeT's.
The following should work, but is a little annoying:
getStrip :: IO ( Maybe String )
getStrip = runMaybeT $ do
pageContent <- MaybeT $ downloadFile mainPageAddress
z <- MaybeT $ return $ patternForStrip pageContent
MaybeT $ downloadFile $ mainPageAddress ++ z
Something like the following might feel cleaner, though:
maybeT :: Maybe a -> MaybeT m a
maybeT = MaybeT . return
downloadFile :: String -> MaybeT IO String
downloadFile s = maybeT (parseURI s) >>= liftIO . httpGet
getStrip :: MaybeT IO String
getStrip = do
pageContent <- downloadFile mainPageAddress
z <- maybeT $ patternForStrip pageContent
downloadFile $ mainPageAddress ++ z
Best,
- Benja
More information about the Haskell-Cafe
mailing list