The use of two (multidimensional) dimensional arrays in Erlang

These days I am solving Project Euler problems in Erlang.

Since I am a C ++ programmer from the very beginning, sometimes I really want to code using two-dimensional arrays.

One of my ideas is to use tuples and lists like this:

List=[{X,0}||X<-lists:seq(1,3)] {1,0} {2,0} {3,0} 

Is there a good way to implement multidimensional arrays in Erlang?

+7
arrays erlang
source share
3 answers

See the array module , but for multidimensional access you need to write your own wrapper. If any of your dimensions are short and access is mostly readable, you can use tuples and use erlang:element and erlang:setelement . In any case, it is recommended to use your own shell.

+3
source share

Try an array (actually a dict) with {X, Y, Z} as the key. It looks like a 3d array;)

+2
source share

I wrote a small wrapper over an array for 2d arrays

 -module(array_2d). -export([new/2, get/3, set/4]). new(Rows, Cols)-> A = array:new(Rows), array:map(fun(_X, _T) -> array:new(Cols) end, A). get(RowI, ColI, A) -> Row = array:get(RowI, A), array:get(ColI, Row). set(RowI, ColI, Ele, A) -> Row = array:get(RowI, A), Row2 = array:set(ColI, Ele, Row), array:set(RowI, Row2, A). 
+2
source share

All Articles