当然,您可以使用sepBy
,但这不仅仅是:
interspersedParser sepP thingP = (:) <$> thingP <*> many (sepP *> thingP)
编辑:哦,这至少需要一件事。你也想要空的,所以<|> pure []
在最后贴上一个。
事实上,这基本上是如何实现sepBy1
的(其中一种变体sepBy
至少需要一个):
-- | @sepBy p sep@ parses /zero/ or more occurrences of @p@, separated
-- by @sep@. Returns a list of values returned by @p@.
--
-- > commaSep p = p `sepBy` comma
sepBy :: Alternative m => m a -> m sep -> m [a]
sepBy p sep = sepBy1 p sep <|> pure []
{-# INLINE sepBy #-}
-- | @sepBy1 p sep@ parses /one/ or more occurrences of @p@, separated
-- by @sep@. Returns a list of values returned by @p@.
sepBy1 :: Alternative m => m a -> m sep -> m [a]
sepBy1 p sep = (:) <$> p <*> many (sep *> p)
{-# INLINE sepBy1 #-}