{"id":844,"date":"2009-07-02T17:37:10","date_gmt":"2009-07-02T15:37:10","guid":{"rendered":"http:\/\/www.navision-blog.de\/2009\/07\/02\/f-bootcamp-questions-and-answers-part-iv-structural-comparison\/"},"modified":"2009-07-05T15:29:14","modified_gmt":"2009-07-05T13:29:14","slug":"f-bootcamp-questions-and-answers-part-iv-structural-comparison","status":"publish","type":"post","link":"http:\/\/www.navision-blog.de\/blog\/2009\/07\/02\/f-bootcamp-questions-and-answers-part-iv-structural-comparison\/","title":{"rendered":"F# BootCamp &ndash; Questions and Answers &ndash; part IV &ndash; Structural comparison"},"content":{"rendered":"<p>This is the third part in a \u201cQuestions and Answers\u201d-series about the <a href=\"http:\/\/dotnet-leipzig.de\/veranstaltungen\/net-bootcamp-2009-5\/\">F# BootCamp in Leipzig<\/a>. This time we will look at structural comparison and structural equality.<\/p>\n<h5>Question 6: Please describe the terms \u201cStructural Comparison\u201d and \u201cStructural Equality\u201d.<\/h5>\n<p>This was a simple question. Basically the answer is F# provides a default implementation for IComparable and .Equals() for all custom types. This default implementation compares all public fields of the two instances. Let\u2019s consider some samples:<\/p>\n<h5>Tuples<\/h5>\n<pre class=\"code\"><span style=\"color: blue\">let <\/span>a = 3,4,<span style=\"color: maroon\">&quot;foo&quot;\n<\/span><span style=\"color: blue\">let <\/span>b = 3,4,<span style=\"color: maroon\">&quot;bar&quot;\n\n<\/span>printfn <span style=\"color: maroon\">&quot;a &gt; b = %b&quot; <\/span>(a &gt; b) <span style=\"color: green\">\/\/ true\n<\/span>printfn <span style=\"color: maroon\">&quot;a &lt; b = %b&quot; <\/span>(a &lt; b) <span style=\"color: green\">\/\/ false<\/span><\/pre>\n<h5>Records<\/h5>\n<pre class=\"code\"><span style=\"color: blue\">type <\/span>MySimpleRecord =\n  { a: int;\n    b: int;}\n    \n<span style=\"color: blue\">type <\/span>MyCompositeRecord =\n  { x: string;\n    y: int;\n    z: MySimpleRecord}\n    \n\n<span style=\"color: blue\">let <\/span>a =   \n  { x = <span style=\"color: maroon\">&quot;Test&quot;<\/span>;\n    y = 3;\n    z = {a = 1; b = 4;}}\n    \n<span style=\"color: blue\">let <\/span>b = \n  { x = <span style=\"color: maroon\">&quot;Test&quot;<\/span>;\n    y = 3;\n    z = {a = 1; b = 2;}}\n\n<span style=\"color: green\">\/\/ Structural comparison\n<\/span>printfn <span style=\"color: maroon\">&quot;a &gt; b = %b&quot; <\/span>(a &gt; b) <span style=\"color: green\">\/\/ true\n<\/span>printfn <span style=\"color: maroon\">&quot;a &lt; b = %b&quot; <\/span>(a &lt; b) <span style=\"color: green\">\/\/ false<\/span>\n\nprintfn <span style=\"color: maroon\">&quot;Min(a,b) = %A&quot; <\/span>(min a b)\nprintfn <span style=\"color: maroon\">&quot;compare(a,b) = %d&quot; <\/span>(compare a b) <\/pre>\n<h5>Lists<\/h5>\n<pre class=\"code\"><span style=\"color: blue\">let <\/span>a = [3; 2; 4; 5]\n<span style=\"color: blue\">let <\/span>b = [3; 2; 4; 3; 3]\n\n<span style=\"color: green\">\/\/ Structural comparison\n<\/span>printfn <span style=\"color: maroon\">&quot;a &gt; b = %b&quot; <\/span>(a &gt; b) <span style=\"color: green\">\/\/ true\n<\/span>printfn <span style=\"color: maroon\">&quot;a &lt; b = %b&quot; <\/span>(a &lt; b) <span style=\"color: green\">\/\/ false\n\n<\/span>printfn <span style=\"color: maroon\">&quot;Min(a,b) = %A&quot; <\/span>(min a b)\nprintfn <span style=\"color: maroon\">&quot;compare(a,b) = %d&quot; <\/span>(compare a b)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is the third part in a \u201cQuestions and Answers\u201d-series about the F# BootCamp in Leipzig. This time we will look at structural comparison and structural equality. Question 6: Please describe the terms \u201cStructural Comparison\u201d and \u201cStructural Equality\u201d. This was a simple question. Basically the answer is F# provides a default implementation for IComparable and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[23,448],"tags":[312,664,555],"_links":{"self":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/844"}],"collection":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/comments?post=844"}],"version-history":[{"count":4,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/844\/revisions"}],"predecessor-version":[{"id":848,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/844\/revisions\/848"}],"wp:attachment":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/media?parent=844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/categories?post=844"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/tags?post=844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}