[Haskell-cafe] Very simple parser
ariep at xs4all.nl
Wed Jul 4 13:33:23 EDT 2007
Alexis Hazell wrote:
| This may be a stupid question, but i don't understand how (indeed, if) one
| maintain multiple states using the State monad, since 'get' etc. don't
| to require that one specify which particular copy of a State monad one
| to 'get' from, 'put' to etc.? Does one have to use (say) a tuple or a list
| contain all the states, and when one wishes to change only one of those
| states, pass that entire tuple or list to 'put' with one element changed
| the rest unchanged?
There are (at least) two ways to do this:
If the pieces of state are in any way related, I would try to put them
together in some data structure. Ideally, this structure is useful beyond
the state monad alone.
The 'gets' and 'modify' functions from Control.Monad.State can help to
keep low overhead.
If you use something like "functional references" (see
define helper functions
> getR = gets . Data.Ref.select
> modifyR = modify . Data.Ref.update
, you may deal with state like this:
> data S = S
> foo :: Foo
> bar :: Int
> $(deriveRecordRefs ''S)
> foo <- getR fooRef
> modifyR barRef succ
> return foo
The syntax can be improved, but it works OK.
If the pieces of state are unrelated, and especially if the different
states are not always present at the same time, I would stack multiple
StateT monad transformers. There was a question about this recently, see
Rules to a drinking game concerning this badge will be forthcoming.
More information about the Haskell-Cafe