Search code examples
parsinghaskellmaxparsec

Extracting Largest Number in Formated Textfile in Haskell


Alright, I'm not sure if there's a straight forward way to this in Haskell, but here's my predicament.

Say I have a text file that contains the following:

map z [1,2,3,4,5,6,7] Z
test x [1,2,3] X
map y [1,2,3,4,5] Y
map q [1...4] Q

What I need to do is find the largest value contained in the map "list". For instance in the above example, the highest any map goes to is 7. They are generally formatted as

map _ [] _

So I just need to find the max value held by the map in this example. Is there any straight forward way of doing this?

Thanks.


Solution

  • My plan of attack would be something like this:

    1. Sit down and carefully write out a grammar for the format I'm willing to accept.
    2. Create an ADT which can store all (and only) the information available from a successful parsing of that format.
    3. Write a Parsec parser. If you've done the previous two steps, this should be a cinch, though you'll need to learn a little bit about Parsec.
    4. Write a function which processes the ADT designed in step 2 and extracts the statistics of interest.
    5. Plumb the results of step 3 and 4 together; often, this is the most tedious and uninteresting part, but it needs to be done. =)

    Let us know how far down the list you make it before you get stuck, and we can provide some more pointed advice.