[Haskell-cafe] Recursive attoparsec

Mario Blažević mblazevic at stilo.com
Wed Oct 5 16:57:18 UTC 2016


On 2016-10-05 08:32 AM, Lian Hung Hon wrote:
> Dear cafe,
>
> Given
>
> data Expression = ExpToken String | ExpAnd Expression Expression
>
> How to write an attoparsec parser that parses this example:
>
> "a" and "b" and "c"
>
> into
>
> ExpAnd (ExpToken "a") (ExpAnd (ExpToken "b") (ExpToken "c"))?
>

You can use recursion at Haskell level:

   expParser = do left <- ExpToken <$> stringLiteral
                  (string "|" *> (ExpAnd left <$> expParser)
                   <|> pure left)



More information about the Haskell-Cafe mailing list