jagged Arrays
-
Wednesday, August 08, 2012 10:14 AM
why type of myjaggedArray is "int[,][]? (it should be int[][,]?)
"myjaggedArray" must be a single-dimensional jagged array that contains two two-dimensional array.
> let element1 : int[,] = array2D [| [|1; 2|]; [|3; 4|] |] let element2 : int[,] = array2D [| [|5; 6; 7 |]; [|8; 9; 10|] |];;
val element1 : int [,] = [[1; 2]
[3; 4]]val element2 : int [,] = [[5; 6; 7]
[8; 9; 10]]> let myjaggedArray = [| element1; element2 |];;
val myjaggedArray : int [,] [] = [|[[1; 2]
[3; 4]]; [[5; 6; 7]
[8; 9; 10]]|]- Edited by Rainmater Wednesday, August 08, 2012 10:15 AM
All Replies
-
Wednesday, August 08, 2012 10:36 AM
An array of integers is an int []; and array of 2D arrays of integers is an int[,] []
An int[] [,] is a 2D array of int []
- Marked As Answer by Rainmater Wednesday, August 08, 2012 7:13 PM
-
Wednesday, August 08, 2012 11:37 AM
is this inverse of what we read in C#? (in C# int[][,] is a single-dimensional jagged array that contains two two-dimensional array.)
it mean that we must read jagged arrays type backward?
-
Wednesday, August 08, 2012 12:44 PM
It's not only for jagged arrays: you can declare types in F# two ways, for example for array of list of int:
1) Explicitly in angle brackets (C# way)
let a : array<list<int>> = [| [1;2;3]; [4;5;6] |]
in this case you define types 'forward'
2) F# default way
let b : int list array = [| [1;2;3]; [4;5;6] |]
In both cases types of values are equivalent, but the last variant has less 'visual noise' and more readable
Petr
- Marked As Answer by Rainmater Wednesday, August 08, 2012 7:14 PM
-
Wednesday, August 08, 2012 1:07 PM
In both cases types of values are equivalent, but the last variant has less 'visual noise' and more readable
1) thanks, but why u say that the last variant has less visual noise and more readable? how you read "int list array"?
2) how we can create multidimensional jagged arrays using first way that you say?
- Edited by Rainmater Wednesday, August 08, 2012 1:55 PM
-
Wednesday, August 08, 2012 2:10 PM
i think this example help to understand difference of declaring types between C# and F# better:
C#:
int[,][] jaggedArray = new int[0,0][] ; Console.WriteLine("Number of dimensions is: {0}", jaggedArray.Rank);//output
Number of dimensions is: 2
F#:
let jaggedArray : int [,][] = [| |] printfn "Number of dimensions is: %d" jaggedArray.Rank
//output
Number of dimensions is: 1
-
Wednesday, August 08, 2012 2:24 PM
1) I would read this "(int list) array)" or "array of lists of int"
2) Same way you created it
Petr
-
Wednesday, August 08, 2012 6:45 PMModerator
You're right that F#'s behavior differs from C#'s here. See this characteristically excellent blog post from Eric Lippert to understand some of the issues with C#'s approach. This means that in F#, you just stick [] (or [,], etc.) at the end of the name of a type to make an array of that type (even if the inner type is an array). It does imply that indexing happens in the syntactically opposite order as the type name: if arr is an int[,][], then you get the first element via arr[0][0,0]. Declarations generally aren't a problem, since you would typically use array literals or functions in the Array module to create instances, which eliminates the third category of issues that Eric raises.- Edited by Keith BattocchiMicrosoft Contingent Staff, Moderator Wednesday, August 08, 2012 6:46 PM
- Marked As Answer by Rainmater Wednesday, August 08, 2012 7:14 PM

