Share via


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

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

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

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

// Signature:
Array.forall2 : ('T1 -> 'T2 -> bool) -> 'T1 [] -> 'T2 [] -> bool

// Usage:
Array.forall2 predicate array1 array2

Parameters

  • predicate
    Type: 'T1 -> 'T2 -> bool

    The function to test the input elements.

  • array1
    Type: 'T1 []

    The first input array.

  • array2
    Type: 'T2 []

    The second input array.

Exceptions

Exception

Condition

ArgumentException

Thrown when the input arrays differ in length.

Return Value

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

Remarks

The predicate is applied to matching elements in the two collections up to the lesser of the two lengths of the 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.

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

Example

The following example shows the use of Array.forall2 to test the equality of all the elements in two arrays.

let allEqual = Array.forall2 (fun elem1 elem2 -> elem1 = elem2)
printfn "%A" (allEqual [| 1; 2 |] [| 1; 2 |])
printfn "%A" (allEqual [| 1; 2 |] [| 2; 1 |])
true
false

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

Silverlight

Supported in: 3

See Also

Reference

Collections.Array Module (F#)

Microsoft.FSharp.Collections Namespace (F#)