[Haskell-cafe] newbie: maintaining relationships
Albert Y. C. Lai
trebla at vex.net
Sat May 24 17:59:41 EDT 2008
> Give the following code below, my question is - how do i setup a
> dependency of User on Common?
Perhaps a first attempt should not have Common store a reference to
User, nor User store a reference to Common. Instead, have two
Data.Map.Map's: one looks up from Common to User, one from User to
Common. This is not the fastest possible, but it is the most
programmable possible. For example, introducing mutation is trivial. For
example, changing to many-to-many is easy (just re-code to look up from
Common to Data.Set.Set User, and from User to Data.Set.Set Common).
This is equivalent to the association pattern from OOP: Do not record
X-Y relationship inside X or Y; instead create an external association
object to store that relationship. The two Data.Map.Map's (and
Data.Set.Set's if you use them) are the external association object.
This is also equivalent to normalization in database theory. (E.g.,
think what you would do in entity-relation diagrams.)
(Aside: Some people with imperative or OOP or DB training feel that FP
is strange, e.g., laziness seem unpredictable to an imperative person,
FP program/data structuring feel strange under OOP or DB... But that is
only because they are just average in their imperative, OOP, or DB
training. For an imperative expert will see how laziness is carried out,
in great detail, and therefore totally predictable; and the truest OOP
or DB devotee will apply the most appropriate design patterns, as
exemplified above, and therefore arrive at the most FP structure as
well. This is no coincidence, for all programming paradigms are supposed
to converge in the limit.)
After you have decided how much mutation and what multiplicity you want,
you can then consider eliminating the Data.Map.Map's for speed.
More information about the Haskell-Cafe