Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse an Integer with parsec

Tags:

haskell

parsec

I was expecting to find a function

integer :: Stream s m Char => ParsecT s u m Integer

or maybe even

natural :: Stream s m Char => ParsecT s u m Integer

in the standard libraries, but I did not find one.

What is the standard way of parsing plain natural numbers directly to an Integer?

like image 709
Joachim Breitner Avatar asked Jun 11 '14 19:06

Joachim Breitner


1 Answers

Here is what I often do is to use the expression

read <$> many1 digit

which can have type Stream s m Char => ParsecT s u m Integer (or simply Parser Integer).

I don’t like the use of the the partial function read, but when the parser succeeds I know that the read will succeed, and it is somewhat readable.

like image 99
Joachim Breitner Avatar answered Sep 21 '22 22:09

Joachim Breitner