Using Structural Equality

Category:
Equality, Comparison and Hashing
Description: F# supports structural comparison and equality of values with the same type. This sample shows the results of some simple structural comparison operations.
Code:
let ComparisonSample1() =
let show a b =
printfn "%A < %A: %b" a b (a < b)
printfn "%A = %A: %b" a b (a = b)
printfn "%A > %A: %b" a b (a > b)

show 1 2;
show 2 2;
show "1" "2"
show "abb" "abc"
show "aBc" "ABB" // case-sensitive
show None (Some 1);
show None None;
show (Some 0) (Some 1);
show (Some 1) (Some 1);
show [1;2;3] [1;2;2];
show [] [1;2;2]


Execution Result:
1 < 2: true
1 = 2: false
1 > 2: false
2 < 2: false
2 = 2: true
2 > 2: false
"1" < "2": true
"1" = "2": false
"1" > "2": false
"abb" < "abc": true
"abb" = "abc": false
"abb" > "abc": false
"aBc" < "ABB": false
"aBc" = "ABB": false
"aBc" > "ABB": true
< Some 1: true
= Some 1: false
> Some 1: false
< : false
= : true
> : false
Some 0 < Some 1: true
Some 0 = Some 1: false
Some 0 > Some 1: false
Some 1 < Some 1: false
Some 1 = Some 1: true
Some 1 > Some 1: false
[1; 2; 3] < [1; 2; 2]: false
[1; 2; 3] = [1; 2; 2]: false
[1; 2; 3] > [1; 2; 2]: true
[] < [1; 2; 2]: true
[] = [1; 2; 2]: false
[] > [1; 2; 2]: false

Using Structural Hashing

Category:
Equality, Comparison and Hashing
Description: F# supports structural hashing on values. Typically only F# record/union structured terms are traversed, though this can be customized on a per-type basis. This sample shows the results of some simple structural hashing operations.
Code:
let HashingSample2() =
let show a = printfn "hash(%A) : %d" a (hash a)
show 1;
show 2;
show "1"
show "2"
show "abb"
show "aBc" // case-sensitive
show None;
show (Some 1);
show (Some 0);
show [1;2;3];
show [1;2;3;4;5;6;7;8];
show [1;2;3;4;5;6;7;8;9;10;11];
show [1;2;3;4;5;6;7;8;9;10;11;12;13;14;15]

//--------------------------------------------------------

Execution Result:
hash(1) : 1
hash(2) : 2
hash("1") : -842352753
hash("2") : -842352754
hash("abb") : 2103075711
hash("aBc") : 539088922
hash() : 0
hash(Some 1) : -1640531462
hash(Some 0) : -1640531463
hash([1; 2; 3]) : 1956583134
hash([1; 2; 3; 4; 5; 6; 7; 8]) : 922428386
hash([1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11]) : 1771492728
hash([1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15]) : -926589492

Last edited Sep 12, 2011 at 11:19 PM by ttliu2000, version 1

Comments

No comments yet.