Share via


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

Tests if any element of the list satisfies the given predicate.

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

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

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

// Usage:
List.exists predicate list

Parameters

  • predicate
    Type: 'T -> bool

    The function to test the input elements.

  • list
    Type: 'T list

    The input list.

Return Value

true if any element satisfies the predicate. Otherwise, returns false.

Remarks

The predicate is applied to the elements of the input list. If any application returns true then the overall result is true and no further elements are tested.

This function is named Exists 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 illustrates the use of List.exists to determine whether a given element exists in a list.

// Use List.exists to determine whether there is an element of a list satisfies a given Boolean expression.
// containsNumber returns true if any of the elements of the supplied list match 
// the supplied number.
let containsNumber number list = List.exists (fun elem -> elem = number) list
let list0to3 = [0 .. 3]
printfn "For list %A, contains zero is %b" list0to3 (containsNumber 0 list0to3)

Output

For list [0; 1; 2; 3], contains zero is true

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#)