On 7/14/07, <b class="gmail_sendername">Stefan O&#39;Rear</b> &lt;<a href="mailto:stefanor@cox.net">stefanor@cox.net</a>&gt; wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Sat, Jul 14, 2007 at 11:26:56PM -0400, David LaPalomento wrote:<br>&gt; I&#39;ve been playing with the parsers decribed in &quot;Monadic Parser Combinators&quot;<br>&gt; (<a href="http://www.cs.nott.ac.uk/~gmh/bib.html#monparsing">
http://www.cs.nott.ac.uk/~gmh/bib.html#monparsing</a>) and I&#39;ve gotten<br>&gt; stumped.&nbsp;&nbsp;I&#39;m trying to get comfortable working monadically, so please<br>&gt; excuse my ignorance.&nbsp;&nbsp;Here&#39;s the relevant portions of my code:
<br><br>You don&#39;t need to excuse yourself.&nbsp;&nbsp;If you *ever* feel as though you<br>might regret a question, we have failed.&nbsp;&nbsp;The point of this list is for<br>newbies to ask questions and learn!</blockquote><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
&gt;<br>[reordered]<br>&gt; -- word parses everything as []<br>&gt; word :: Parser String<br>&gt; word = mzero `mplus` do<br>&gt;&nbsp;&nbsp;x &lt;- letter;<br>&gt;&nbsp;&nbsp;xs &lt;- word;<br>&gt;&nbsp;&nbsp;return (x:xs)<br>&gt;<br>&gt; As I noted in the code, no matter what inputs I give it,
<br>&gt;&gt; parse word &quot;blah blah&quot;<br>&gt; always returns [].&nbsp;&nbsp;Any ideas where where my misunderstanding is?<br><br>Your base case is subtly wrong - it should be return [], not mzero.<br>Mzero always fails - mzero `mplus` x = x, by one of the MonadPlus laws.
</blockquote><div><br>Ah!&nbsp; So here&#39;s another quick question: if mzero is the identity element, why isn&#39;t it part of the Monad class?&nbsp; Correct me if I&#39;m wrong but aren&#39;t Monads (in the mathematical sense) required an identity element by definition?&nbsp;
</div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">&gt; sat :: (Char -&gt; Bool) -&gt; Parser Char<br>&gt; sat p = Parser (\inp -&gt; [ (v, out) | (v, out) &lt;- parse item inp, p v])
<br>&gt;<br>&gt; lower :: Parser Char<br>&gt; lower = Parser (\inp -&gt; parse (sat (\x -&gt; &#39;a&#39; &lt;= x &amp;&amp; x &lt;= &#39;z&#39;)) inp)<br>&gt;<br>&gt; upper :: Parser Char<br>&gt; upper = Parser (\inp -&gt; parse (sat (\x -&gt; &#39;A&#39; &lt;= x &amp;&amp; x &lt;= &#39;Z&#39;)) inp)
<br><br>These definitions aren&#39;t in the best style; while they are correct, I<br>would prefer to write them as:<br><br>sat :: (Char -&gt; Bool) -&gt; Parser Char<br>sat p = do { ch &lt;- item ; guard (p ch) ; return ch }
<br><br>lower :: Parser Char<br>lower = sat isLower<br><br>upper :: Parser Char<br>upper = sat isUpper</blockquote><div><br>Thanks for the style tips.&nbsp; Your definition is much clearer (as well as more succinct).<br></div>
<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Stefan</blockquote><div><br>Thanks again!<br>David</div></div><br>