Using applicative notation for parsers whose result is discarded

I have a question about Parsec refactoring to use an interface Applicative. Suppose I have a parser using a monadic interface as follows:

filePath0 :: GenParser Char st Info
filePath0 = do
  optional (string "./")
  r <- artist
  slash
  l <- album
  slash
  t <- track
  return $ Song r l t

I would like to turn it into something like this:

filePath :: GenParser Char st Info
filePath = Song <$> artist <*> album <*> track

But, as you can see, this is not complete. My question is: where in this refactoring version would I insert a parser optional (string "./")and slash?

+4
source share
1 answer

*> <* , ( ):

(*>) :: f a -> f b -> f b
(<*) :: f a -> f b -> f a

filePath :: GenParser Char st Info
filePath = optional "./" *> Song <$> artist <*> slash *> album <*> slash *> track
+7

Source: https://habr.com/ru/post/1659152/


All Articles