On Sun, Jan 17, 2010 at 6:30 AM, Mark Spezzano <span dir="ltr">&lt;<a href="mailto:mark.spezzano@chariot.net.au">mark.spezzano@chariot.net.au</a>&gt;</span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
I&#39;ve written a Parser called keyword<br>
<br>
keyword :: Parser Verb<br>
keyword = do x &lt;- many1 letter<br>
                        return (read x)<br>
<br>
(read this as &quot;take-at-least-one-alphabetic-letter-and-convert-to-a-Verb-type&quot;)<br>
<br>
which DOES work provided that the user types in one of my Verbs. If they don&#39;t, well, the whole thing fails with an Exception and halts processing, returning to GHCi prompt.<br>
<br>
Question: Am I going about this the right way? I want to put together lots of &quot;data&quot; types like Verb and Noun etc so that I can build a kind of &quot;BNF grammar&quot;.<br></blockquote><div><br></div><div>Sounds good to me.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
Question: If I am going about this the right way then what do I about the &quot;read x&quot; bit failing when the user stops typing in a recognised keyword. I could catch the exception, but typing an incorrect sentence is just a typo, not really appropriate for an exception, I shouldn&#39;t think. If it IS appropriate to do this in Haskell, then how do I catch this exception and continue processing.<br>
</blockquote><div><br></div><div>In my opinion, traditional exceptions have no place in Haskell.  In some others&#39; opinions, they have their place, but are infrequently used.  In any case, you&#39;re right, this is not the time to catch an exception.</div>
<div><br></div><div>This is a usability failure on the part of the Haskell prelude.  read should have the type Read a =&gt; String -&gt; Maybe a, because failure is possible.  You can write a proper version:</div><div><br>
</div><div>import Data.Maybe (listToMaybe)</div><div>maybeRead :: Read a =&gt; String -&gt; Maybe a</div><div>maybeRead = fmap fst . listToMaybe . reads</div><div><br></div><div>Luke</div></div>