8000 what does <|> means in Parser ? · Issue #92 · ekmett/trifecta · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
what does <|> means in Parser ? #92
Open
@aammfe

Description

@aammfe

Hello!
While reading Haskell book I came across trifecta

I'm trying to wrap my head around but still not able to understand <|>
in simple word (<|>) = Monadic Choose ?
p = a <|> b -- use parser a if not then use b ?
if yes then why following parser is failing ?

parseFraction :: Parser Rational
parseFraction = do
    numerator <- decimal
    char '/'
    denominator <- decimal
    case denominator of 
        0 -> fail "denominator cannot be zero"
        _ -> return (numerator % denominator)


type RationalOrDecimal = Either Rational Integer
parseRationalOrDecimal = (Left <$> parseFraction) <|> (Right<$> decimal)


main = do
    let p f i = parseString f mempty i
    print $ p (some (skipMany (oneOf "\n")  *> parseRationalOrDecimal <* skipMany (oneOf "\n"))) "10"

in perfect word if a is parseFraction is going to fail then decimal should work.
1-what I'm missing ?
2-why we need to use try when <|> should run second parser on first failure ?
parseRationalOrDecimal = try (Left <$> parseFraction) <|> (Right<$> decimal)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0