{"id":2845,"date":"2015-02-04T19:59:56","date_gmt":"2015-02-04T19:59:56","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=2845"},"modified":"2015-03-06T15:05:26","modified_gmt":"2015-03-06T15:05:26","slug":"mutually-recursion-in-f","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/mutually-recursion-in-f\/","title":{"rendered":"Mutual recursion in F#"},"content":{"rendered":"<p>One of the annoyances of F#, well it is when you come from C# (or the likes), is that to use a function or type, the function or type needs to have been declared before it&#8217;s used. Obviously this is a problem if a type references another type which itself references the first type &#8211; but ofcourse they F# guys have a way to handle this and it&#8217;s called mutual type recursion.<\/p>\n<p>Let&#8217;s look at an example of what I&#8217;m talking about and at the solution<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\n&#x5B;&lt;Measure&gt;\r\ntype g = \r\n   static member toKilograms value : float&lt;g&gt; = value \/ 1000.0&lt;g\/kg&gt;\r\nand &#x5B;&lt;Measure&gt; kg = \r\n   static member toGrams value : float&lt;kg&gt; = value * 1000.0&lt;g\/kg&gt;\r\n<\/pre>\n<p>The above is taken from my previous post on units of measure.<\/p>\n<p>So in the above code we have type g returning a unit of measure type kg and type kg returns a unit of measure type g. Obviously g cannot use kg as it&#8217;s not declared before type g, but it can in the above example by using the <strong>and<\/strong> keyword. This can be thought of, as telling the compiler to wait until it&#8217;s read all the &#8220;chained&#8221; types in before evaluating them.<\/p>\n<p>The same technique is used for creating recursive functions which might call other functions which might call the previous function, i.e. circular function calls.<\/p>\n<p>Here&#8217;s some code taken from Microsoft&#8217;s MSDN site (referenced below) as this is a better example than any I&#8217;ve come up with to demonstrate this<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet rec Even x =\r\n   if x = 0 then true \r\n   else Odd (x - 1)\r\nand Odd x =\r\n   if x = 1 then true \r\n   else Even (x - 1)\r\n<\/pre>\n<p>So in this code we can see that the function Even is marked as <strong>rec<\/strong> so it&#8217;s recursive, it may call the function Odd which in turn may call function Even. Again we chain these functions together using the <strong>and<\/strong> keyword.<\/p>\n<p><strong>References<\/strong><\/p>\n<p><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/dd233232.aspx\" title=\"Mutually Recursive Functions\" target=\"_blank\">Mutually Recursive Functions<\/a><br \/>\n<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/dd233205.aspx\" title=\"Mutually Recursive Types\" target=\"_blank\">Mutually Recursive Types<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the annoyances of F#, well it is when you come from C# (or the likes), is that to use a function or type, the function or type needs to have been declared before it&#8217;s used. Obviously this is a problem if a type references another type which itself references the first type &#8211; [&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-2845","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\/2845","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=2845"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2845\/revisions"}],"predecessor-version":[{"id":2932,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2845\/revisions\/2932"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=2845"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=2845"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=2845"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}