根据您使用的是实际的2D数组(矩形数组)还是锯齿状数组(元素为数组的数组,可能具有不同的长度),有两种选择:
如果您不确定使用哪一个,那么最好知道锯齿状阵列更快,但使用矩形阵列可能更容易(因为您确定尺寸).
let foo (ar:bool[,]) = ar.[0, 0] // Get element at specified coordinates
let bar (ar:bool[][]) = ar.[0].[0] // Get first array and then the element
Run Code Online (Sandbox Code Playgroud)
要调用这两个函数,可以使用以下语法:
// Create array of arrays and call the function
bar [| [| true |] |]
// Creates array of arrays and converts it to multi-dimensional array
// You can also use plenty of functions from 'Array2D' module
foo (array2D [| [| true |] |])
Run Code Online (Sandbox Code Playgroud)
如果要编写更通用的函数,还可以使用序列序列.这将仅与锯齿状数组兼容,但您也可以将该函数与例如列表的F#列表或任何.NET集合类型一起使用.
let woo (ar:seq<#seq<bool>>) = Seq.head (Seq.head ar)
Run Code Online (Sandbox Code Playgroud)
该#seq<..>类型意味着它可以是序列或任何其他派生类型.这对于元素类型是必需的,但对于外部类型则不需要,因为F#自动转换外部类型(但不是元素).
| 归档时间: |
|
| 查看次数: |
498 次 |
| 最近记录: |