[Haskell-beginners] Looking for a data structure

Levi Stephen levi.stephen at gmail.com
Tue Sep 16 23:54:21 EDT 2008


On Tue, Sep 16, 2008 at 3:48 PM, Alexander Dunlap <
alexander.dunlap at gmail.com> wrote:

> On Tue, 16 Sep 2008, Levi Stephen wrote:
>
>  Hi,
>> I'm looking for how best to represent selections from a set of items.
>>
>> For example, consider a soccer/football type game.
>>
>> You would have a Player data type. The game would store a player pool of
>> all
>> players in that game. Each club will have a number of players from that
>> player pool, each match will involve that club selecting a number of
>> players
>> from its list of players.
>>
>> What is the best way to structure this data and hopefully at the type
>> level
>> enforce this subset type relationship?
>>
>> Thanks,
>> Levi
>>
>>
> Hi Levi et al.,
>
> One way would be to use Data.Map or Data.IntMap to store a list of IDs and
> then have each club store a list of IDs. For example,
>
>  data Player = ...
>> type Id = ... -- probably Int or String
>> newtype Pool = Pool (Data.Map.Map Id Player)
>> data Club = Club { ..., players :: [Id] }
>>
>
> Then, you can Data.Map.lookup a particular player from an the IDs in order
> to figure out what players a club owns.
>
> Depending on how you're using it, you could also store the list of clubs
> using a particular player along with the player:
>
>  data Club = ...
>> data Player = Player { ..., clubs :: [Club] }
>> newtype Pool = Pool (Data.Set.Set Player)
>>
>
> but I think the former is probably closer to what you're looking for. Also,
> there might be more tricky ways of doing this, but this is simple and
> probably the way I'd do it: you don't have any problems enforcing the subset
> type relationship as long as you insert sane IDs.
>
> Alex


Thanks for the reply Alex, and for reminding me of simple approaches.

Is using ids and lookups like this common practice in Haskell when one type
needs to reference a possibly updating value of another type?

Thanks,
Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20080917/44e94095/attachment.htm


More information about the Beginners mailing list