<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<TITLE>Message</TITLE>
<META name=GENERATOR content="MSHTML 8.00.6001.18812"></HEAD>
<BODY>
<DIV><FONT size=2 face=Arial>Hi Haskellers,</FONT></DIV>
<DIV> </DIV>
<DIV><FONT size=2 face=Arial>I'm asking some advice on a small piece of code
representing a<BR>simplified version of a treatment I need to perform.<BR>I have
a line-oriented string/file, from which I want to extract<BR>only <SPAN
class=196151919-04092009>a substring of </SPAN>those <SPAN
class=196151919-04092009>lines </SPAN>starting with char '+'<SPAN
class=196151919-04092009> (the detail</SPAN></FONT></DIV>
<DIV><SPAN class=196151919-04092009></SPAN><FONT face=Arial><FONT size=2><SPAN
class=196151919-04092009>of the extraction is irrelevant here, I'll just return
what follows</SPAN></FONT></FONT></DIV>
<DIV><SPAN class=196151919-04092009></SPAN><FONT face=Arial><FONT size=2><SPAN
class=196151919-04092009>the '+')</SPAN>.</FONT></FONT></DIV>
<DIV><SPAN class=196151919-04092009><FONT size=2 face=Arial>[I also simplified
the "eol" parser for shorter code.]</FONT></SPAN></DIV>
<DIV><FONT size=2 face=Arial></FONT> </DIV>
<DIV><FONT size=2 face=Arial>I came out with the code below.<BR>The line parser
returns a "Maybe String".<BR>The complete parser return a "[Maybe String]" by
mere concatenation.<BR>The main function filters the 'Nothing' with
'catMaybes'.</FONT></DIV>
<DIV><FONT size=2 face=Arial></FONT> </DIV>
<DIV><FONT size=2 face=Arial>> import Text.ParserCombinators.Parsec<BR>>
import Data.Maybe<BR>> <BR>> maybePlusFile :: GenParser Char st [Maybe
String]<BR>> maybePlusFile = endBy maybePlusLine eol<BR>> <BR>>
maybePlusLine :: GenParser Char st (Maybe String)<BR>> maybePlusLine = try
(do
char('+')<BR>>
result <- many (noneOf
"\n")<BR>>
return $ Just
result)<BR>>
<|> do many (noneOf
"\n")<BR>>
return $ Nothing<BR>> <BR>> eol = char '\n'<BR>> <BR>> selectPlus ::
String -> Either ParseError [String]<BR>> selectPlus input =
<BR>> case parse maybePlusFile "(input)" input
of<BR>> Left e -> Left
e<BR>> Right mblist -> Right $
catMaybes mblist</FONT></DIV>
<DIV><FONT size=2 face=Arial></FONT> </DIV>
<DIV><FONT size=2 face=Arial>This works as expected (or so it seems), as the
ghci dump shows:</FONT></DIV>
<DIV><FONT size=2 face=Arial></FONT> </DIV>
<DIV><FONT size=2 face=Arial>> GHCi, version 6.10.1: <A
href="http://www.haskell.org/ghc/">http://www.haskell.org/ghc/</A> :? for
help<BR>> ...<BR>> Prelude> :l selectPlus.hs<BR>> [1 of 1] Compiling
Main (
selectPlus.hs, interpreted )<BR>> Ok, modules loaded: Main.<BR>> *Main>
selectPlus "abc\n+123\ndef\n+456\n"<BR>> Loading package parsec-2.1.0.1 ...
linking ... done.<BR>> Right ["123","456"]<BR>> *Main></FONT></DIV>
<DIV><FONT size=2 face=Arial></FONT> </DIV><FONT size=2 face=Arial>
<DIV><BR>I'd like to know if this code is good style, and how you
would<BR>possibly improve it.</DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV>Thanks in advance.</DIV>
<DIV> </DIV>
<DIV>--Serge</DIV>
<DIV></FONT> </DIV>
<DIV><FONT size=2 face=Arial></FONT> </DIV></BODY></HTML>