title: Haskell - how to generate next move in tic tac toe game with list monad
body: <p>I am implementing a tic tac toe game for <code>n * n</code> board in Haskell and i need to generate all board configurations that i can get from next move.</p>
  
  <p>I have board defined as follows:</p>
  
  <pre><code>data Cell = E
        | X  
        | O 
        deriving (Eq,Show)
  
  type Row a = [a]
  type Board = Row (Row Cell)
  
  iniBoard :: Int -&gt; Board
  iniBoard n = let row = replicate n E in replicate n row
  </code></pre>
  
  <p>I can determine, whether given board configuration is winning for player <code>x</code>, so i have</p>
  
  <pre><code>win :: Cell -&gt; Board -&gt; Bool
  win E _   = False
  win x brd = any full $ diags brd ++ rows brd ++ cols brd
  where
      diags brd = mainDiag : [secondDiag]
      mainDiag = zipWith (!!) brd [0..]
      secondDiag = zipWith (!!) revBrd [0..]
      revBrd = do
          xs &lt;- brd
          return (reverse xs)
      rows = id
      cols = transpose
      full xs = all (==x) xs
  </code></pre>
  
  <p>But i have no idea, how to generate all board configurations that player <code>x</code> can make as next move.</p>
  
  <p>I understand, that i need to traverse all cells and check, if cell is empty, then i can place mark here and return new configuration. If i have already winning configuration, then there is no next move, and i must return empty list</p>
  
  <p>I have a code like this:</p>
  
  <pre><code>nxt :: Cell -&gt; Board -&gt; [Board]
  nxt x brd = do
  if (win x brd || win (switch x) brd)
      then
          []
      else
          undefined
  </code></pre>
  
  <p>How can i do it, using list monad? Thanks for help!</p>