
Hello, I am writing some toys programs to learn and try to apply Monads properties (without success, I must say). Although I spent half a day on this code: http://hpaste.org/3957 I couldn't simplify (shorten) getStrip function. After reading "Doing it with class" ( http://www.haskell.org/all_about_monads/html/class.html ) I had an impression that I could "collapse" cases using some Monads properties. But maybe I misunderstood something. Can anyone look at it and give me a pointers?? (I don't mind if the code becomes unreadable a bit.) Thank you, Radek.

Hi Radosław,
You should be able to write this with MaybeT as follows:
getStrip :: IO ( Maybe String )
getStrip = runMaybeT $ do
pageContent <- liftIO $ downloadFile mainPageAddress
let x = patternForStrip pageContent
print x
z <- x
liftIO $ downloadFile $ mainPageAddress ++ z
If you can do without the 'print', you should be able to write it as:
getStrip :: IO ( Maybe String )
getStrip = runMaybeT $ do
pageContent <- liftIO $ downloadFile mainPageAddress
z <- patternForStrip pageContent
liftIO $ downloadFile $ mainPageAddress ++ z
You can find MaybeT here:
http://www.haskell.org/haskellwiki/New_monads/MaybeT
Best,
- Benja
On 11/18/07, Radosław Grzanka
Hello, I am writing some toys programs to learn and try to apply Monads properties (without success, I must say). Although I spent half a day on this code:
I couldn't simplify (shorten) getStrip function. After reading "Doing it with class" ( http://www.haskell.org/all_about_monads/html/class.html ) I had an impression that I could "collapse" cases using some Monads properties. But maybe I misunderstood something.
Can anyone look at it and give me a pointers?? (I don't mind if the code becomes unreadable a bit.)
Thank you, Radek. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi Benja,
You can find MaybeT here:
Thank you, that you spent some time figuring this out. This is exacly what I have expected. (This "print" was debug leftovers). Now I will try to understand how exacly this works. My big thanks to you again, Radek.

On 11/18/07, Benja Fallenstein
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

Hi Benja,
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
This is even neater. However, I fail to implement this. It does not compile and although I spent few hours trying to figure out correct usage of transformers I still fail to solve this. Here are types of expressions parseURI :: String -> Maybe URI httpGet :: URI -> IO (Maybe String) maybeT parseURI :: (Monad m) => String -> MaybeT m Network.URI.URI So far so good. However: liftIO . httpGet :: (Control.Monad.Trans.MonadIO m) => Network.URI.URI -> m (Maybe String) so it gives different type than is required here because httpGet already returns IO Maybe String. So I would need transformation from IO ( Maybe a) -> MaybeT IO a . Is there such possibility? Thank you for your spent time on this. Cheers, Radek. -- Codeside: http://codeside.org/ Przedszkole Miejskie nr 86 w Lodzi: http://www.pm86.pl/
participants (2)
-
Benja Fallenstein
-
Radosław Grzanka