[Haskell-beginners] Baffled by Parsec

Felipe Lessa felipe.lessa at gmail.com
Sun Feb 22 02:37:09 EST 2009


On Sun, Feb 22, 2009 at 4:23 AM, Colin Paul Adams
<colin at colina.demon.co.uk> wrote:
> parse_integer :: Parser Int
> parse_integer = do
>  digits <- many1 digit
>  let n = read digits
>  return n

'digits' come from 'digits <- many1 digit', and 'digit' comes from
Parsec itself[1] (and is the same as 'satisfy isDigit'[2,3]). I should
note that it is more idiomatic to write 'parse_integer' as

> import Control.Monad (liftM)
>
> parse_integer :: Parse Int
> parse_integer = read `liftM` many1 digit

if you don't need signs.

HTH,

[1] http://hackage.haskell.org/packages/archive/parsec/3.0.0/doc/html/Text-Parsec-Char.html#v%3Adigit
[2] http://hackage.haskell.org/packages/archive/parsec/3.0.0/doc/html/src/Text-Parsec-Char.html#digit
[3] http://hackage.haskell.org/packages/archive/base/4.0.0.0/doc/html/Data-Char.html#v%3AisDigit

-- 
Felipe.


More information about the Beginners mailing list