Tuesday, April 7, 2009

Multi-dimensional arrays vs arrays of arrays in F#

Recently, I had to write a module with some array-manipulation functionality, and I decided to write it on F#

I had an array initialization function, something like this

let size = 100
let create_empty_array() = Array.create size (Array.create size 0)

Actually I meant to create an array of arrays initialized with zero. :)
But then I found that I have a lot of bugs with the functions that use that array. What an idiot I was when I was writing this!
Of course it will not work, because this code doesn't create 100 arrays of arrays, it actually initializes 1 array int[100] and then creates 100 references to the same array.

So I have to change it for the following

let create_empty_array() = Array.init size (fun i -> Array.create size 0)

The difference is that init function is evaluated value (using lambda expression) for each row, so it will really initialize 100 arrays of 100 integers

If you will use multidimensional arrays (instead of arrays of arrays) you will find that it will more convenient to use Array2 class (for 2-dimensional arrays) or Array3 (for 3-dimentional)...

let create_empty_array() = Array2.create size size 0

So what will you prefer - multidimensional arrays or arrays of arrays?

In my case I use arrays of arrays only to simplify multithreading processing if the need of it will arise in the future. I have a function that takes one row of array for some long-running processing, and I can run several threads to process rows in parallel (currently my function is not working that long, so it is only an imaginary scenario :) )

For those who is interested in some intorduction to arrays in F#, I will recommend the following post http://mariusbancila.ro/blog/?p=109

No comments: