On Sun, Jan 17, 2010 at 6:30 AM, Mark Spezzano <mark.spezzano@chariot.net.au> wrote:
I've written a Parser called keyword

keyword :: Parser Verb
keyword = do x <- many1 letter
                       return (read x)

(read this as "take-at-least-one-alphabetic-letter-and-convert-to-a-Verb-type")

which DOES work provided that the user types in one of my Verbs. If they don't, well, the whole thing fails with an Exception and halts processing, returning to GHCi prompt.

Question: Am I going about this the right way? I want to put together lots of "data" types like Verb and Noun etc so that I can build a kind of "BNF grammar".

Sounds good to me.
 

Question: If I am going about this the right way then what do I about the "read x" 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't think. If it IS appropriate to do this in Haskell, then how do I catch this exception and continue processing.

In my opinion, traditional exceptions have no place in Haskell.  In some others' opinions, they have their place, but are infrequently used.  In any case, you're right, this is not the time to catch an exception.

This is a usability failure on the part of the Haskell prelude.  read should have the type Read a => String -> Maybe a, because failure is possible.  You can write a proper version:

import Data.Maybe (listToMaybe)
maybeRead :: Read a => String -> Maybe a
maybeRead = fmap fst . listToMaybe . reads

Luke