{"id":1010,"date":"2014-01-02T15:16:55","date_gmt":"2014-01-02T15:16:55","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=1010"},"modified":"2014-01-02T15:16:55","modified_gmt":"2014-01-02T15:16:55","slug":"pattern-matching-in-f","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/pattern-matching-in-f\/","title":{"rendered":"Pattern matching in F#"},"content":{"rendered":"<p>Pattern matching allows us to use a more powerful form of if..then..else or switch\/select case type statements.<\/p>\n<p>We declare a pattern matching implementation as follows<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet option o =\r\n   match o with\r\n   | 1 -&gt; &quot;item 1&quot;\r\n   | 2 -&gt; &quot;item 2&quot;\r\n   | _ -&gt; &quot;unkown&quot;\r\n\r\n\/\/ usage\r\nlet choice = option 2\r\n<\/pre>\n<p>The _ is used as a wildcard character, i.e. anything not matching the patterns above..<\/p>\n<p>We can also add matching along the lines of<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet option o =\r\n   match o with\r\n   | o when o &lt;= 0 -&gt; failwith &quot;value must be greater than 0&quot;\r\n   \/\/ other patterns\r\n<\/pre>\n<p>You can also match to more than one pattern as per<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet option o =\r\n   match o with\r\n   | 1 | 2 -&gt; &quot;one or two&quot;\r\n   \/\/ other patterns\r\n<\/pre>\n<p>We can also pattern match on tuples for example<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet options tuple =\r\nmatch tuple with\r\n   | &quot;one&quot;, 1 -&gt; &quot;got one&quot;\r\n   | &quot;two&quot;, 2 -&gt; &quot;got two&quot;\r\n   | _, _ -&gt; &quot;unknown&quot;\r\n\r\n\/\/ usage\r\nlet k = options (&quot;one&quot;, 1)\r\nprintfn &quot;%s&quot; &lt;| k <\/pre>\n<p>Better still we can ignore part of a tuple<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\"> let options tuple =  match tuple with    | &quot;one&quot;, _ -&gt; &quot;got one&quot;\r\n   | _, 2 -&gt; &quot;got two&quot;\r\n   | _, _ -&gt; &quot;unknown&quot;\r\n\r\n\/\/ usage\r\nlet k = options (&quot;?&quot;, 2)\r\nprintfn &quot;%s&quot; &lt;| k <\/pre>\n<p>or we could use the following to denote tuples<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\"> let options (a, b) =     match (a, b) with    | &quot;one&quot;, _ -&gt; &quot;got one&quot;\r\n   | _, 2 -&gt; &quot;got two&quot;\r\n   | _, _ -&gt; &quot;unknown&quot;\r\n\r\nlet k = options (&quot;?&quot;, 2)\r\nprintfn &quot;%s&quot; &lt;| k\r\n<\/pre>\n<p><strong>Type pattern matching<\/strong><\/p>\n<p>We can also use the pattern matching to match against the type of the value passed to it, for example<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\n&lt;!&#x5B;CDATA&#x5B;\r\nlet value = 3.12f\r\n\r\nlet y =\r\n   match box value with\r\n   | :? System.Int32 -&gt; &quot;integer&quot;\r\n   | :? System.Single -&gt; &quot;float&quot;\r\n   | :? System.String -&gt; &quot;string&quot;\r\n   | _ -&gt; &quot;undefined&quot;\r\n\r\nprintfn &quot;%s&quot; &lt;| y\r\n]]&gt;\r\n<\/pre>\n<p>In the above we need to use <strong>box<\/strong>, alternatively we can use the following<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet y (o: obj) =\r\n   match o with\r\n   | :? System.Int32 -&gt; &quot;integer&quot;\r\n   | :? System.Single -&gt; &quot;float&quot;\r\n   | :? System.String -&gt; &quot;string&quot;\r\n   | _ -&gt; &quot;undefined&quot;\r\n\r\nprintfn &quot;%s&quot; &lt;| string y\r\n<\/pre>\n<p><em>Note: Here, instead of creating a let statement to get the result from the match I&#8217;m supplying it directly to the printfn function. Interestingly whilst the return is a string and the printfn I set to output a string, at this point it&#8217;s a generic string and we need to cast it directly to a string (non-generic)<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pattern matching allows us to use a more powerful form of if..then..else or switch\/select case type statements. We declare a pattern matching implementation as follows let option o = match o with | 1 -&gt; &quot;item 1&quot; | 2 -&gt; &quot;item 2&quot; | _ -&gt; &quot;unkown&quot; \/\/ usage let choice = option 2 The _ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-1010","post","type-post","status-publish","format-standard","hentry","category-f"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=1010"}],"version-history":[{"count":19,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1010\/revisions"}],"predecessor-version":[{"id":1086,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1010\/revisions\/1086"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=1010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=1010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=1010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}