共用方式為


Seq.compareWith<'T> 函式 (F#)

使用指定的比較函式,逐一比較兩個序列的每個項目。

命名空間/模組路徑:Microsoft.FSharp.Collections.Seq

組件:FSharp.Core (在 FSharp.Core.dll 中)

// Signature:
Seq.compareWith : ('T -> 'T -> int) -> seq<'T> -> seq<'T> -> int

// Usage:
Seq.compareWith comparer source1 source2

參數

  • comparer
    型別:'T -> 'T -> int

    函式,從每個序列各接受一個項目並傳回 int。 如果評估結果為非零值,則會停止反覆項目並傳回該值。

  • source1
    型別:seq<'T>

    第一個輸入序列。

  • source2
    型別:seq<'T>

    第二個輸入序列。

例外狀況

例外狀況

條件

ArgumentNullException

當其中一個輸入序列為 null 時擲回。

傳回值

從比較函式傳回第一個非零結果。 到達序列結尾時,如果第一個序列較短則傳回 -1,如果第二個序列較短則傳回 1。

備註

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

範例

下列範例示範使用 Seq.compareWith來比較兩個序列使用 自訂比較函式。

let sequence1 = seq { 1 .. 10 }
let sequence2 = seq { 10 .. -1 .. 1 }

// Compare two sequences element by element.
let compareSequences = Seq.compareWith (fun elem1 elem2 ->
    if elem1 > elem2 then 1
    elif elem1 < elem2 then -1
    else 0) 

let compareResult1 = compareSequences sequence1 sequence2
match compareResult1 with
| 1 -> printfn "Sequence1 is greater than sequence2."
| -1 -> printfn "Sequence1 is less than sequence2."
| 0 -> printfn "Sequence1 is equal to sequence2."
| _ -> failwith("Invalid comparison result.")
  

平台

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# 執行階段

支援版本:2.0、4.0

Silverlight

支援版本:3

請參閱

參考

Collections.Seq 模組 (F#)

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