[Haskell-cafe] (Newbie Question) How to get the value of an "IO String" Monad?

Lemmih lemmih at gmail.com
Fri Feb 17 09:07:36 EST 2006

On 2/17/06, Peter <peter at commonlawgov.org> wrote:
> Hello,
> For the purpose of familiarizing myself with Haskell, and also because I
> love Haskell :),
> I am trying to re-make a script that I made in Python that sends a
> request to a server and extracts the list of email addresses from a
> Mailman web-page by using an XML Parser on the page's HTML that has been
> converted to XHTML by "HTML Tidy".
> However, I cannot seem to figure out a way to get the state of a Monad;
> Specifically I cannot get the value of an "IO String" Monad.
> I have read some tutorials on Monads but I guess I must have missed
> something.
> I have read that the >>= operator is the only way to extract the state
> of an action as a string, and pipe it to a function. So far so good.
> But, That does not seem to work, because as I understand the >>=
> operator, it expects the function on the right hand side to return an IO
> Monad, which completely defeats the purpose here.
> So, How am I supposed to get the value of an IO Monad, such as "IO
> String", without returning an IO Monad?
> If this is of any help, here is the function I am stuck on:
> recv_headers' :: Socket.Socket -> String -> IO [[String]]
> recv_headers' sock bulk
>     | received == ""            = error "Connection died unexpectedly."
>     | received == "\n"
>       && endswith bulk "\r\n\r" = return [["foo", "bar"]]
>     | otherwise                 = recv_headers' sock (bulk ++ received)
>     where received = (Socket.recv sock 1)


recv_headers' sock bulk
  = do received <- Socket.recv sock 1
       case received of
         "" -> error ...
         "\n" | endswith bulk "\r\n\r" -> ...
         _ -> recv_headers' sock (bulk ++ received)


More information about the Haskell-Cafe mailing list