[Haskell-beginners] How to "insert" a cross reference into a tree structure

Tim Baumgartner baumgartner.tim at googlemail.com
Sun Dec 19 09:42:43 CET 2010


Hi Haskellers,

since I learned the basics of algebraic data types, I have a question.
First I thought I could answer it after getting a little more used to
Haskell's type system. But now I prefer to ask here. Given the
following example code, how can I "insert" a cross reference from the
track "Money for Nothing" to the artist "Sting" who sang the
background vocals? I know it must be a new MusicCollection, but I'd
like to know the shortest general code to construct it.

data MusicCollection = MusicCollection [Artist] deriving (Show)
data Artist = Artist String [Album] deriving (Show)
data Album  = Album  String [Track] deriving (Show)
data Track  = Track  String [Artist]
-- featured Artists are cross referenced, so Show cannot be derived

collection  = MusicCollection [direStraits, sting]
direStraits = Artist "Dire Straits" [brothersInArms]
brothersInArms  = Album "Brothers in Arms" [moneyForNothing]
moneyForNothing = Track "Money for Nothing" []

Thanks in advance
Tim



More information about the Beginners mailing list