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

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

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

Apply a function to pairs of elements drawn from the two collections, right-to-left, threading an accumulator argument through the computation. The two input arrays must have the same lengths, otherwise an ArgumentException is raised.

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

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


// Signature:
Array.foldBack2 : ('T1 -> 'T2 -> 'State -> 'State) -> 'T1 [] -> 'T2 [] -> 'State -> 'State

// Usage:
Array.foldBack2 folder array1 array2 state


folder Type: 'T1 -> 'T2 -> 'State -> 'State

The function to update the state given the input elements.

array1 Type: 'T1[]

The first input array.

array2 Type: 'T2[]

The second input array.

state Type: 'State

The initial state.

Return Value

The final state.


This function is named FoldBack2 in the .NET assembly. If accessing the member from a .NET language other than F#, or through reflection, use this name.


The following code shows how to use Array.foldBack2.

type Transaction =
    | Deposit
    | Withdrawal

let transactionTypes = [| Deposit; Deposit; Withdrawal |]
let transactionAmounts = [| 100.00; 1000.00; 95.00 |]
let initialBalance = 200.00

let endingBalance = Array.foldBack2 (fun elem1 elem2 acc ->
                        match elem1 with
                        | Deposit -> acc + elem2
                        | Withdrawal -> acc - elem2)
printfn "Ending balance: $%.2f" endingBalance


Ending balance: $1205.00


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

Version Information

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

See Also

Collections.Array Module (F#)

Microsoft.FSharp.Collections Namespace (F#)

© 2020 Microsoft