0

In class, we wrote parsers by defining our own Parser type and this gave us a lot of flexibility. For example, if we wanted to make code and parse out "[at]" as '@', we could write

atParser = Parser $ \s ->
    case s of
        w:x:y:z:zs ->
            | (w:x:y:z:[]) == "[at]" = ['@',zs]
            | otherwise = []
        zs -> []

However, I cannot figure out how to implement this sort of parser using Text.ParserCombinators. Is it possible?

Thanks

1 Answer 1

1

I believe you're looking for the string combinator.

λ> :set -package parsec
package flags have changed, resetting and loading new packages...

λ> import Text.Parsec

λ> :t string
string :: Stream s m Char => String -> ParsecT s u m String

λ> parse (string "[at]") "" "[at]"
Right "[at]"

λ> parse (string "[at]") "" "[at"
Left (line 1, column 1):
unexpected end of input
expecting "[at]"

λ> parse ('@' <$ string "[at]") "" "[at]"
Right '@'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.