[Haskell-cafe] A common pattern

Andrew Coppin andrewcoppin at btinternet.com
Mon May 5 04:15:08 EDT 2008


Neil Mitchell wrote:
> Hi
>
>   
>> I've found myself writing code like this several times now. Is there a
>> better way?
>>     
>
> hGetContents might be a different way to write a similar thing:
>
> read_args h = do
>      src <- hGetContents h
>      let (has,rest) = span ("#" `isPrefixOf`) $ lines src
>      return (map tail has)
>
> Of course, depending on exactly the kind of IO control you need to do,
> it may not work.
>   

Yeah, I was thinking about that. I'm not sure if I could get it to work. 
It's reading from a network socket, not a file, and I think I might need 
to be able to explicitly close it. (What happens if the other end 
closes? Do I get an exception or just the end of the string?) I might 
possibly be able to just use hGetContents and run a parser lazily over 
the result, we'll see.

I know there's a few monad-related functions that I don't know about. I 
was curios to see if any of them help in this instance...



More information about the Haskell-Cafe mailing list