[Haskell-cafe] Rethinking OO idioms
Bayley, Alistair
Alistair_Bayley at ldn.invesco.com
Thu Sep 30 04:01:06 EDT 2004
There are three pieces to this story:
- read a config file into some structure
- query/modify elements of that structure
- write structure out to a file
Create a data structure (analogous to a class with no methods) and functions
to query/manipulate that structure. Nested FiniteMaps might be a good,
simple, first attempt. Unlike the functions to read and write the file
(which must be in the IO monad), the functions to query and manipulate the
structure can (and probably should) be non-monadic.
Ben suggested using Parsec for reading the config file. Parsec is a fine
library, but there is a learning curve, and you might find it quicker to do
the parsing yourself, if it's very simple. Your call.
> ... Haskell has no mutable variables. A call like
> config.setOption("main", "initpath", "/usr") in Python -- which alters
> the state of the config object and returns nothing -- would be
> impossible in Haskell (unless perhaps the FiniteMaps are mutable
> somehow?)
When you "modify" a structure, you return a new copy of it, where the bit
that you wanted to change is replaced by the new value. For example, this is
what happens when you call addToFM/delFromFM for FiniteMaps. This isn't
necessarily as expensive as it sounds, as there's a lot of data sharing
going on in Haskell programs, due to the fact that everything /is/
immutable.
If it does turn out to be too expensive, then you could look at using a
state monad. But assuming you need one immediately looks like premature
optimisation to me.
Alistair.
-----------------------------------------
*****************************************************************
Confidentiality Note: The information contained in this
message, and any attachments, may contain confidential
and/or privileged material. It is intended solely for the
person(s) or entity to which it is addressed. Any review,
retransmission, dissemination, or taking of any action in
reliance upon this information by persons or entities other
than the intended recipient(s) is prohibited. If you received
this in error, please contact the sender and delete the
material from any computer.
*****************************************************************
More information about the Haskell-Cafe
mailing list