[Haskell-beginners] Parsing 'A's and then ('A's or 'B's)

Francesco Ariis fa-ml at ariis.it
Sun Jan 24 19:19:42 UTC 2016


On Sun, Jan 24, 2016 at 07:11:08PM +0100, Simon Jakobi wrote:
> I want to test whether a sequence of the characters 'A' and 'B' can
> represent a sequence of the symbols x and y where x may be represented by
> one or more 'A's and y may be represented by one or more 'A's or one or
> more 'B's.
> 
> In code, I would like to see the following:
> 
> λ> "AABB" `represents` [x, y]
> True
> λ> "AA" `represents` [x, y]
> True

Hello Simon,
    if I understood your specification correctly, there would be multiple
ways to parse the string "AAA":

    - 3 'x' elements ("A", "A", "A")
    - 2 'x' elements ("AA", "A")
    - 2 'x' elements again (first one shorter) ("A", "AA")
    - 1 'x' element ("AAA")

Which of these four should we choose? Maybe "parse as many As as possible
without consuming the A followed by a series of B"? If so, a useful
combinator is `notFollowedBy` (present in parsec, pretty sure is in
attoparsec too, if not it can be easily replicated).
Does that help?



More information about the Beginners mailing list