Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

List.forall2<'T1,'T2> Function (F#)

Tests if all corresponding elements of the collection satisfy the given predicate pairwise.

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

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

// Signature:
List.forall2 : ('T1 -> 'T2 -> bool) -> 'T1 list -> 'T2 list -> bool

// Usage:
List.forall2 predicate list1 list2
predicate

Type: 'T1 -> 'T2 -> bool

The function to test the input elements.

list1

Type: 'T1 list

The first input list.

list2

Type: 'T2 list

The second input list.

Exception

Condition

ArgumentException

Thrown when the input lists differ in length.

true if all of the pairs of elements satisfy the predicate. Otherwise, returns false.

The predicate is applied to matching elements in the two collections. If any application returns false then the overall result is false and no further elements are tested. Otherwise, if one collection is longer than the other then the ArgumentException exception is raised. Otherwise, true is returned.

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

The following code example illustrates the use of List.forall2.


let listEqual list1 list2 = List.forall2 (fun elem1 elem2 -> elem1 = elem2) list1 list2
printfn "%b" (listEqual [0; 1; 2] [0; 1; 2])
printfn "%b" (listEqual [0; 0; 0] [0; 1; 0])


Output

true
false

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

Supported in: 2.0, 4.0

Silverlight

Supported in: 3

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.