I have a Map (Int, Int) a where a can be any type. Based on the Tuple2 stored in the array as key I wanna create a String table which stores the values of the map.
Because the maximal values of the Tuple2 are known and can be large, my first thought was to create a mutable 2D-Array with a default value. While iterating over the entries of the Map, this array could be filled and then printed out. A pseudo-code example with a Map of Ints:
map = Map (Int, Int) Int
table = Array.ofDim (maxY, maxX) withDefaultValue 0
for ((x,y), int) <- map do
table[y][x] = int
end
rows = table.rows map toString
str = rows map (toString ++ lineEnd)
print str
The only problem is: I don’t know how to do this in Haskell. Furthermore I don’t know if this is the preferred way one goes in Haskell.
I didn’t find good examples how to use 2D-Arrays in Haskell, thus can someone give me one? If there is a better way to do this, maybe with a built-in data type such as Table, Matrix, 2DStringBuilder etc., can someone show me an example how to use them?
That can be done with
Then for tabular output of any two-dimensional array,
You can also define some the functions point-free,
rowsandtableare not comfortably defined point-free, though, so I stop here.