Share via


Seq.average<^T> Function (F#)

Returns the average of the elements in the sequence.

Namespace/Module Path: Microsoft.FSharp.Collections.Seq

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

// Signature:
Seq.average : seq<^T> -> ^T (requires ^T with static member (+) and ^T with static member DivideByInt and ^T with static member Zero)

// Usage:
Seq.average source

Parameters

  • source
    Type: seq<^T>

    The input sequence.

Exceptions

Exception

Condition

ArgumentException

Thrown when the input sequence has zero elements.

ArgumentNullException

Thrown when the input sequence is null.

Return Value

The average.

Remarks

The elements are averaged using the + operator, DivideByInt method and Zero property associated with the element type.

This function cannot be used directly on a sequence of integers since it requires that the type support an exact division operation, which is indicated by the constraint that the element type must support DivideByInt.

This function is named Average in compiled assemblies. If you are accessing the function from a language other than F#, or through reflection, use this name.

Example

The following code example shows how to use Seq.average, and also compares Seq.average with Seq.averageBy.

// You can use Seq.average to average elements of a list, array, or sequence. 
let average1 = Seq.average [ 1.0 .. 10.0 ]
printfn "Average: %f" average1
// To average a sequence of integers, use Seq.averageBy to convert to float. 
let average2 = Seq.averageBy (fun elem -> float elem) (seq { 1 .. 10 })
printfn "Average: %f" average2

Output

Average: 5.500000
Average: 5.500000

Platforms

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

Version Information

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

See Also

Reference

Collections.Seq Module (F#)

Microsoft.FSharp.Collections Namespace (F#)