共用方式為


List.scan<'T,'State> 函式 (F#)

更新:2010 年 8 月

將函式套用至集合的每一個項目,以透過計算建立累計值引數的執行緒。 這個函式會使用第二個] 引數和函式套用於它或清單的第一個項目。 然後,將這個結果傳遞至函式與第二個項目,以及等等。 最後,它傳回中繼結果,最後的結果的清單。

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

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

// Signature:
List.scan : ('State -> 'T -> 'State) -> 'State -> 'T list -> 'State list

// Usage:
List.scan folder state list

參數

  • folder
    型別:'State -> 'T -> 'State

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

  • state
    型別:'State

    初始狀態。

  • list
    Type: 'T list

    輸入清單。

傳回值

狀態的清單。

備註

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

範例

下列程式碼顯示如何使用 List.scan

let initialBalance = 1122.73
let transactions = [ -100.00; +450.34; -62.34; -127.00; -13.50; -12.92 ]
let balances =
    List.scan (fun balance transactionAmount -> balance + transactionAmount)
              initialBalance transactions
printfn "Initial balance:\n $%10.2f" initialBalance
printfn "Transaction   Balance"
for i in 0 .. List.length transactions - 1 do
    printfn "$%10.2f $%10.2f" transactions.[i] balances.[i]
printfn "Final balance:\n $%10.2f" balances.[ List.length balances - 1]

輸出

  

平台

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.List 模組 (F#)

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

變更記錄

日期

History

原因

2010 年 8 月

加入程式碼範例。

資訊加強。