Computing all Tic-Tac-Toe moves Recently I started to learn Haskell. I did this by making exercises on the Internet. The problem of making these exercises is that I never know if I solved it the correct way. I currently wrote a function which I think looks really ugly, but I don't know how to improve it. I hope that someone could give me some tips to improve it.

In this exercise I need to make a Tic-Tac-Toe game.

The question of the function I wrote is as follows:


  Exercise 8. Write a function moves :: Player -&gt; Board -&gt; [Board] that,
  given the current player and the current state of the board, returns
  all possible moves that player can make expressed as a list of
  resulting boards. (For now, you should continue making moves, even if
  one of the players has already won.)


I wrote the following code:

moves :: Player -&gt; Board -&gt; [Board]
moves p ((a,b,c),(d,e,f), (g,h,i)) = (moves' a (((symbol p),b,c), (d,e,f), (g,h,i))) ++
                                     (moves' b ((a,(symbol p),c), (d,e,f), (g,h,i))) ++
                                     (moves' c ((a,b,(symbol p)), (d,e,f), (g,h,i))) ++
                                     (moves' d ((a,b,c), ((symbol p),e,f), (g,h,i))) ++
                                     (moves' e ((a,b,c), (d,(symbol p),f), (g,h,i))) ++
                                     (moves' f ((a,b,c), (d,e,(symbol p)), (g,h,i))) ++
                                     (moves' g ((a,b,c), (d,e,f), ((symbol p),h,i))) ++
                                     (moves' h ((a,b,c), (d,e,f), (g,(symbol p),i))) ++
                                     (moves' i ((a,b,c), (d,e,f), (g,h,(symbol p))))

moves' :: Field -&gt; Board -&gt; [Board]
moves' c m = if c == B then [m] else []


Is there a way to improve my code?
First of all, you can use a where binding to clear this up:

moves :: Player -&gt; Board -&gt; [Board]
moves p ((a,b,c),(d,e,f), (g,h,i)) = (moves' a ((s,b,c), (d,e,f), (g,h,i))) ++
                                     (moves' b ((a,s,c), (d,e,f), (g,h,i))) ++
                                     (moves' c ((a,b,s), (d,e,f), (g,h,i))) ++
                                     (moves' d ((a,b,c), (s,e,f), (g,h,i))) ++
                                     (moves' e ((a,b,c), (d,s,f), (g,h,i))) ++
                                     (moves' f ((a,b,c), (d,e,s), (g,h,i))) ++
                                     (moves' g ((a,b,c), (d,e,f), (s,h,i))) ++
                                     (moves' h ((a,b,c), (d,e,f), (g,s,i))) ++
                                     (moves' i ((a,b,c), (d,e,f), (g,h,s)))
    where
        s = symbol p


Next, I would recommend changing your Board type to use a flat list instead of nested tuples, it's a lot easier to work with:

type Board = [Field]


For whatever your Field type is (for future reference please include all relevant definitions in your questions on SO, this goes for any language).  Now you can write this a bit differently:

moves :: Player -&gt; Board -&gt; [Board]
moves p [a, b, c, d, e, f, g, h, i] = moves' a [s, b, c, d, e, f, g, h, i] ++
                                      moves' b [a, s, c, d, e, f, g, h, i] ++
                                      moves' c [a, b, s, d, e, f, g, h, i] ++
                                      moves' d [a, b, c, s, e, f, g, h, i] ++
                                      moves' e [a, b, c, d, s, f, g, h, i] ++
                                      moves' f [a, b, c, d, e, s, g, h, i] ++
                                      moves' g [a, b, c, d, e, f, s, h, i] ++
                                      moves' h [a, b, c, d, e, f, g, s, i] ++
                                      moves' i [a, b, c, d, e, f, g, h, s]
    where
        s = symbol p


Now I recognize a zipWith here between the first and second arguments to moves', although we'll need a concat after the zipWith is done:

moves :: Player -&gt; Board -&gt; [Board]
moves p board@[a, b, c, d, e, f, g, h, i]
    = concat
    $ zipWith moves'
        board
        [[s, b, c, d, e, f, g, h, i],
         [a, s, c, d, e, f, g, h, i],
         [a, b, s, d, e, f, g, h, i],
         [a, b, c, s, e, f, g, h, i],
         [a, b, c, d, s, f, g, h, i],
         [a, b, c, d, e, s, g, h, i],
         [a, b, c, d, e, f, s, h, i],
         [a, b, c, d, e, f, g, s, i],
         [a, b, c, d, e, f, g, h, s]]
    where
        s = symbol p


Now, this still leaves the big block of different boards.  There is definitely a pattern here.  If you have a replace function with the type replace :: Int -&gt; a -&gt; [a] -&gt; [a] (I'll leave this as an exercise to the reader) then you could write this as

[replace 0 s board,
 replace 1 s board,
 replace 2 s board,
 replace 3 s board,
 replace 4 s board,
 replace 5 s board,
 replace 6 s board,
 replace 7 s board,
 replace 8 s board]


Or more simply

map (\i -&gt; replace i s board) [0..8]


So your entire function becomes

moves p board
    = concat $ zipWith moves' board
    $ map (\i -&gt; replace i s board) [0..8]
    where
        s = symbol p
        replace i a xs = undefined
        moves' B m = [m]
        moves' _ _ = []