Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Problem with Parser combinators (Baa)


----------------------------------------------------------------------

Message: 1
Date: Fri, 3 Nov 2017 13:43:40 +0200
From: Baa <aqua...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: [Haskell-beginners] Problem with Parser combinators
Message-ID: <20171103134340.58a7b386@Pavel>
Content-Type: text/plain; charset=US-ASCII

Hello, all!

I try this parser for `Read` instance:

  import qualified Text.ParserCombinators.ReadP    as P
  import qualified Text.ParserCombinators.ReadPrec as RP
  import Data.Text                                 as T

  instance Read RV where
    readPrec = parens $ do
      RP.lift P.skipSpaces
      s <- RP.look
      guard (not $ null s)
      (RP.+++) (RV1 <$> readPrec::RP.ReadPrec RV) (pure $ RV2 $ T.pack s)

And see that `+++` (or <|>) works in strange way (I don't understand
what happens). IMHO if first parser fails (i.e. `RV1 <$> readPrec::RP.ReadPrec 
RV`)
then second should return `RV2 $ T.pack s`). But seems that if 1st
fails then all fail too! I tried `+++`, `<++`, `<|>` - the same result.
How is it possible?!

I need to return something if parsing fails. Best is to return another
parser but initial problem was that alternative parser must return the
same type as first one. So, I decided to return "constant" - here it is
`RV2 $ T.pack s`. Guarding from empty string is needed for this
alternative value which I'll return when 1st parser fails. But... seems
something is wrong.


===
Best regards, Paul


------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 113, Issue 3
*****************************************

Reply via email to