[Haskell-cafe] Is this Parsec code idiomatic?

Serge LE HUITOUZE slehuitouze at telisma.com
Fri Sep 4 15:23:35 EDT 2009

Hi Haskellers,
I'm asking some advice on a small piece of code representing a
simplified version of a treatment I need to perform.
I have a line-oriented string/file, from which I want to extract
only a substring of those lines starting with char '+' (the detail
of the extraction is irrelevant here, I'll just return what follows
the '+').
[I also simplified the "eol" parser for shorter code.]
I came out with the code below.
The line parser returns a "Maybe String".
The complete parser return a "[Maybe String]" by mere concatenation.
The main function filters the 'Nothing' with 'catMaybes'.
> import Text.ParserCombinators.Parsec
> import Data.Maybe
> maybePlusFile :: GenParser Char st [Maybe String]
> maybePlusFile = endBy maybePlusLine eol
> maybePlusLine :: GenParser Char st (Maybe String)
> maybePlusLine = try (do char('+')
>                         result <- many (noneOf "\n")
>                         return $ Just result)
>             <|> do many (noneOf "\n")
>                    return $ Nothing
> eol = char '\n'
> selectPlus :: String -> Either ParseError [String]
> selectPlus input = 
>   case parse maybePlusFile "(input)" input of
>        Left e -> Left e
>        Right mblist -> Right $ catMaybes mblist
This works as expected (or so it seems), as the ghci dump shows:
> GHCi, version 6.10.1: http://www.haskell.org/ghc/  :? for help
> ...
> Prelude> :l selectPlus.hs
> [1 of 1] Compiling Main             ( selectPlus.hs, interpreted )
> Ok, modules loaded: Main.
> *Main> selectPlus "abc\n+123\ndef\n+456\n"
> Loading package parsec- ... linking ... done.
> Right ["123","456"]
> *Main>

I'd like to know if this code is good style, and how you would
possibly improve it.
Thanks in advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090904/ce2c2fe2/attachment.html

More information about the Haskell-Cafe mailing list