List.findIndex<'T> 函数 (F#)

返回满足给定谓词的列表中第一个元素的索引。 如果不存在这样的元素,则引发 KeyNotFoundException

命名空间/模块路径:Microsoft.FSharp.Collections.List

程序集:FSharp.Core(在 FSharp.Core.dll 中)

// Signature:
List.findIndex : ('T -> bool) -> 'T list -> int

// Usage:
List.findIndex predicate list

参数

  • predicate
    类型:'T -> bool

    用于测试输入元素的函数。

  • list
    类型:'T list

    输入列表。

异常

异常

Condition

ArgumentException

如果谓词对列表的所有元素的计算结果都为 false,则引发此异常。

返回值

满足谓词的第一个元素的索引。

备注

此函数在编译的程序集中名为 FindIndex。 如果从 F# 以外的 .NET 语言中访问函数,或通过反射访问成员,请使用此名称。

示例

以下代码演示如何使用 List.findIndex,并将其行为与 List.find 行为进行比较。

let list1 = [ 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 = List.find (fun elem -> isPerfectSquare elem && isPerfectCube elem) list1
let index = List.findIndex (fun elem -> isPerfectSquare elem && isPerfectCube elem) list1
printfn "The first element that is both a square and a cube is %d and its index is %d." element index

Output

  

平台

Windows 8,Windows 7,Windows server 2012中,Windows server 2008 R2

版本信息

F#核心库版本

支持:2.0,4.0,可移植

请参见

参考

Collections.List 模块 (F#)

Microsoft.FSharp.Collections 命名空间 (F#)