Funzione List.sum<^T> (F#)

Restituisce la somma degli elementi nell'elenco.

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

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

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

// Usage:
List.sum list

Parametri

  • list
    Tipo: ^Tlist

    Elenco di input.

Valore restituito

Somma risultante.

Note

Questa funzione è denominata Sum negli assembly compilati.Utilizzare questo nome se si accede alla funzione da un linguaggio 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 8, Windows 7, Windows Server 2012, Windows Server 2008 R2

Informazioni sulla versione

Versioni della libreria di base F#

Supportato in: 2,0, 4,0, portabile

Vedere anche

Riferimenti

Modulo Collections.List (F#)

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