共用方式為


Array.fold2<'T1,'T2,'State> 函式 (F#)

更新:2010 年 8 月

將函式套用至從兩個集合繪製的項目組 (從左到右),並透過計算來建立累計值引數的執行緒。 兩個輸入的陣列必須有相同的長度,否則 ArgumentException引發

命名空間/模組路徑: Microsoft.FSharp.Collections.Array

組件:FSharp.Core (在 FSharp.Core.dll 中)

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

// Usage:
Array.fold2 folder state array1 array2

參數

  • folder
    型別:'State -> 'T1 -> 'T2 -> 'State

    根據指定的輸入項目更新狀態的函式。

  • state
    型別:'State

    初始狀態。

  • array1
    Type: 'T1 []

    第一個輸入陣列。

  • array2
    Type: 'T2 []

    第二個輸入陣列。

例外狀況

例外狀況

條件

ArgumentException

當輸入陣列的長度不同時擲回。

傳回值

最終狀態。

備註

這個函式是名為 Fold2中 已編譯的組件。 如果從一個語言,F # 以外,或透過反映存取函式使用這個名稱。

範例

下列程式碼顯示如何使用 Array.fold2

// Use Array.fold2 to perform computations over two arrays (of equal size)
// at the same time.
// Example: Add the greater element at each array position.
let sumGreatest array1 array2 =
    Array.fold2 (fun acc elem1 elem2 ->
        acc + max elem1 elem2) 0 array1 array2

let sum = sumGreatest [| 1; 2; 3 |] [| 3; 2; 1 |]
printfn "The sum of the greater of each pair of elements in the two arrays is %d." sum

輸出

  

平台

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

請參閱

參考

Collections.Array 模組 (F#)

Microsoft.FSharp.Collections 命名空間 (F#)

變更記錄

日期

History

原因

2010 年 8 月

加入程式碼範例。

資訊加強。