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
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.
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
// 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