Share via


Funzione List.sumBy<'T,^U> (F#)

Restituisce la somma dei risultati generati applicando la funzione a ogni elemento dell'elenco.

Percorso di spazio dei nomi/modulo: Microsoft.FSharp.Collections.List

Assembly: FSharp.Core (in FSharp.Core.dll)

// Signature:
List.sumBy : ('T -> ^U) -> 'T list -> ^U (requires ^U with static member (+) and ^U with static member Zero)

// Usage:
List.sumBy projection list

Parametri

  • projection
    Tipo: 'T -> ^U

    Funzione che trasforma gli elementi dell'elenco nel tipo da sommare.

  • list
    Tipo: 'T list

    Elenco di input.

Valore restituito

Somma risultante.

Note

Questa funzione è denominata SumBy negli assembly compilati. Utilizzare questo nome se si accede alla funzione da un linguaggio .NET diverso da F# o tramite reflection.

Esempio

Nell'esempio di codice riportato di seguito viene illustrato l'utilizzo di List.sum, List.sumBy e List.average.

// Compute the sum of the first 10 integers by using List.sum.
let sum1 = List.sum [1 .. 10]

// Compute the sum of the squares of the elements of a list by using List.sumBy.
let sum2 = List.sumBy (fun elem -> elem*elem) [1 .. 10]

// Compute the average of the elements of a list by using List.average.
let avg1 = List.average [0.0; 1.0; 1.0; 2.0]

printfn "%f" avg1

Output

  

Piattaforme

Windows 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2.

Informazioni sulla versione

F# Runtime

Supportato in: 2.0, 4.0

Silverlight

Supportato in: 3

Vedere anche

Riferimenti

Modulo Collections.List (F#)

Spazio dei nomi Microsoft.FSharp.Collections (F#)

Cronologia delle modifiche

Data

Cronologia

Motivo

Maggio 2010

Aggiunto esempio di codice.

Miglioramento delle informazioni.