<div dir="ltr">Thanks Kyle,<div><br></div><div>My initial implementation was evaluating the whole list - the current one though just returns the first successful result. Anyway, I think I need the backtracking - I would want the &quot;aaa&quot; as the result :)</div>

<div><br></div><div>I will now explore using go-routines to implement laziness.</div><div><br></div><div>Thank you so much for your input.</div><div><br></div><div>Regards,</div><div>Kashyap</div><div><div><br></div><div>

<br></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Jul 25, 2013 at 1:44 AM, Kyle Miller <span dir="ltr">&lt;<a href="mailto:kmill31415@gmail.com" target="_blank">kmill31415@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Because of laziness, you do in a sense only take the first successful value.  When I&#39;ve made parser combinators for Python before, I&#39;ve used either generators or exceptions to get lazy evaluation, since computing the whole list of possibilities for each bind would ruin the running time of the algorithm (or make it never terminate).  From your go code, it looks like you&#39;re evaluating the entire list.<div>


<br></div><div>The bind you give looks like it&#39;s for a backtracking parser.  For non-backtracking, though, I believe it would be possible to use Maybe.  Parsec has a different bind operator which only lets backtracking happen when you explicitly allow it with &#39;try&#39;.<br>


<div><br></div><div>Assuming you&#39;re wanting a full backtracking parser, here&#39;s a counterexample for the &quot;take 1&quot;:</div><div><br></div><div>needsList = do</div><div>  v &lt;- many (char &#39;a&#39;)</div>


<div>  a &lt;- return v  -- just to make sure the &quot;take 1&quot; bind happens at least once before the next step</div><div>  guard $ length a == 3</div><div>  return a</div><div><br></div><div>If my input string is &quot;aaaa&quot;, then many (char &#39;a&#39;) will produce matches of &#39;&#39;, &#39;a&#39;, &#39;aa&#39;, &#39;aaa&#39;, and &#39;aaaa&#39;, but the bind will probably force the incorrect one of these before it reaches the guard.</div>


</div><div><br></div><div>I can&#39;t guarantee this is any good, and I haven&#39;t looked at it in a while, but at [1] I have an example of using exceptions to get a parsec-like backtracking-when-explicitly-allowed parser.  I was planning on writing an article about how to do this technique, but I never got around to it.</div>


<div><br></div><div>Kyle</div><div><br></div><div>[1] <a href="https://github.com/kmill/metaview/blob/master/src/mparserlib/parser.py" target="_blank">https://github.com/kmill/metaview/blob/master/src/mparserlib/parser.py</a></div>

</div>
<div class="gmail_extra"><br><br><div class="gmail_quote"><div><div class="h5">On Wed, Jul 24, 2013 at 10:26 AM, C K Kashyap <span dir="ltr">&lt;<a href="mailto:ckkashyap@gmail.com" target="_blank">ckkashyap@gmail.com</a>&gt;</span> wrote:<br>


</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div dir="ltr">Dear Cafe,<div><br></div><div>I am trying to implement[1] parsec in go using the &quot;Monadic Parser Combinators&quot; paper [2] . I&#39;ve been able to implement &quot;plus&quot; &quot;bind&quot; and &quot;many&quot;</div>




<div>While doing the implementation - I looked at bind closely</div><div><br></div><div><div>bind :: Parser a -&gt; (a -&gt; Parser b) -&gt; Parser b</div><div>p `bind` f = \inp -&gt; concat [f v inp&#39; | (v,inp&#39;) &lt;- p inp]</div>




<div><br></div><div>I wondered if the result needs the complete list - wouldn&#39;t just the first successful value suffice?</div><div>Perhaps -</div><div>p `bind` f = \inp -&gt; take 1 $ concat [f v inp&#39; | (v,inp&#39;) &lt;- p inp]<br>




</div><div><br></div><div>Will this miss out matches?</div><div><br></div><div><br></div><div>Regards,</div><div>Kashyap</div></div><div><br></div><div>[1] <a href="https://github.com/ckkashyap/parsec/blob/master/parsec.go" target="_blank">https://github.com/ckkashyap/parsec/blob/master/parsec.go</a></div>




<div>[2] Monadic Parser Combinators: Graham Hutton, Erik Meijer</div></div>
<br></div></div><div class="im">_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></div></blockquote></div><br></div>
</blockquote></div><br></div>