[Haskell-cafe] Very simple parser

Arie Peterson ariep at xs4all.nl
Mon Jul 2 17:46:14 EDT 2007


Gregory Propf wrote:

> As a programming exercise I'm trying to use the State monad to create a
> simple parser.  It's for a very simple assembly language for a simple
> virtual machine.  The state is a string of instructions.  I want to be
> able to call something like getNextInstruction to pull out the next
> instruction and then update the state (string).  I know I can do this
> non-monadically by just passing the string explicitly each time but I'd
> like to learn more about the State monad.  I also know about Parsec and
> Happy and so forth but this is just an exercise so I want to do it this
> way.  Any ideas?  I can't seem to get anything to work.  I've tried
> different things but I suspect I'm just missing something basic.  Can
> someone post a simple prototype for this?  Just assume the instructions
> are integers.

Did you look at the documentation for the State monad?
<http://haskell.org/ghc/docs/latest/html/libraries/mtl/Control-Monad-State-Lazy.html>
It also contains some examples.

Getting the next instruction sounds like a job for 'get'; to remove it
from the state you might use 'put' or 'modify'.

If this doesn't help, can you show something you tried?


Greetings,

Arie



More information about the Haskell-Cafe mailing list