[Haskell-cafe] Re: I want my free (and good looking) parser!
Christian Maeder
Christian.Maeder at dfki.de
Mon Oct 6 13:07:39 EDT 2008
Slavomir Kaslev wrote:
>> freeParser = freeParser' minBound
>> where enumAll' :: (Bounded a, Enum a) => a -> [a]
>> enumAll' _ = enumAll
>> freeParser' :: (Enum a, Bounded a, Show a, Read a) => a -> Parser a
>> freeParser' x = liftM read $ choice (map (string . show) (enumAll' x))
1. I would use an explicit function argument instead of "Show" to allow
strings starting with lower case.
2. Calling read after parsing looks stupid. Just return the value shown
as parser result (within map).
3. Instead of the "string" parser, it should be checked if a further
alphaNum or '_' follows (for the longest match). And don't forget "try"!
Cheers Christian
> [Actually, in my code I use reserved' (reserved' x = reserved x >> return x)
> instead of string, where reserved is from Parsec's builtin tokenizer (which does
> some neat things behind the curtains). Here string is used just to
> illustrate the
> expamle.]
>
> The problem is that freeParser, although useful, is far from elegant. It's
> something that I came up with by trial and error. In short: it's a hack.
>
> I would like to hear your suggestions about how it can be beautified.
>
> Thank you in advance.
>
> Cheers!
>
More information about the Haskell-Cafe
mailing list