[Haskell-beginners] Parsing

Francesco Ariis fa-ml at ariis.it
Sun Mar 6 10:17:19 UTC 2016


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!



More information about the Beginners mailing list