Table of contents
Collapse the table of content
Expand the table of content

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

Den Delimarsky|Last Updated: 8/18/2017
1 Contributor

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


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.

Return Value

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


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.


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


Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2

Version Information

F# Core Library Versions

Supported in: 2.0, 4

See Also

Collections.Array Module (F#)

Microsoft.FSharp.Collections Namespace (F#)

© 2020 Microsoft