<|> in Parsec - why do these examples behave differently?

Drew

I think I'm misunderstanding <|> in parsec - I have an input stream that contains either a bunch of as in one representation or a bunch of as in another representation. I would expect the following to functions to be equivalent (given that the input is the form I said, and I have verified that it is):

foo = do
    ...
    a1s <- many $ try $ a1
    a2s <- many $ try $ a2
    return $ a1s ++ a2s

versus

foo = do
    ...
    as <- (many $ try $ a1) <|> (many $ try $ a2)
    return as

What could be going wrong? The first function works on my input, the second function fails, saying unexpected a2, expecting a1.

snak

When you give a sequence of a2 to the latter parser, the first many matches and returns an empty list, so it doesn't try to match against the second many.

You can use many1 instead.

foo = do
    ...
    as <- many1 a1 <|> many a2
    return as

In this case, the many1 fails when you give a sequence of a2, and the many matches against the input.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

<|> in Parsec - why do these examples behave differently?

From Dev

Why does "last" behave differently in Perl in these examples?

From Dev

Why does "last" behave differently in Perl in these examples?

From Java

Why do Object and var variables behave differently?

From Dev

Why do these generator expressions behave differently?

From Dev

Why do while modifiers behave differently with blocks?

From Dev

Why do these small D programs behave differently?

From Dev

Why do `vector` and `[...]` sometimes behave differently in Clojure?

From Dev

Why do Chrome and Firefox behave differently on margin?

From Dev

Why do the attributes for 'names' behave differently?

From Dev

Why do the assignments to v, w behave differently?

From Dev

Why do Object and var variables behave differently?

From Dev

Why do these small D programs behave differently?

From Dev

Why do these blocks of code behave differently?

From Dev

Why do "data Unit = Unit" and "()" behave differently in GHCi?

From Dev

Why do Java regular expressions behave differently on Linux and Windows?

From Dev

Why do value types and reference types behave differently in a loop with tasks?

From Dev

Why do interface aliases behave differently in function parameters?

From Dev

Why do hasResolution() and getStatusCode==RESOLUTION_REQUIRED behave differently in SmartLock?

From Dev

Why do null columns behave differently with the count aggragate function?

From Dev

Why do these Objective-C and Swift subclasses behave differently?

From Dev

why do bash behave differently for script from stdin?

From Dev

Why do hasResolution() and getStatusCode==RESOLUTION_REQUIRED behave differently in SmartLock?

From Dev

Do joins in Hive behave differently?

From Dev

Why does strptime() behave differently on OSX and on Linux?

From Dev

Why does this array initialize behave differently?

From Dev

Why does this constraint behave differently on different simulators?

From Dev

Why does this contextmanager behave differently with dict comprehensions?

From Dev

why char * and char array behave differently in comparison?

Related Related

  1. 1

    <|> in Parsec - why do these examples behave differently?

  2. 2

    Why does "last" behave differently in Perl in these examples?

  3. 3

    Why does "last" behave differently in Perl in these examples?

  4. 4

    Why do Object and var variables behave differently?

  5. 5

    Why do these generator expressions behave differently?

  6. 6

    Why do while modifiers behave differently with blocks?

  7. 7

    Why do these small D programs behave differently?

  8. 8

    Why do `vector` and `[...]` sometimes behave differently in Clojure?

  9. 9

    Why do Chrome and Firefox behave differently on margin?

  10. 10

    Why do the attributes for 'names' behave differently?

  11. 11

    Why do the assignments to v, w behave differently?

  12. 12

    Why do Object and var variables behave differently?

  13. 13

    Why do these small D programs behave differently?

  14. 14

    Why do these blocks of code behave differently?

  15. 15

    Why do "data Unit = Unit" and "()" behave differently in GHCi?

  16. 16

    Why do Java regular expressions behave differently on Linux and Windows?

  17. 17

    Why do value types and reference types behave differently in a loop with tasks?

  18. 18

    Why do interface aliases behave differently in function parameters?

  19. 19

    Why do hasResolution() and getStatusCode==RESOLUTION_REQUIRED behave differently in SmartLock?

  20. 20

    Why do null columns behave differently with the count aggragate function?

  21. 21

    Why do these Objective-C and Swift subclasses behave differently?

  22. 22

    why do bash behave differently for script from stdin?

  23. 23

    Why do hasResolution() and getStatusCode==RESOLUTION_REQUIRED behave differently in SmartLock?

  24. 24

    Do joins in Hive behave differently?

  25. 25

    Why does strptime() behave differently on OSX and on Linux?

  26. 26

    Why does this array initialize behave differently?

  27. 27

    Why does this constraint behave differently on different simulators?

  28. 28

    Why does this contextmanager behave differently with dict comprehensions?

  29. 29

    why char * and char array behave differently in comparison?

HotTag

Archive