[Haskell-cafe] How to represent a board?
daniel.trstenjak at gmail.com
Fri Jun 20 16:12:10 UTC 2014
> Having in mind the sort of operations such games have to make on boards, I ask:
> what are the best representations for a board in Haskell?
What works pretty nicely is using a one dimensional Data.Vector to represent
your board, a tuple (x, y) to represent a position on the board and then using
a lens to transform the tuple (x, y) into an index of the one dimensional Data.Vector.
You can then write code like:
-- get board value at position (1, 2)
board ^. atCoord (1, 2)
-- set board value at position (1, 2)
board & atCoord (1, 2) .~ newValue
Having a one dimensional vector has the advantage, that's a lot nicer to e.g. map or fold over it.
In the toy example hsbot I used this kind of representation.
More information about the Haskell-Cafe