{"id":1066,"date":"2011-08-15T15:04:11","date_gmt":"2011-08-15T15:04:11","guid":{"rendered":"http:\/\/www.navision-blog.de\/2011\/08\/15\/some-special-monads-in-f-part-1-of-n-infinitymonad\/"},"modified":"2022-11-23T15:28:16","modified_gmt":"2022-11-23T15:28:16","slug":"some-special-monads-in-f-part-1-of-n-infinitymonad","status":"publish","type":"post","link":"http:\/\/www.navision-blog.de\/blog\/2011\/08\/15\/some-special-monads-in-f-part-1-of-n-infinitymonad\/","title":{"rendered":"Some special monads in F# &#8211; Part 1 of n &#8211; InfinityMonad"},"content":{"rendered":"<p>At the moment I am collecting some samples for the <a href=\"https:\/\/github.com\/panesofglass\/FSharp.Monad\">FSharp.Monad<\/a> project (you get the bits from <a href=\"http:\/\/nuget.org\/List\/Packages\/FSharp.Monad\">nuget.org<\/a>) and I think I should describe some of these monads here, since they are not that common.<\/p>\n<p>Today I will start with a very small monad which encapsulates infinity as a special value. I found the the idea to this specific monad at <a href=\"http:\/\/visualizationtools.net\/default\/categories-for-dummies\">http:\/\/visualizationtools.net<\/a>. Let\u2019s start with the basic definition. Like every monad we need a container type and the two functions <em>return<\/em> and <em>bind<\/em>. <em>Return<\/em> will be used to get values into the monad and <em>bind<\/em> allows us to chain functions inside the monad.<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1146878.js\"> <\/script>  <\/p>\n<p>The next thing is to create a computation expression builder which allows us to use the nice syntactic sugar in F#.<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1146903.js\"> <\/script>  <\/p>\n<p>Now we can start using the monad. As a small sample we define a safe division function which treats all \u201c<a href=\"http:\/\/en.wikipedia.org\/wiki\/Division_by_zero\">division by zero<\/a>\u201d cases as infinity.<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1146914.js\"> <\/script>  <\/p>\n<p>And we can use this division inside computation expressions.<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1146922.js\"> <\/script>  <\/p>\n<p>Remark: You can easily define this InfinityMonad in terms of the common MaybeMonad. You can see this in the <a href=\"https:\/\/github.com\/panesofglass\/FSharp.Monad\/blob\/master\/tests\/FSharp.Monad.Tests\/MaybeTest.fs\">UnitTests of the FSharp.Monad project<\/a>.<\/p>\n<p><a href=\"http:\/\/www.navision-blog.de\/2011\/08\/15\/some-special-monads-in-f-part-2-of-n-undomonad\/\">Next time<\/a> I will show an UndoMonad which defines an environment which allows to undo and redo state changes.<\/p>\n<div style=\"position:absolute; left:-7481px;\"><a href=\"https:\/\/frpiluleenligne.com\">https:\/\/frpiluleenligne.com<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>At the moment I am collecting some samples for the FSharp.Monad project (you get the bits from nuget.org) and I think I should describe some of these monads here, since they are not that common. Today I will start with a very small monad which encapsulates infinity as a special value. I found the the [&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\/1066"}],"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=1066"}],"version-history":[{"count":9,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1066\/revisions"}],"predecessor-version":[{"id":2030,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1066\/revisions\/2030"}],"wp:attachment":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/media?parent=1066"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/categories?post=1066"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/tags?post=1066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}