Freigeben über


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

Applies a function to corresponding elements of two collections, threading an accumulator argument through the computation. The collections must have identical sizes. If the input function is f and the elements are i0...iN and j0...jN, then this function computes f i0 j0 (...(f iN jN s)).

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

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

// Signature:
List.foldBack2 : ('T1 -> 'T2 -> 'State -> 'State) -> 'T1 list -> 'T2 list -> 'State -> 'State

// Usage:
List.foldBack2 folder list1 list2 state

Parameters

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

    The function to update the state given the input elements.

  • list1
    Type: 'T1list

    The first input list.

  • list2
    Type: 'T2list

    The second input list.

  • state
    Type: 'State

    The initial state.

Return Value

The final state value.

Exceptions

Exception

Condition

ArgumentException

Thrown when the input lists differ in length.

Remarks

This function is named FoldBack2 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 examples illustrate the difference between List.fold2 and List.foldBack2.

Example

type Transaction2 =
    | Deposit
    | Withdrawal
    | Interest

let transactionTypes2 = [Deposit; Deposit; Withdrawal; Interest]
let transactionAmounts2 = [100.00; 1000.00; 95.00; 0.05 / 12.0 ]
let initialBalance2 = 200.00

// Because fold2 processes the lists by starting at the head element, 
// the interest is calculated last, on the balance of 1205.00. 
let endingBalance2 = List.fold2 (fun acc elem1 elem2 ->
                                match elem1 with
                                | Deposit -> acc + elem2
                                | Withdrawal -> acc - elem2
                                | Interest -> acc * (1.0 + elem2))
                                initialBalance2
                                transactionTypes2
                                transactionAmounts2
printfn "%f" endingBalance2

Output

1210.020833
// Because foldBack2 processes the lists by starting at end of the list, 
// the interest is calculated first, on the balance of only 200.00. 
let endingBalance3 = List.foldBack2 (fun elem1 elem2 acc ->
                                match elem1 with
                                | Deposit -> acc + elem2
                                | Withdrawal -> acc - elem2
                                | Interest -> acc * (1.0 + elem2))
                                transactionTypes2
                                transactionAmounts2
                                initialBalance2
printfn "%f" endingBalance3

Output

1205.833333

Platforms

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

Reference

Collections.List Module (F#)

Microsoft.FSharp.Collections Namespace (F#)