[Haskell-beginners] RDBMS like references?

martin martin.drautzburg at web.de
Tue Jun 28 18:39:46 UTC 2016


Hello all,

I recently tried to model "Items", where an Item can either be inside another Item or at a certain location. I started
like this:

data Location i l = At l
                  | In (Item i l)
                    deriving (Eq, Show)

data Item i l     = Itm i (Location i l)
                    deriving (Eq, Show)

type ItemDb i l = [Item i l]

ex_itemdb = let i1 = Itm 1 (At "a")
                i2 = Itm 2 (In i1)
            in [i1, i2]

Now I wanted to move Item 1 to a different location using this code:

moved = fmap f ex_itemdb
        where
            f (Itm 1 (At "a")) = (Itm 1 (At "b"))
            f x = x

Not this does not do what I want. I get

-- *Main> ex_itemdb
-- [Itm 1 (At "a"),Itm 2 (In (Itm 1 (At "a")))]

-- *Main> moved
-- [Itm 1 (At "b"),Itm 2 (In (Itm 1 (At "a")))]

While Item 1 has moved to "b" Item 2 still claims to be in an "Itm 1 (At "a")". I understand what's going on here and
coming from a DB background I can see that the redundant data is the root of the problem.

I can model this in a relational way such that the redundancy is avoided. But maybe there is a more haskellish/idiomatic
way to acomplish this.

Is there?



More information about the Beginners mailing list