[Haskell-beginners] Parsing

Mike Houghton mike_k_houghton at yahoo.co.uk
Sun Mar 6 10:48:11 UTC 2016


Hi Francesco,

This is really helpful - thank you.
I’m neutral  about using a record - it is just how I’d done other simple parsers before.
Using a list of tuples seems to simplify  the parsing but then the ‘hit’ comes on converting to a record. 
However, this has made me question the necessity of a record, I suspect the processing subsequent to parsing can be better achieved by
mapping over the list.

Thanks



> On 6 Mar 2016, at 10:17, Francesco Ariis <fa-ml at ariis.it> wrote:
> 
> On Sun, Mar 06, 2016 at 09:54:34AM +0000, Mike Houghton wrote:
>> Hi Francesco,
>> Quick response! Thanks.
>> 
>> I see, so would it reduce to something like?
>> many itemValue 
>> 
>> and originally I was thinking the data structure that it would parse
>> into would be 
>> 
>> data Module = Module {— some record structure—}
>> 
>> but now it would be roughly like…
>> 
>> type Entry = (String, String)
>> 
>> data Module  = Module [Entry]
>> 
>> Thanks
> 
> Yes, it would lead to some kind of (YourType, String) association list.
> If you are more interested in a datatype with records I see two
> ways of achieving it:
> 
>    a. a function `[(YrType, String)] -> RecordsData` (not so
>       pretty but doable, also you can check for well-formedness here)
>       (Using a sum type YrType is in my opinion better than plain
>       Strings as it catches some more errors at compile time).
> 
>    b. directly via parsing, using `optionMaybe` and glue.
>       Depending on how your input is structured this may or may not be
>       more hairy (can name and source appear after an optional tag?
>       What about duplicated tags? etc.).
>       In its simplest form you can use a succinct applicative-style,
>       but the castle crumbles if want more.
> 
> 
> See which fits better (I suspect a.), play with it and report
> back; parsing has never been an elegant business!
> 
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



More information about the Beginners mailing list