[Haskell-cafe] How can I represent 4x4 map in haskell

Janis Voigtlaender voigt at tcs.inf.tu-dresden.de
Mon Mar 31 11:09:06 EDT 2008

iliali16 wrote:
> Just to let you know that that is not a homework just for one function
> representing the map. It is my project wich I subdivided into peaces but
> since I am not that new to haskell I am not sure if I am on the right track
> so that is my code till now:
> type Line = [Char]
> type Board = [Line]
> so my question is if this is ok to represent a map. 

That certainly is one way to represent a map. For example, for any


you will be able to access elements via b !! i !! j for some i,j from 0 
to 3.

Whether it is the best choice of representation for your application 
very much depends on what you are going to do with it. For example, 
which kind of operations on boards you are going to need.

Just to mention an alternative: if it is important for your application 
that you always have exactly 4x4 maps, and you do not want to ensure 
this invariant "by hand", then you could use something along the lines of:

 > type Quadruple a = (a,a,a,a)
 > type Line = Quadruple Char
 > type Board = Quadruple Line

Then, of course, you have to write your own "accessors" to get an 
element at position i,j.

(But there are several ways, differing in their level of complication, 
of making this more generic, rather than inventing your own Quadruple. 
Probably they are overkill for your goal.)

Ciao, Janis.

Dr. Janis Voigtlaender
mailto:voigt at tcs.inf.tu-dresden.de

More information about the Haskell-Cafe mailing list