[Haskell-cafe] Re: Rethinking OO idioms

Graham Klyne GK at ninebynine.org
Thu Sep 30 05:03:49 EDT 2004


At 01:44 30/09/04 +0000, John Goerzen wrote:
>On 2004-09-29, Ben.Yu at combined.com <Ben.Yu at combined.com> wrote:
> > You can use state monad to mimic mutation.
>
>Is that really what I want?  In other words, is there a completely
>different, more Haskellish, way to look at this?
>
> > Also, take a look at the recursive decent monadic parsec library. It may
> > have done what you are trying to do.
>
>Thanks for the pointer.  I'll take a look.

Sometimes what you want really *is* a mutable value of some kind, but far 
less than you might expect.

I recently implemented an RDF/XML parser using Parsec to parse from an 
"event stream" of XMLish things.  Parsec is Monadic, and provides for user 
state in the parser, which I duly used.  But the amount of user state I 
used was tiny:

[[
data RDFParserState = RDFParserState
     { nodegen   :: Int
     , listgen   :: Int
     }
]]

I.e., just two counters that were used for generating identifiers within 
the parsing process.  Everything else was quite comfortably treated as 
immutable values.

The code can be seen at:
   http://www.ninebynine.org/Software/HaskellRDF/RDF/Harp/RDFXMLParser.hs
The function that invokes Parsec being 'parseEventsToRDF'.  (This may not 
be the best introductory example, because it touches a lot of other 
logic.  There is a test suite in the same directory if you want to dig deeper.)

#g


------------
Graham Klyne
For email:
http://www.ninebynine.org/#Contact



More information about the Haskell-Cafe mailing list