<!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>&nbsp;</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&nbsp;<SPAN 
class=196151919-04092009>a substring of </SPAN>those&nbsp;<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>&nbsp;</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>&nbsp;</DIV>
<DIV><FONT size=2 face=Arial>&gt; import Text.ParserCombinators.Parsec<BR>&gt; 
import Data.Maybe<BR>&gt; <BR>&gt; maybePlusFile :: GenParser Char st [Maybe 
String]<BR>&gt; maybePlusFile = endBy maybePlusLine eol<BR>&gt; <BR>&gt; 
maybePlusLine :: GenParser Char st (Maybe String)<BR>&gt; maybePlusLine = try 
(do 
char('+')<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
result &lt;- many (noneOf 
"\n")<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
return $ Just 
result)<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;|&gt; do many (noneOf 
"\n")<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
return $ Nothing<BR>&gt; <BR>&gt; eol = char '\n'<BR>&gt; <BR>&gt; selectPlus :: 
String -&gt; Either ParseError [String]<BR>&gt; selectPlus input = 
<BR>&gt;&nbsp;&nbsp; case parse maybePlusFile "(input)" input 
of<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Left e -&gt; Left 
e<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Right mblist -&gt; Right $ 
catMaybes mblist</FONT></DIV>
<DIV><FONT size=2 face=Arial></FONT>&nbsp;</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>&nbsp;</DIV>
<DIV><FONT size=2 face=Arial>&gt; GHCi, version 6.10.1: <A 
href="http://www.haskell.org/ghc/">http://www.haskell.org/ghc/</A>&nbsp; :? for 
help<BR>&gt; ...<BR>&gt; Prelude&gt; :l selectPlus.hs<BR>&gt; [1 of 1] Compiling 
Main&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ( 
selectPlus.hs, interpreted )<BR>&gt; Ok, modules loaded: Main.<BR>&gt; *Main&gt; 
selectPlus "abc\n+123\ndef\n+456\n"<BR>&gt; Loading package parsec-2.1.0.1 ... 
linking ... done.<BR>&gt; Right ["123","456"]<BR>&gt; *Main&gt;</FONT></DIV>
<DIV><FONT size=2 face=Arial></FONT>&nbsp;</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>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>Thanks in advance.</DIV>
<DIV>&nbsp;</DIV>
<DIV>--Serge</DIV>
<DIV></FONT>&nbsp;</DIV>
<DIV><FONT size=2 face=Arial></FONT>&nbsp;</DIV></BODY></HTML>