[Haskell-cafe] How to represent a board?

Daniel Trstenjak daniel.trstenjak at gmail.com
Fri Jun 20 16:12:10 UTC 2014


Hi Rafael,

> 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[1] to represent
your board, a tuple (x, y) to represent a position on the board and then using
a lens[2] 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[3] I used this kind of representation.


Greetings,
Daniel

[1] https://hackage.haskell.org/package/vector
[2] https://hackage.haskell.org/package/lens
[3] https://github.com/dan-t/hsbot


More information about the Haskell-Cafe mailing list