I wrote the following repsep
parser.
It consists of (where ~
means followed by) and +
means 1 or more times.
parser ~ +(separator ~ parser)
repsep :: Parser a -> Parser a -> a -> Parser [a]
repsep p sep sepLit = fmap (intersperse sepLit) $
(\xs y -> xs ++ [y]) <$>
(oneOrMore (p <* spaces <* sep)) <*> p
To be honest, I'm actually not sure why oneOrMore
doesn't gobble up all of the tokens. But, it made sense and I tried it out:
ghci> let p = repsep (char 'F') (char ',') ','
-- no separator
ghci> runParser p "FFFFFFF"
Nothing
ghci> runParser p "F,F,F,F,F,F,F"
Just ("F,F,F,F,F,F,F","")
-- must be 1 or more
ghci> runParser p "F"
Nothing
Please critique my Haskell code.