如何使用Parsec解析整数 [英] How to parse an Integer with parsec
本文介绍了如何使用Parsec解析整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望找到一个函数
integer :: Stream s m Char => ParsecT s u m Integer
或者甚至
natural :: Stream s m Char => ParsecT s u m Integer
在标准库中,但我没有找到.
in the standard libraries, but I did not find one.
将纯自然数直接解析为Integer
的标准方法是什么?
What is the standard way of parsing plain natural numbers directly to an Integer
?
推荐答案
这里我经常要做的就是使用表达式
Here is what I often do is to use the expression
read <$> many1 digit
,其类型可以为Stream s m Char => ParsecT s u m Integer
(或简称为Parser Integer
).
which can have type Stream s m Char => ParsecT s u m Integer
(or simply Parser Integer
).
我不喜欢使用部分函数read
,但是当解析器成功时,我知道read
将成功,并且可读性强.
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.
这篇关于如何使用Parsec解析整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文