need help with list comprehension

Mike T. Machenry dskippy@ccs.neu.edu
Sun, 16 Feb 2003 01:43:26 -0500


Hello Everyone,

  I am writing a program to model a game. I am trying to move 5 players around
a graph such that no two players may occupy the same vertex. Players move in
a certain order, so to resolve collisions, one must know the order. I'll give
a simple example.

  Imagine a numberline with each number connect to its lesser and greater
neighbor. player one (p1) is on vertex 55 and player two (p2) is on vertex
56. p1 must move to 54 because he moves first and p2 is on the only other
option. p2 may move to either 55 or 57 because when he moves p1 is off 55.

  Alright so I am trying to use a list comprehension to model this movement on
my graph. I want to build a list of all possible ways the players could move
on one particular turn. I have a function (outlets) that gives me the neighbors
of a  node. This is a first draft function.

  GameState = GameState Int Int Int Int Int
  movePlayers :: GameState -> [GameState]
  movePlayers (GameState p1 p2 p3 p4 p5) =
    [ (GameState p1' p2' p3' p4' p5') |
      p1' <- outlets (p1),
      p2' <- outlets (p2),
      p3' <- outlets (p3),
      p4' <- outlets (p4),
      p5' <- outlets (p5)]

  This gives me all the possible combinations of moves but does not check to
make sure there were no collisions. I tried to write a function to check for
a legal state, but it was very unweildy. It had to check the inequality of
p1' against p2-p5, then p2' against p1' and p3-p4, and so on. Does anyone
know of a better way to do this?

Thanks,
-mike