List.findIndex<'T> Function (F#)
Visual Studio 2010
Updated: August 2010
Returns the index of the first element in the list that satisfies the given predicate. Raises KeyNotFoundException if no such element exists.
Namespace/Module Path: Microsoft.FSharp.Collections.List
Assembly: FSharp.Core (in FSharp.Core.dll)
// Signature:
List.findIndex : ('T -> bool) -> 'T list -> int
// Usage:
List.findIndex predicate list
The following code shows how to use List.findIndex and compares its behavior to that of 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
The first element that is both a square and a cube is 64 and its index is 62.