> On Mar 23, 2015, at 10:38 PM, Timothy Washington <twashing@gmail.com> wrote:
>
[snip]
...
You seem set on lenses. So if you write that move function like this (in case anyone’s wondering, I happen to know this isn’t a homework question):
data Piece = X | O | E deriving Show
move :: a -> b1 -> ASetter a b a1 b1 -> b
move board position piece = board & position .~ piece
This’ll work. But how are you going to get the position? If you’re given an integer based coordinate, as you seem to want from your definitions of Position, then you’re going to have to do something ugly.
Why not just go with an array and be done with it? Something like this (with your original definition of Piece):
import Data.Array
data Piece' = X | O | E deriving Show
type Position' = (Int,Int)
type Board' = Array Position’ Piece'
board' :: Board'
board' = array ((1,1),(3,3)) [((i,j), E) | i <- [1,2,3], j <- [1,2,3]]
move' :: Board' -> Piece' -> Position' -> Board'
move' board piece pos = board // [(pos, piece)]
If you want a slightly less ugly of looking at the board
import qualified Data.List.Split as S
pp board = mapM_ print $ S.chunksOf 3 $ elems board
will display the board something like:
[E,E,E]
[E,E,E]
[E,E,E]
I hope I didn’t say too much.
import Data.Arrayimport qualified Data.List.Split as Sdata Piece' = X | O | E deriving Showtype Position' = (Int,Int)type Board' = Array Position' Piece'board' :: Board'board' = array ((1,1),(3,3)) [((i,j), E) | i <- [1,2,3], j <- [1,2,3]]ppb' :: Board' -> IO()ppb' b = mapM_ print $ S.chunksOf 3 $ elems bmove' :: Board' -> Piece' -> Position' -> Board'move' board piece pos = board // [(pos, piece)]
λ> board'array ((1,1),(3,3)) [((1,1),E),((1,2),E),((1,3),E),((2,1),E),((2,2),E),((2,3),E),((3,1),E),((3,2),E),((3,3),E)]λ> let b1 = move' board' X (1,3)λ> b1array ((1,1),(3,3)) [((1,1),E),((1,2),E),((1,3),X),((2,1),E),((2,2),E),((2,3),E),((3,1),E),((3,2),E),((3,3),E)]λ> ppb' b1[E,E,X][E,E,E][E,E,E]