newbie: maintaining relationships

I am learning haskell and i need some advice about writing functional-style code. Give the following code below, my question is - how do i setup a dependency of User on Common? Obviously what i have there is wrong, because a change in common (i know it's actually immutable) is not reflected in the version captured by User. I understand why my current approach has not a chance a of working, I am using it to, hopefully, illustrate my goal. Thanks for any suggestions
data Common = Common { cName :: String, cValue :: Int } deriving (Show) data User = User { uCommon :: Common } deriving (Show) makeWorld = (commons, users) where commons = [Common "A" 1, Common "B" 2] users = [User (head commons)]
main = do let world = makeWorld putStrLn (show world)

geoffrey wrote:
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.

Hi
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.
Sounds like a bidirectional Map to me - fortunately hackage already has one of these: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/bimap Thanks Neil

Neil Mitchell wrote:
Sounds like a bidirectional Map to me - fortunately hackage already has one of these: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/bimap
Yes, bimap is even better. Save lots of work.
participants (3)
-
Albert Y. C. Lai
-
geoffrey
-
Neil Mitchell