ReadP a -> ReadP [a] --...">

"count 3" in ReadP analyzes 3 events. How to analyze between 3 and 8 events?

ReadP has this feature:

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

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

      

I am wondering if there is a similar function to parse between 3 and 8 occurrences:

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

      

If I need to create my own, how would you do it?

+3


source to share


1 answer


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 []

      



Note the similarities with many

. The munch

ing option would use <++

instead +++

.

+4


source







All Articles