How to parse a Tuple (String,Int) in Haskell using parsec

时光怂恿深爱的人放手 提交于 2019-12-02 13:04:41

You can do this by composing from existing parsers using either the Applicative or the Monad interface. Parser has instances of both.

Using Applicative:

stringIntTuple :: Parser (String, Int)
stringIntTuple = (,) <$> yourStringParser <*> yourIntParser

Which is the same as the following:

stringIntTuple :: Parser (String, Int)
stringIntTuple = liftA2 (,) yourStringParser yourIntParser

Using Monad:

stringIntTuple :: Parser (String, Int)
stringIntTuple =
  do
    theString <- yourStringParser
    theInt <- yourIntParser
    return (theString, theInt)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!