English 中文(简体)
F# 通过多层面篮子,以发挥功能
原标题:F# pass multidimensional array of bool to function
  • 时间:2011-04-10 21:38:59
  •  标签:
  • f#

我怎么能够把一大批ool子带给F#的功能?

Thanks Maybettle

最佳回答

有两个选择,取决于您是否重新使用实际的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 

To call the two functions, you can use the following syntax:

// 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 |] |])

If you want to write function that will be more general, you can also use sequence of sequences. This will be compatible with jagged arrays only, but you can also use the function with e.g. F# lists of lists or any .NET collection types.

let woo (ar:seq<#seq<bool>>) = Seq.head (Seq.head ar)

The #seq<.> 类型是指它可以是顺序还是任何其他衍生类型。 由于F#自动投出外型(但并非部件),因此该元素类型需要这种处理,但不适用于外型。

问题回答

暂无回答




相关问题
F#: Storing and mapping a list of functions

I have a number of events that happen in a game. I want to control the time and order at which these events occur. For example: Event 1: Show some text on screen for N frames & play a sound ...

Creating Silverlight 3 applications with F#

Is there an easy way to create Silverlight 3 applications with F# (October CTP)? I have seen the F# for Silverlight, but that only works with the May CTP. I am using Visual Studio Integrated Shell ...

How To Change List of Chars To String?

In F# I want to transform a list of chars into a string. Consider the following code: let lChars = [ a ; b ; c ] If I simply do lChars.ToString, I get "[ a ; b ; c ]". I m trying to get "abc". I ...

Unzipping ZLIB compressed portions of a binary file

I m reading a file(a flash swf) from .Net 3.5 that has a header which states whether the body of the file/Stream is compressed or not. However, I m having problems-after I rewrap the basic File ...

Pretty print a tree

Let s say I have a binary tree data structure defined as follows type a tree = | Node of a tree * a * a tree | Nil I have an instance of a tree as follows: let x = Node (Node (...

F# String Pattern-Matching with Wildcards

As part of a project I have assigned myself as a way of improving my knowledge of F# and functional programming in general, I am attempting to write a string pattern-matching algorithm from scratch ...

热门标签