The difference is that 'a[][]
represents an array of arrays (of possibly different lengths), while in 'a[,]
, represents a rectangular 2D array. The first type is also called jagged arrays and the second type is called multidimensional arrays. The difference is the same as in C#, so you may want to look at the C# documentation for jagged arrays and multidimensional arrays. There is also an excelent documentation in the F# WikiBook.
To demonstrate this using a picture, a value of type 'a[][]
can look like this:
0 1 2 3 4
5 6
7 8 9 0 1
While a value of type a[,]
will always be a rectangle and may look for example like this:
0 1 2 3
4 5 6 7
8 9 0 1
To get a single "line" of a multidimensional array, you can use the slice notation:
let array1d = array2d.[0..0,0..9];;
Slices actually return multidimensional array (in this case, with one dimension equal to 1), so you can write a conversion function that returns 'a[]
like this:
let toArray (arr:_[,]) =
Array.init arr.Length (fun i -> arr.[0, i])
let array1d = array2d.[0..0,0..9] |> toArray;;