[Haskell-beginners] Looking for a data structure
Alexander Dunlap
alexander.dunlap at gmail.com
Tue Sep 16 01:18:49 EDT 2008
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
More information about the Beginners
mailing list