"计数3"在ReadP中解析3次出现。如何解析3到8次事件?

时间:2017-06-27 20:58:17

标签: haskell

ReadP具有此功能:

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

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

我想知道是否有类似的功能可以解析3到8次事件:

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

如果我必须创建自己的,你会怎么做?

1 个答案:

答案 0 :(得分:4)

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变种将使用<++代替+++