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

前端 未结 3 1844
感情败类
感情败类 2020-12-21 15:50

I\'ve got the following, which type-checks:

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

Now, as the fu

3条回答
  •  不思量自难忘°
    2020-12-21 16:21

    p_int is a parser that produces an Int, so the type would be Parser Int or similar¹.

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

    Alternatively, you can type the read function, (read :: String -> Int) to tell the compiler which type the expression has.

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

    As for the cleaner ways, consider replacing many (char ' ') with spaces.

    ¹ ParsecT x y z Int, for example.

提交回复
热议问题