Zero or Many Parser

list / many1

Let's now write two functions. One takes a parser and applies itself zero or many times (list) and the other takes a parser and applies itself one or many times (many1). These functions are useful for such things as parsing white-space, where we may take a parser of a single white-space character and obtain a parser of e.g. one or many white-space characters.

list :: Parser a -> Parser [a]
list k = many1 k ||| value []

many1 :: Parser a -> Parser [a]
many1 k = bindParser k (\k' -> mapParser (list k) (\kk' -> k' : kk'))