[Haskell-beginners] parsec question

Michael Mossey mpm at alumni.caltech.edu
Sun Jul 18 23:36:23 EDT 2010


data Command = Play Int [Int]
              | Jump Int

-- I want to parse a string that will have any of the following forms
-- and turn it into Command
--  "p"   - Produces "Play 0 []"
--  "p v55" - Produces "Play 55 []"
--  "p c123" - Produces "Play 0 [1,2,3]"
--  "p v13 c12" - Produces "Play 13 [1,2]"

-- In other words the p command can have two kinds of arguments, "v"
-- and "c", and there are defaults for the case that no argument is
-- supplied.
-- So it's going to look something like

play :: Parser Command
play = do char 'p'
           vResult <- .. maybe a v, otherwise supply default value 0
           cResult <- .. maybe a c ..
           (could the v and c be put in either order?)
           return $ Play vResult cResult


More information about the Beginners mailing list