傳回第一個指定之函式會傳回 true 的項目。 如果沒有這類元素,則引發 KeyNotFoundException。
命名空間/模組路徑: Microsoft.FSharp.Collections.Array
組件:FSharp.Core (在 FSharp.Core.dll 中)
// Signature:
Array.find : ('T -> bool) -> 'T [] -> 'T
// Usage:
Array.find predicate array
參數
例外狀況
例外狀況 |
條件 |
|---|---|
如果擲回 predicate不會傳回 true的任何項目。 |
傳回值
第一個項目,如哪些 predicate傳回 true
備註
這個函式是名為 Find中 已編譯的組件。 如果從一個語言,F # 以外,或透過反映存取函式使用這個名稱。
範例
下列範例示範使用 Array.find和 Array.findIndex來識別第一個整數,則大於 1 的是方形和 Cube。
let arrayA = [| 2 .. 100 |]
let delta = 1.0e-10
let isPerfectSquare (x:int) =
let y = sqrt (float x)
abs(y - round y) < delta
let isPerfectCube (x:int) =
let y = System.Math.Pow(float x, 1.0/3.0)
abs(y - round y) < delta
let element = Array.find (fun elem -> isPerfectSquare elem && isPerfectCube elem) arrayA
let index = Array.findIndex (fun elem -> isPerfectSquare elem && isPerfectCube elem) arrayA
printfn "The first element that is both a square and a cube is %d and its index is %d." element index
平台
Windows 7、Windows Vista SP2、Windows XP SP3、Windows XP x64 SP2、Windows Server 2008 R2、Windows Server 2008 SP2、Windows Server 2003 SP2
版本資訊
F# 執行階段
支援版本:2.0、4.0
Silverlight
支援版本:3