{"id":1077,"date":"2011-08-15T18:41:16","date_gmt":"2011-08-15T18:41:16","guid":{"rendered":"http:\/\/www.navision-blog.de\/2011\/08\/15\/some-special-monads-in-f-part-3-of-n-distributionmonad\/"},"modified":"2011-08-17T07:20:45","modified_gmt":"2011-08-17T07:20:45","slug":"some-special-monads-in-f-part-3-of-n-distributionmonad","status":"publish","type":"post","link":"http:\/\/www.navision-blog.de\/blog\/2011\/08\/15\/some-special-monads-in-f-part-3-of-n-distributionmonad\/","title":{"rendered":"Some special monads in F# &#8211; Part 3 of n &#8211; DistributionMonad"},"content":{"rendered":"<p>In <a href=\"http:\/\/www.navision-blog.de\/2011\/08\/15\/some-special-monads-in-f-part-1-of-n-infinitymonad\/\">part I of this blog series<\/a> I showed a simple InfinityMonad, which allows to treat special calculations as infinity and in the <a href=\"http:\/\/www.navision-blog.de\/2011\/08\/15\/some-special-monads-in-f-part-2-of-n-undomonad\/\">second part<\/a> I showed the UndoMonad, which defines an environment which allows to undo and redo state changes.<\/p>\n<p>This and the following posts are based on a famous paper by Martin Erwig and Steve Kollmansberger called <a href=\"http:\/\/web.engr.oregonstate.edu\/~erwig\/papers\/PFP_JFP06.pdf\">&quot;Functional Pearls: Probabilistic functional programming in Haskell&quot;<\/a>.<\/p>\n<p>Let\u2019s start by looking at a small scenario:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1147341.js\"> <\/script>  <\/p>\n<p>This simple query calculates the probability of the event, that an dice roll gives a value greater than 3 and an independent coin flip gives \u201cHeads\u201d. In order to do this the DistributionMonad enumerates all possibilities and calculates the joint probability using the following formula:<\/p>\n<p><a href=\"http:\/\/en.wikipedia.org\/wiki\/Conditional_probability\"><img loading=\"lazy\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"Probability\" border=\"0\" alt=\"Probability\" src=\"http:\/\/www.navision-blog.de\/images\/69a303dfdc03_11683\/Probability.png\" width=\"226\" height=\"21\" \/><\/a><\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1147375.js\"> <\/script>  <\/p>\n<p>If we want the nice syntactic sugar we can easily define a computation expression builder:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1147381.js\"> <\/script>  <\/p>\n<p>In order to allow easier access to our monad, we define some helper functions and basic distributions for fair coins and dices:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1147385.js\"> <\/script>In the <a href=\"http:\/\/www.navision-blog.de\/2011\/08\/16\/some-special-monads-in-f-part-4-of-n-application-the-monty-hall-problem\/\">next part of this blog series<\/a> I will show how we can utilize the DistributionMonad in order to solve the famous <a href=\"http:\/\/en.wikipedia.org\/wiki\/Monty_Hall_problem\">Monty Hall problem<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In part I of this blog series I showed a simple InfinityMonad, which allows to treat special calculations as infinity and in the second part I showed the UndoMonad, which defines an environment which allows to undo and redo state changes. This and the following posts are based on a famous paper by Martin Erwig [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[448,8],"tags":[664,582],"_links":{"self":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1077"}],"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=1077"}],"version-history":[{"count":5,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1077\/revisions"}],"predecessor-version":[{"id":1088,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1077\/revisions\/1088"}],"wp:attachment":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/media?parent=1077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/categories?post=1077"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/tags?post=1077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}