Skip to content

Instantly share code, notes, and snippets.

@naoto-ogawa
Created April 16, 2018 13:19
Show Gist options
  • Save naoto-ogawa/45cd02b3580372535b5f0942f9fe9c8b to your computer and use it in GitHub Desktop.
Save naoto-ogawa/45cd02b3580372535b5f0942f9fe9c8b to your computer and use it in GitHub Desktop.
Using Megaparsec with state monad
import Control.Monad.State
import Data.Void
import Text.Megaparsec hiding (State)
import Text.Megaparsec.Char
import qualified Text.Megaparsec.Char.Lexer as L
type Parser = ParsecT Void String (State Int)
str :: String
str = "11,2,43"
main1 :: IO ()
main1 = case (runState (runParserT numbers "" str) (1::Int)) of
(Left err, s) -> putStr (parseErrorPretty err)
(Right xs, s) -> print $ (sum xs) + s
numbers :: Parser [Int]
numbers = do
x <- get
put (x+5)
integer `sepBy` char ','
integer = L.decimal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment