[Haskell-cafe] Lazy IO.

Sebastiaan Visser sfvisser at cs.uu.nl
Sat Jun 14 12:50:50 EDT 2008


On Jun 14, 2008, at 6:45 PM, Felipe Lessa wrote:
> (Sorry, Sebastiaan, I hit send accidentally)
>
> On Sat, Jun 14, 2008 at 1:18 PM, Sebastiaan Visser  
> <sfvisser at cs.uu.nl> wrote:
>>  readHttpMessage :: IO (Headers, Data.ByteString.Lazy.ByteString)
>>  readHttpMessage = do
>>    myStream <- <accept http connection from client>
>>    request <- hGetContents myStream
>>    header <- parseHttpHeader request
>>    bs <- Data.ByteString.Lazy.hGetContents myStream
>>    return (header, body)
>
> Why not
>
> readHttpMessage = do
>   myStream <- <accept http connection from client>
>   data <- Data.ByteString.Lazy.hGetContents myStream
>   (header, rest) <- parseHttpHeader data
>   return (header, rest)
>
> i.e. make parseHttpHeader return the rest of the string it didn't  
> parse?

Doesn't this imply that the parseHttpHeader must work on ByteStrings  
instead of regular Strings? Maybe this works for HTTP headers, but  
sometimes ByteStrings are not appropriate. Especially when you are  
not using the regular `System.IO.hGetContents' but the  
`System.IO.UTF8.hGetContents'.

> In fact, may I ask why parseHttpHeader is not a pure function?

This is not a real-life example. It might as well be a pure function.

> HTH,
>
> -- Felipe.

--
Sebastiaan


More information about the Haskell-Cafe mailing list