Export (0) Print
Expand All
Expand Minimize

Seq.pairwise<'T> Function (F#)

Returns a sequence of each element in the input sequence and its predecessor, with the exception of the first element which is only returned as the predecessor of the second element.

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

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

// Signature:
Seq.pairwise : seq<'T> -> seq<'T * 'T>

// Usage:
Seq.pairwise source

source

Type: seq<'T>

The input sequence.

Exception

Condition

ArgumentNullException

Thrown when the input sequence is null.

The result sequence.

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

The following example demonstrates the use of Seq.pairwise. The initial sequence is a sequence of squares up to 100. The Seq.pairwise function generates a sequence of tuples of consecutive squares, { (1, 4), (4, 9), (9, 16) ... }. The second part of the example produces a list of the differences in each pair of squares.


let printSeq seq1 = Seq.iter (printf "%A ") seq1; printfn ""
let seqPairwise = Seq.pairwise (seq { for i in 1 .. 10 -> i*i })
printSeq seqPairwise

printfn ""
let seqDelta = Seq.map (fun elem -> snd elem - fst elem) seqPairwise
printSeq seqDelta


(1, 4) (4, 9) (9, 16) (16, 25) (25, 36) (36, 49) (49, 64) (64, 81) (81, 100) 

3 5 7 9 11 13 15 17 19

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# Runtime

Supported in: 2.0, 4.0

Silverlight

Supported in: 3

Community Additions

ADD
Show:
© 2014 Microsoft