F# - Compare two lists, Compare two Sequences, Compare two Sets

Post date: Jan 28, 2012 10:47:19 AM

Can you compare lists, sequences and sets to know if they are equal?

Lists and Sequences define equal as if their elements are the same in the same order.

Sets define equal as if they contain the same elements. Duplicate elements cannot exist in Sets.

let a = [1; 2; 3] let b = [2; 1; 3] let c = [1; 2; 3] printfn "List [1; 2; 3] = [2; 1; 3]: %b" (a = b) printfn "List [1; 2; 3] = [1; 2; 3]: %b" (a = c) printfn "" let s1 = Seq.ofList a let s2 = Seq.ofList b let s3 = Seq.ofList c printfn "Seq [1; 2; 3] = [2; 1; 3]: %b" (s1 = s2) printfn "Seq [1; 2; 3] = [1; 2; 3]: %b" (s1 = s3) printfn "" let set1 = Set.ofList a let set2 = Set.ofList b let set3 = Set.ofList c printfn "Set [1; 2; 3] = [2; 1; 3]: %b" (set1 = set2) printfn "Set [1; 2; 3] = [1; 2; 3]: %b" (set1 = set3)

Results:

List [1; 2; 3] = [2; 1; 3]: false List [1; 2; 3] = [1; 2; 3]: true Seq [1; 2; 3] = [2; 1; 3]: false Seq [1; 2; 3] = [1; 2; 3]: true Set [1; 2; 3] = [2; 1; 3]: true Set [1; 2; 3] = [1; 2; 3]: true