[Haskell-cafe] [Parsec] A combinator to match between M and N times?

Chris Kuklewicz haskell at list.mightyreason.com
Tue Aug 29 13:08:45 EDT 2006


Udo Stenzel wrote:
> Stephane Bortzmeyer wrote:
>> Parsec provides "count n p" to run the parser p exactly n times. I'm
>> looking for a combinator "countBetween m n p" which will run the
>> parser between m and n times. It does not exist in Parsec.
> 
> infixr 2 <:>
> (<:>) = ap . ap (return (:))
> 

Again, Parsec requires you to put "try" where you need it

> countBetween 0 0 _ = return []
> countBetween 0 n p = try p <:> countBetween   0   (n-1) p <|> return []
> countBetween m n p = p <:> countBetween (m-1) (n-1) p
> 
> or what I'd ordinarily use:
> 
> (<:>) = liftM2 (:)
> 
> 


More information about the Haskell-Cafe mailing list