List.foldBack2<'T1,'T2,'State> 函式 (F#)
更新:2010 年 5 月
將函式套用至兩個集合的對應項目,以透過計算建立累計值引數的執行緒。 這些集合的大小必須完全相同。 如果輸入的函式為 f和各個元素,是 i0...iN和 j0...jN、 然後這個函式會計算 f i0 j0 (...(f iN jN s))
命名空間/模組路徑: Microsoft.FSharp.Collections.List
組件:FSharp.Core (在 FSharp.Core.dll 中)
// Signature:
List.foldBack2 : ('T1 -> 'T2 -> 'State -> 'State) -> 'T1 list -> 'T2 list -> 'State -> 'State
// Usage:
List.foldBack2 folder list1 list2 state
參數
folder
型別:'T1 -> 'T2 -> 'State -> 'State根據指定的輸入項目更新狀態的函式。
list1
Type: 'T1 list第一個輸入清單。
list2
Type: 'T2 list第二個輸入清單。
state
型別:'State初始狀態。
傳回值
最終狀態值。
例外狀況
例外狀況 |
條件 |
---|---|
會在輸入清單的長度不同時擲回。 |
備註
這個函式是名為 FoldBack2中 已編譯的組件。 如果從以.NET 語言,F # 以外,或透過反映存取函式使用這個名稱。
下列程式碼範例說明 的 List.fold2 之間差異 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
輸出
平台
Windows 7、Windows Vista SP2、Windows XP SP3、Windows XP x64 SP2、Windows Server 2008 R2、Windows Server 2008 SP2、Windows Server 2003 SP2
版本資訊
F# 執行階段
支援版本:2.0、4.0
Silverlight
支援版本:3
請參閱
參考
Microsoft.FSharp.Collections 命名空間 (F#)
變更記錄
日期 |
History |
原因 |
---|---|---|
2010 年 5 月 |
加入程式碼範例。 |
資訊加強。 |