Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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.

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.