ReadP具有此功能:

count :: Int -> ReadP a -> ReadP [a]

-- Usage:
count 3 $ satisfy (== 'c')


我想知道是否有类似的函数可以解析3到8个事件:

count_between 3 8 $ satisfy (== 'c')


如果必须创建自己的文件,您将如何做?

最佳答案

count_between a b p = (++) <$> count a p <*> count_upto (b - a) p

count_upto 0 _ = pure []
count_upto b p = ((:) <$> p <*> count_upto (b-1) p) +++ pure []


注意与many相似。 munch ing变体将使用<++而不是+++

10-06 02:44