The Unfun Cat
The Unfun Cat

Reputation: 32008

Return integer parsed in Parser integer >> eof using bind

Using the trifecta library, I am supposed to parse an integer string that does not contain trailing letters and return the integer parsed:

Prelude> parseString (yourFuncHere) mempty "123"
Success 123
Prelude> parseString (yourFuncHere) mempty "123abc"
Failure (interactive):1:4: error: expected: digit,
    end of input
123abc<EOF>

I have been able to do this using do notation like so:

x <- decimal
eof
return x

But I have been unsuccessful in translating this to bind/lambdas.

This does not keep the parsed number, but is correct otherwise:

decimal >> eof

I guess I should start like this

decimal >>= \x -> eof

but after this, every permutation I have tried does not work. How do I return the number parsed and check for eof using bind syntax instead of do?

Upvotes: 1

Views: 124

Answers (1)

bheklilr
bheklilr

Reputation: 54078

You would need to do

decimal >>= (\x -> (eof >> return x))

The eof combinator does not return anything, so you have to return the thing you want yourself.

Upvotes: 4

Related Questions