How do I get Parsec to let me call `read` :: Int?

I have the following: check type:

p_int = liftA read (many (char ' ') *> many1 digit <* many (char ' '))

Now, as the name of the function implies, I want it to give me Int. But if I do this:

p_int = liftA read (many (char ' ') *> many1 digit <* many (char ' ')) :: Int

I get an error of this type:

Couldn't match expected type `Int' with actual type `f0 b0'
In the return type of a call of `liftA'
In the expression:
    liftA read (many (char ' ') *> many1 digit <* many (char ' ')) ::
      Int
In an equation for `p_int':
    p_int
      = liftA read (many (char ' ') *> many1 digit <* many (char ' ')) ::
          Int

Is there an easier and clearer way to parse integers that may have spaces? Or a way to fix it?

Ultimately, I want this to be part of the following:

betaLine = string "BETA " *> p_int <*> p_int  <*> p_int <*>
           p_int <*> p_parallel <*> p_exposure <* eol

which should parse lines that look like this:

BETA  6 11 5 24 -1 oiiio

So I can end up calling the BetaPair constructor, which will need these values ​​(some like Int, some like other types like [Exposure] and Parallel)

( , , , , - . !)

+2
3

p_int - , Int, Parser Int .

p_int = liftA read (many (char ' ') *> many1 digit <* many (char ' ')) :: Parser Int

read, (read :: String -> Int), , .

p_int = liftA (read :: String -> Int) (many (char ' ') *> many1 digit <* many (char ' ')) :: Int

, many (char ' ') spaces.

¹ ParsecT x y z Int, .

+5

Parsec read :: Int?

: " ".

read , , Parsec . , read Parsec, Haskell, .

LanguageDef Parsec Token, , read:

-- | Needs @foldl'@ from Data.List and 
-- @digitToInt@ from Data.Char.
--
positiveNatural :: Stream s m Char => ParsecT s u m Int
positiveNatural = 
    foldl' (\a i -> a * 10 + digitToInt i) 0 <$> many1 digit
+5

Text-Megaparsec-Lexer.integer :: MonadParsec s m Char => m Integer

, .

, , "", " ". , .

https://hackage.haskell.org/package/megaparsec-4.2.0/docs/Text-Megaparsec-Lexer.html

:

Prelude Text.Parsec Text.Parsec.Language Text.Parsec.Token> parse ( integer . makeTokenParser $ haskellStyle ) "integer" "-1234"
Right (-1234)
+1
source

Source: https://habr.com/ru/post/1544219/


All Articles