[Haskell-cafe] A common pattern

Abhay Parvate abhay.parvate at gmail.com
Mon May 5 05:24:51 EDT 2008

Hi Andrew,

I don't know whether it's intentional, but the patterns for "case line of"
are not exaustive. Are you sure you do not expect anything else apart from a
single "." or a line starting with '#'?

More below:

On Mon, May 5, 2008 at 1:45 PM, Andrew Coppin <andrewcoppin at btinternet.com>

> Neil Mitchell wrote:
> > 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.
> >
> Please correct me if I am wrong; but the rest of the contents from the
handle h will be unavailable after the evaluation of this function: it goes
into a semi-closed state. (Correctly so: 'src' is supposed to have the
entire contents obtained from h if needed.)

Another minor observation: if the partial pattern in the original code was
intentional, then this is not exactly the same.

what about

read_args' :: [String] -> ([String],[String])
read_args' src = span ("#" `isPrefixOf`) $ lines src

and then using

s <- hGetContents
let (arg, rest) = read_args' $ lines s

So that you can get both the result and the remaining list of lines, in case
you need them. Again, this does not exactly stop where there is a "." on a
single line; it stops as soon as it gets a line without a '#'.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20080505/18359abc/attachment.htm

More information about the Haskell-Cafe mailing list