Share via


List.tryFindIndex<'T> Function (F#)

Returns the index of the first element in the list that satisfies the given predicate. Return None if no such element exists.

Namespace/Module Path: Microsoft.FSharp.Collections.List

Assembly: FSharp.Core (in FSharp.Core.dll)

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

// Usage:
List.tryFindIndex predicate list

Parameters

  • predicate
    Type: 'T -> bool

    The function to test the input elements.

  • list
    Type: 'T list

    The input list.

Return Value

The index of the first element for which the predicate returns true, or None if every element evaluates to false.

Remarks

This function is named TryFindIndex in compiled assemblies. If you are accessing the function from a language other than F#, or through reflection, use this name.

Example

The following code example illustrates the use of List.tryFind and List.tryFindIndex.

let list1d = [1; 3; 7; 9; 11; 13; 15; 19; 22; 29; 36]
let isEven x = x % 2 = 0
match List.tryFind isEven list1d with
| Some value -> printfn "The first even value is %d." value
| None -> printfn "There is no even value in the list."

match List.tryFindIndex isEven list1d with
| Some value -> printfn "The first even value is at position %d." value
| None -> printfn "There is no even value in the list."

Output

The first even value is 22.
The first even value is at position 8.

Platforms

Windows 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2

Version Information

F# Runtime

Supported in: 2.0, 4.0

Silverlight

Supported in: 3

See Also

Reference

Collections.List Module (F#)

Microsoft.FSharp.Collections Namespace (F#)