共用方式為


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

將函式套用至集合的每一個項目,以透過計算建立累計值引數的執行緒。 這個函式會接受第二個引數,並且將函式套用至這個引數和清單的第一個項目。 然後將這個結果與第二個項目一併傳遞到函式中,依此類推。 最後它會傳回中間結果與最終結果的清單。

**命名空間/模組路徑:**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
    型別:'T list

    輸入清單。

傳回值

狀態的清單。

備註

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

範例

下列程式碼示範 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]

Output

  

平台

Windows 8 中, Windows 7, Windows Server 2012 上, Windows Server 2008 R2

版本資訊

F# 核心程式庫版本

支援版本:2.0, 4.0,可攜式執行檔 (PE)。

請參閱

參考

Collections.List 模組 (F#)

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