need help with list comprehension

Arjan van IJzendoorn afie@cs.uu.nl
Sun, 16 Feb 2003 11:46:05 +0100


Hi Mike,

>   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)]
>
> [...] 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?

[ (GameState p1' p2' p3' ...) | p1' <- outlets p1, p2' <- outlets p2, ... ,
               p5' <- outlets p5, allDifferent [p1', p2', p3', p4', p5']  ]

allDifferent :: Eq a => [a] -> Bool
allDifferent list = length list == length (nub list)

Not very efficient, though. (nub removes duplicates from a list, so if the
original list is just as long as with the duplicates removed, the elements
must have been unique)

Arjan