You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
The text was updated successfully, but these errors were encountered:
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 ?
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)
The text was updated successfully, but these errors were encountered: