<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Dorian Corompt's Blog</title><link>http://blogs.msdn.com/b/doriancorompt/</link><description /><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>Bringing the querying power of SQL to JavaScript</title><link>http://blogs.msdn.com/b/doriancorompt/archive/2013/01/21/bringing-the-querying-power-of-sql-to-javascript.aspx</link><pubDate>Mon, 21 Jan 2013 18:02:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10386899</guid><dc:creator>DorianCorompt</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/doriancorompt/rsscomments.aspx?WeblogPostID=10386899</wfw:commentRss><comments>http://blogs.msdn.com/b/doriancorompt/archive/2013/01/21/bringing-the-querying-power-of-sql-to-javascript.aspx#comments</comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; Hello and Happy New Year to everyone, I have been really busy recently and it has been long time I didn't write, however do not worry I didn’t forget I have to finish my functional programming series of articles.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;This post comes directly from a session I presented internally about JavaScript advanced programming concepts. I thought it would be a good idea to share some of its content on this blog. This session was mainly to show how to take advantage of the prototype and functional paradigms that JavaScript supports. &lt;/p&gt;  &lt;p&gt;If you read my previous articles about F#, you probably understood how useful functional programming concepts are, when it is about handling sets of data. JavaScript does not have the linked list based on pairs representation that most functional programming have, however it has arrays. Let see how we can easily increase the power of JavaScript by extending its Array type with an SQL like query mechanism based on the most functional programming common functions.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;First a word about JavaScript arrays…&lt;/h2&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;JavaScript arrays are associative arrays, also called map or dictionaries in other languages. It means they associate a key to a value. Because JavaScript is dynamically typed you can use arrays with mixed keys, or values types.&lt;/p&gt;  &lt;p&gt;As you probably know, almost everything in JavaScript is object. However what you perhaps ignore is that all objects are arrays, I don't mean they are based on the &lt;em&gt;Array&lt;/em&gt; JavaScript type nor that they share its prototype, but they behave as arrays. Indeed objects slots may be represented as an associative array of properties, since function are closure in JavaScript (aka. first class values) they can be used as values for properties thus representing the concept of methods. So improving the way we use array is not only good to work on data, but also good to work on the code, for meta-programming.&lt;/p&gt;  &lt;p&gt;The code below shows that objects and surprisingly even functions (which are objects) can be used as arrays:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7343.image_5F00_260214ED.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/8836.image_5F00_thumb_5F00_2BDCB886.png" width="273" height="301" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;JavaScript function doesn’t really have a fixed parameter prototype, thus function arguments number may vary, so it is important to know they can be obtained through the &lt;em&gt;arguments&lt;/em&gt; special object variable (not of type &lt;em&gt;Array&lt;/em&gt;):&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/3531.image_5F00_11301C6D.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/4111.image_5F00_thumb_5F00_7B66340F.png" width="546" height="141" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;You can see in the example above that it is possible to use &lt;em&gt;Array&lt;/em&gt; methods through the &lt;em&gt;Array&lt;/em&gt; prototype on non &lt;em&gt;Array&lt;/em&gt; type objects because they behave the same.&lt;/p&gt;  &lt;p&gt;About the &lt;em&gt;curry&lt;/em&gt; function:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Wikipedia:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;In mathematics and computer science, currying is the technique of transforming a function that takes multiple arguments (or a tuple of arguments) in such a way that it can be called as a chain of functions, each with a single argument (partial application). It was originated by Moses Schönfinkel and later re-discovered by Haskell Curry. Because of this, some say it would be more correct to name it schönfinkeling.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;So from a practical point of view&lt;em&gt; curry&lt;/em&gt; helps us to create partially applied functions on the fly without typing a lot of code.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Fold, map, filter the essential tools&lt;/h2&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;In this section I assume you know what the notion of catamorphism is and what are the fold, map and filter functions, if you don’t please read my previous article &lt;a href="http://blogs.msdn.com/b/doriancorompt/archive/2012/02/05/4-1st-class-functions-power-is-in-simplicity.aspx"&gt;1st Class Functions – Power is in simplicity&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Fold can be defined recursively, but since JavaScript works with array and for performance reasons as it should be considered as a collection processing function building block, I prefer to implement it with a loop. It is interesting to highlight that optimizing terminal recursions is not a part of the JavaScript standard but that the most used script engines optimize it.&lt;/p&gt;  &lt;p&gt;Here an implementation of &lt;em&gt;fold left&lt;/em&gt; in JavaScript:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/8311.image_5F00_4EDD0429.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/5287.image_5F00_thumb_5F00_071B7B42.png" width="282" height="108" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Once we have fold, we can define &lt;em&gt;map&lt;/em&gt; and &lt;em&gt;filter&lt;/em&gt; in terms of it:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0882.image_5F00_0CF61EDB.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0407.image_5F00_thumb_5F00_772C367D.png" width="867" height="58" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;We can now do funny things with arrays of data:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1070.image_5F00_55605DEC.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/2134.image_5F00_thumb_5F00_702C83F8.png" width="550" height="188" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Give me some power&lt;/h2&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Now that we have the &lt;em&gt;map&lt;/em&gt; and &lt;em&gt;filter&lt;/em&gt; operations, let’s augment and empower the JavaScript &lt;em&gt;Array &lt;/em&gt;prototype.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1057.image_5F00_728605F6.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/4606.image_5F00_thumb_5F00_58B1CFC7.png" width="490" height="39" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;We can now use the &lt;em&gt;select &lt;/em&gt;and &lt;em&gt;where&lt;/em&gt; methods on any new &lt;em&gt;Array&lt;/em&gt; type instances.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/8475.image_5F00_6492888B.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7080.image_5F00_thumb_5F00_752A8379.png" width="821" height="336" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Look how easily we can query data!&lt;/p&gt;  &lt;p&gt;As shown you can use the built in&lt;em&gt; sort&lt;/em&gt; operation, and if you need a&lt;em&gt; join&lt;/em&gt; don’t forget the &lt;em&gt;concat&lt;/em&gt; method. Since from the past few&amp;#160; years applications started to become more and more client based, those short functional helpers defined by less than 10 lines of code appeared to me to be quite useful when processing data incoming from web services. As a comics guys would say “With great power comes great responsibility”. I hope you will have great use of it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10386899" width="1" height="1"&gt;</description></item><item><title>8. Quotations – Say “your name”</title><link>http://blogs.msdn.com/b/doriancorompt/archive/2012/09/03/8-quotations-say-your-name.aspx</link><pubDate>Mon, 03 Sep 2012 13:53:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10345928</guid><dc:creator>DorianCorompt</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/doriancorompt/rsscomments.aspx?WeblogPostID=10345928</wfw:commentRss><comments>http://blogs.msdn.com/b/doriancorompt/archive/2012/09/03/8-quotations-say-your-name.aspx#comments</comments><description>&lt;p&gt;[DRAFT]&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Say your name ! ; Say “your name”! Do you understand the difference between those two sentences?&lt;/p&gt;  &lt;p&gt;The first uses the meaning of the words “your name”, while the other refers the words themselves. How do you know that? They are quoted. &lt;/p&gt;  &lt;p&gt;Sometimes you would like to be able to do the same thing with programming languages. Being able to manipulate the code from the code help us make programs who generate programs, and as crazy at it may sound, it is really powerful. If you are a C# developer you probably know about .NET reflection, F# allows you to go a little further, by manipulating code expressions in a concise safe and statically typed way.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;What can you do with F# quotations ?&lt;/h2&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Most programmers use language constructs such as functions as a mean of abstraction, however most of them don’t use the power of language abstraction. Quotations are a really interesting to create interpreters and compilers using F# code. But why creating a new F# compiler or interpreter?&amp;#160; &lt;/p&gt;  &lt;p&gt;Look at the web! How many different languages do you need nowadays to code a simple web site such as a blog? Probably HTML, CSS, JavaScript on the client side, C#, Java or PHP and SQL on the server side, so at least five different languages!&lt;/p&gt;  &lt;p&gt;Some people think programming problems have their own specific solution better expressed in a specific language, I think it is totally true, so the use of so many languages to create a web site may be justified.&lt;/p&gt;  &lt;p&gt;But what if a language is expressive enough to allow to write more specific language within it syntax? I mean a host language embedding an other. This may sound weird but it is actually something which appears really common to functional programmers, especially LISP programmers. Indeed LISP syntax and core primitives are by design really limited, but powerful enough to create all the others. So big LISP programs consist usually in creating and using different embedded languages specifics to some part of the application, for example a language for the UI, one allowing Object Oriented Programming, one SQL like to query data, and one declarative data language to store data in files. Those specifics embedded languages are called Domain Specific Languages (DSL). &lt;/p&gt;  &lt;p&gt;By the way minimalistic doesn’t mean powerless, look the matter in this world is composed of no more than a hundred different types of atoms, and actually all the diversity of living creatures have their DNA encoded with four basic building block molecules known as ATGC. In computer sciences, what about the dilemma RISC vs. CISC? Didn’t we “recently” realize that simple RISC architecture such as ARM, easier to build and minimize, thus consuming less energy have a lot of advantages over complexes ones?&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;A taste of quotations…&lt;/h2&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;It is really easy to define a quotation in F#, you just have to surround code with &lt;em&gt;&amp;lt;@&lt;/em&gt; and &lt;em&gt;@&amp;gt; &lt;/em&gt;for typed quotations and &lt;em&gt;&amp;lt;@@&lt;/em&gt; and &lt;em&gt;@@&amp;gt;&lt;/em&gt; for untyped quotations. Look at the example below:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/4530.image_5F00_6209B365.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0726.image_5F00_thumb_5F00_7C4A1C89.png" width="703" height="101" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The &lt;em&gt;% &lt;/em&gt;splicing&lt;em&gt; &lt;/em&gt;operator in front of a variable allows the integration of a quotation expression into an other, there is also an untyped &lt;em&gt;%%&lt;/em&gt; splicing operator. The evaluation of this code, shows:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1200.image_5F00_0B60FB64.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7585.image_5F00_thumb_5F00_57990512.png" width="634" height="128" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Both&lt;em&gt; favoritePlace&lt;/em&gt; and&lt;em&gt; code&lt;/em&gt; are bindings whose value are expressions, represented by the generic type Quotations.Expr&amp;lt;’a&amp;gt;. You clearly see that the Abstract Syntax Tree (AST) is directly accessible. Refers to the Quotations.Expr&amp;lt;’a&amp;gt; for more information about the expression type constructors available (&lt;a title="http://msdn.microsoft.com/en-us/library/ee370577.aspx" href="http://msdn.microsoft.com/en-us/library/ee370577.aspx"&gt;http://msdn.microsoft.com/en-us/library/ee370577.aspx&lt;/a&gt;).&lt;/p&gt;  &lt;p&gt;You could imagine transforming, interpreting or compiling this AST using pattern matching on the different constructors.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Making the web easier !&lt;/h2&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Although we can regret F# lacks LISP macros, it is one of those languages that are expressive enough to allow multi-paradigm programming and the creation of DSL. We could then use F# as a base language for the web. Instead of having those four languages we talked about earlier. We could just only use one! We could then use F# functions to design declarative languages to represents HTML and CSS, use F# type providers syntax modeled by monads to query data sources as a replacement of SQL and basic F# for the logic to replace JavaScript and C#, Java or PHP.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;    &lt;p&gt;[Coming Soon]&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10345928" width="1" height="1"&gt;</description></item><item><title>Windows 8: Time to bite the Apple</title><link>http://blogs.msdn.com/b/doriancorompt/archive/2012/06/19/windows-8-time-to-bite-the-apple.aspx</link><pubDate>Tue, 19 Jun 2012 08:25:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10321658</guid><dc:creator>DorianCorompt</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/doriancorompt/rsscomments.aspx?WeblogPostID=10321658</wfw:commentRss><comments>http://blogs.msdn.com/b/doriancorompt/archive/2012/06/19/windows-8-time-to-bite-the-apple.aspx#comments</comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;A short post to share a video featuring a new series of well designed slates running on Windows 8:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:9c3b8071-7061-4ebd-8969-04868aadef6f" class="wlWriterEditableSmartContent" style="margin: 0px; padding: 0px; float: none; display: inline;"&gt;&lt;div&gt;&lt;object width="448" height="252"&gt;&lt;param name="movie" value="http://www.youtube.com/v/dpzu3HM2CIo?hl=en&amp;amp;hd=1"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/dpzu3HM2CIo?hl=en&amp;amp;hd=1" type="application/x-shockwave-flash" width="448" height="252"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;/div&gt;&lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Important to notice that these slates would be available on both ARM and PC architectures.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10321658" width="1" height="1"&gt;</description></item><item><title>7. Workflows – A Monad alias</title><link>http://blogs.msdn.com/b/doriancorompt/archive/2012/05/25/7-workflows-a-monad-alias.aspx</link><pubDate>Fri, 25 May 2012 15:43:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10310474</guid><dc:creator>DorianCorompt</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/doriancorompt/rsscomments.aspx?WeblogPostID=10310474</wfw:commentRss><comments>http://blogs.msdn.com/b/doriancorompt/archive/2012/05/25/7-workflows-a-monad-alias.aspx#comments</comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Monads are one of the most complex concept to apprehend I encountered while learning functional programming, and unfortunately, like many developers I probably use them without understanding all the possibilities they offer. Before talking about what are Monads, it is interesting to understand where they come from. Monads come from Math, and are used as far as I know by two programming Haskell and F#.&lt;/p&gt;  &lt;p&gt;Haskell designers philosophy was to create a pure functional programming language: a language without side effects. However there are side effects that you cannot avoid: The first unavoidable type of side effects coming to mind is IOs. Indeed if your functions have to, given the same input always return the same output, it would be impossible to represent some functions such as the one which gets a line typed on the keyboard by the user and returns it as a string. &lt;strong&gt;Interactions with the real world generate side effects.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;In order to avoid creating an unusable language Haskell designers looked for a way to separate side effects code from the rest of the code, in way that pure and impure code won’t mix, if you introduce an impure function in a pure function the code is tainted as being impure. That’s where Monads come into play.&lt;/p&gt;  &lt;p&gt;Monads represent a really generic powerful concept, which has a lot of other usages, as you will see F# don’t even use Monads for IO.&lt;/p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;h2&gt;What are Monads?&lt;/h2&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Wikipedia:&lt;/u&gt; &lt;/p&gt;  &lt;p&gt;&lt;em&gt;“In &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Functional_programming"&gt;&lt;em&gt;functional programming&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, a &lt;b&gt;monad&lt;/b&gt; is a &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Programming"&gt;&lt;em&gt;programming&lt;/em&gt;&lt;/a&gt;&lt;em&gt; structure that represents &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Computation"&gt;&lt;em&gt;computations&lt;/em&gt;&lt;/a&gt;&lt;em&gt;. Monads are a kind of &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Abstract_data_type"&gt;&lt;em&gt;abstract data type&lt;/em&gt;&lt;/a&gt;&lt;em&gt; &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Type_constructor"&gt;&lt;em&gt;constructor&lt;/em&gt;&lt;/a&gt;&lt;em&gt; that &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Separation_of_concerns"&gt;&lt;em&gt;encapsulate&lt;/em&gt;&lt;/a&gt;&lt;em&gt; program logic instead of data in the &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Domain_model"&gt;&lt;em&gt;domain model&lt;/em&gt;&lt;/a&gt;&lt;em&gt;. A defined monad allows the programmer to chain actions together and build different &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Pipeline_(computing)"&gt;&lt;em&gt;pipelines&lt;/em&gt;&lt;/a&gt;&lt;em&gt; that process data in various steps, in which each action is &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Decorator_pattern"&gt;&lt;em&gt;decorated&lt;/em&gt;&lt;/a&gt;&lt;em&gt; with additional processing rules provided by the monad. For example, a sequence of &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Arithmetic"&gt;&lt;em&gt;arithmetic&lt;/em&gt;&lt;/a&gt;&lt;em&gt; operations can be controlled to avoid &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Division_by_zero"&gt;&lt;em&gt;division by zero&lt;/em&gt;&lt;/a&gt;&lt;em&gt; in intermediate results. Programs written in functional style can make use of monads to structure procedures that include sequenced operations,&lt;sup&gt;&lt;a href="http://en.wikipedia.org/wiki/Monad_(functional_programming)#cite_note-0"&gt;[1]&lt;/a&gt;&lt;/sup&gt;&lt;sup&gt;&lt;a href="http://en.wikipedia.org/wiki/Monad_(functional_programming)#cite_note-1"&gt;[2]&lt;/a&gt;&lt;/sup&gt; or to define some arbitrary &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Control_flow"&gt;&lt;em&gt;control flows&lt;/em&gt;&lt;/a&gt;&lt;em&gt; (like handling &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Concurrency_(computer_science)"&gt;&lt;em&gt;concurrency&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Continuation"&gt;&lt;em&gt;continuations&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Side_effect_(computer_science)"&gt;&lt;em&gt;side effects&lt;/em&gt;&lt;/a&gt;&lt;em&gt; such as &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Input/output"&gt;&lt;em&gt;input/output&lt;/em&gt;&lt;/a&gt;&lt;em&gt; and variable &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Assignment_(computer_science)"&gt;&lt;em&gt;assignment&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, or &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Exception_handling"&gt;&lt;em&gt;exceptions&lt;/em&gt;&lt;/a&gt;&lt;em&gt;).&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Formally, a monad is constructed by defining two operations (bind and return) and a &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Type_constructor"&gt;&lt;em&gt;type constructor&lt;/em&gt;&lt;/a&gt;&lt;em&gt; M that must fulfill several properties to allow the correct composition of monadic functions (i.e. functions that use values from the monad as their arguments). The return operation takes a value from a plain type and puts it into a monadic container of type M. The bind operation performs the reverse process, extracting the original value from the container and passing it to the associated next function in the pipeline.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;A programmer uses an existing monad by composing monadic functions to define a new data-processing pipeline. The monad acts as a &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Software_framework"&gt;&lt;em&gt;framework&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, as it's a reusable behavior that decides the order in which the specific monadic functions in the pipeline are called, and manages all the undercover work required by the computation.&lt;sup&gt;&lt;a href="http://en.wikipedia.org/wiki/Monad_(functional_programming)#cite_note-2"&gt;[3]&lt;/a&gt;&lt;/sup&gt; The bind and return operators interleaved in the pipeline will be executed after each monadic function returns control, and will take care of the particular &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Aspect_(computer_science)"&gt;&lt;em&gt;aspects&lt;/em&gt;&lt;/a&gt;&lt;em&gt; handled by the monad.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;The name is taken from the &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Monad_(category_theory)"&gt;&lt;em&gt;mathematical monad&lt;/em&gt;&lt;/a&gt;&lt;em&gt; construct in &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Category_theory"&gt;&lt;em&gt;category theory&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, though the two concepts are not identical.”&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;This definition is quite big, and hard to understand, let’s highlight the important ideas:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;strong&gt;1- A monad is a programming structure that represents computations&lt;/strong&gt;&lt;/p&gt;   &lt;strong&gt;&lt;/strong&gt;    &lt;p&gt;&lt;strong&gt;2- A defined monad allows the programmer to chain actions together&lt;/strong&gt;&lt;/p&gt;   &lt;strong&gt;&lt;/strong&gt;    &lt;p&gt;&lt;strong&gt;3- A monad is constructed by defining two operations (bind and return) and a type constructor M that must fulfill several properties to allow the correct composition of monadic functions&lt;/strong&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&amp;#160; &lt;/p&gt;  &lt;h2&gt;Better an example than precept&lt;/h2&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Let’s say you want to define a function to look for an element in a key value pair list given its key , this function could have the type: (‘a –&amp;gt; (‘a * ‘b) list –&amp;gt; ‘b) if an equality operator is defined for type ‘a. &lt;/p&gt;  &lt;p&gt;In the following example the list of pairs is a phone book. It associates contact names with phone numbers. I want to get a phone number given a contact name:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1462.image_5F00_5B008F23.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1440.image_5F00_thumb_5F00_726B1D87.png" width="466" height="344" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;u&gt;About the contact names chosen in this example:&lt;/u&gt; In Warring States period, the whole Japan history might have changed on June 21, 1582, if Hideyoshi had a mobile phone. Due to simple a letter which didn’t come on time, the most powerful Japanese Daimyou, Oda Nobunaga died, killed by one of his men Mitsuhide who misunderstood his true intentions. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160; &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/3515.image_5F00_72A7A0BC.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/2110.image_5F00_thumb_5F00_7F354DD8.png" width="525" height="283" /&gt;&lt;/a&gt;&amp;#160; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Our function &lt;em&gt;lookFor&lt;/em&gt; works nice, however it has a quite heavy behavior when a key is not found, it throws an exception. In dynamic typed languages you could&amp;#160; return some meaningful value such as a&lt;em&gt; false&lt;/em&gt; boolean value if a key is not found. In typed languages such as C# you could use a &lt;em&gt;null &lt;/em&gt;value, however you probably know that it has to be handled carefully, since if not some null reference exceptions may appear in your code, returned values need to be checked on each call. That’s why without exception C#, error handling and propagation would look pretty like C:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/2526.image_5F00_6BA7EE37.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7701.image_5F00_thumb_5F00_4EBEC962.png" width="288" height="231" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;It is interesting to notice how with the indentation the code is going to the right, making it hard to read. I will talk about it later.&lt;/p&gt;  &lt;p&gt;We wish our functions to be able to either return a valid result or a meaningful value to indicate our computation failed, if failed the status should be propagated skipping the next computing steps. And possibly avoiding the code overhead needed to achieve this propagation (which makes your code little by little indent to the right.)&lt;/p&gt;  &lt;p&gt;Believe it or not, but Monads can help us here.&lt;/p&gt;  &lt;p&gt;First, we are going to create a type to represent the fact a computation may or may not success in returning a value. And then update our lookFor function definition to use this type.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7888.image_5F00_09A5FC2C.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/4251.image_5F00_thumb_5F00_33A5FA54.png" width="295" height="115" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The maybe type is a generic type, when a function successes it will return &lt;em&gt;Just value&lt;/em&gt;, and when it fails it will return &lt;em&gt;Nothing:&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/2502.image_5F00_19659130.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/3107.image_5F00_thumb_5F00_7F25280B.png" width="300" height="81" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Although more meaningful, we now have an equivalent to the return valid value or return null value pattern we showed in C#.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;Note:&lt;/u&gt; There already is a type&amp;#160; equivalent to &lt;em&gt;Maybe&amp;lt;’a&amp;gt;,&lt;/em&gt; it is defined in the F# core library and called &lt;em&gt;option&amp;lt;’a&amp;gt;.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Let say I want to create a function which given a phone book will check that all my friends contact are in it, and return their numbers. If one of my contact numbers is not in the phone book the whole function should tell me it failed. If you follow the cascading if pattern, you will get something like:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/6708.image_5F00_3E16A8A7.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/4645.image_5F00_thumb_5F00_3CD20FC8.png" width="455" height="165" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;That’s bad right? We still have these redundant checks on the result values, and the code keeps moving to the right. We are going to try factorize this a little first :&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/6708.image_5F00_1B726A2C.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/4237.image_5F00_thumb_5F00_053C4EDA.png" width="464" height="182" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;We use here a pattern quite common in functional programming languages which consists in capturing continuations with closures (cf. my article on continuations). If you come from non functional language and worked with some asynchronous code, you may be familiar with the concept of callbacks which is similar.&lt;/p&gt;  &lt;p&gt;Here we succeeded in factorizing the conditional (match) expressions by defining a check function. This function takes a &lt;em&gt;Maybe&lt;/em&gt; value and a function representing a continuation, and according to the value it stops or continue the computation with the value wrapped by the &lt;em&gt;Just &lt;/em&gt;constructor.&lt;/p&gt;  &lt;p&gt;We are close from a Monad here, actually, if we generalize our code a little more it is one. Conceptually, we have a context, the type representing a possibility of failure, this context wraps a value, and thanks to a function, &lt;em&gt;check&lt;/em&gt; in our example we are able to propagate this context through the different steps of our computation by extracting and wrapping back the value into the context. The function we use to wrap back the value into a context is in our specific case the &lt;em&gt;Just&lt;/em&gt; type constructor.&lt;/p&gt;  &lt;p&gt;If you remember one of the points we highlighted previously: “&lt;em&gt;A monad is constructed by defining two operations (bind and return) and a type constructor M that must fulfill several properties to allow the correct composition of monadic functions&lt;/em&gt;”. &lt;/p&gt;  &lt;p&gt;The &lt;em&gt;check &lt;/em&gt;function we defined is the bind operation, while the&lt;em&gt; Just&lt;/em&gt; constructor is our return operation:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/6303.image_5F00_63DCA93D.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/3250.image_5F00_thumb_5F00_504F499C.png" width="495" height="198" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Ok, I know, what you are thinking, I just renamed some functions. But it is important to clearly see those two operations. And by defining them we officially created our first Monad!&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;A Monad, is it only that???&lt;/h2&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;No, no, don’t worry, there is more to come. The previous pattern I showed you may appear quite specific to my error handling problem, but actually it is really generic! So generic that the concept of Monads got its own syntax in Haskell and F#.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;But, first I have to admit I lied to you, Monads aren’t called Monads in F#, they are called workflows, or computational expressions. And if you remember the second point we highlighted in the Monad definition, you can easily understand why: “&lt;em&gt;A defined monad allows the programmer to chain actions together&lt;/em&gt;”. It is exactly what we did in our error handling example, we chained functions calls, in a way the failure context is managed and propagated between calls.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;To define a Monad in F# and take advantage of a syntax I am going to show you, we need to make a builder type.&lt;/p&gt;  &lt;p&gt;A builder is a .NET class that contains methods definitions for the bind and return operations, and possibly other operations related to F# monads. To correctly define this class we need at least to implement three methods Return(value), Bind(value, continuation) and Zero(). The Zero() method returns the default Monadic value. Once we have this class, we make a singleton instance from it.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1581.image_5F00_2EEFA400.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/6371.image_5F00_thumb_5F00_1FD8C526.png" width="246" height="150" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;This is the correct way to define a basic Monad in F#.&lt;/p&gt;  &lt;p&gt;We can now enjoy a more friendly syntax to rewrite our&lt;em&gt; getContactNumbers&lt;/em&gt; function.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/2577.image_5F00_7A6ED1B7.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/5488.image_5F00_thumb_5F00_6B57F2DD.png" width="343" height="101" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Look how nice this code looks!&lt;/p&gt;  &lt;p&gt;Our code which was moving to the right and was hardly readable is now really concise and clear. What happened here? We used our singleton instance, curly braces and the new &lt;em&gt;let!&lt;/em&gt; and &lt;em&gt;return&lt;/em&gt; keywords to improve the readability. Under the hood &lt;em&gt;let!&lt;/em&gt; just calls our Monad &lt;em&gt;Bind(…)&lt;/em&gt; operation, implicitly capturing the current continuation while the &lt;em&gt;return &lt;/em&gt;keyword calls our Monad &lt;em&gt;Return(…)&lt;/em&gt; method. If you are an imperative programmer it is really important to understand that the &lt;em&gt;return&lt;/em&gt; keyword has strictly nothing to do with the &lt;em&gt;return&lt;/em&gt; keyword you may encounter in C, C++, or C# for example: it won’t break the flow of a computation, it is just a way to wrap a value into the context expressed by the used Monad.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;You already know a Monad…&lt;/h2&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I told you the monad concept, although strange at a first glance, is really powerful and generic. The proof is that if you read my previous article on Sequences, you already met and used Monads.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Indeed F# sequences are Monads! And in Haskell even list are monads. Why? Because if you think about it, list or sequences are just a context around values, and you may want to propagate this sequence context through computations while modifying the value(s) wrapped. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/4705.image_5F00_5836C631.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/4212.image_5F00_thumb_5F00_30241712.png" width="374" height="66" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Here the &lt;em&gt;yield&lt;/em&gt; keyword is a keyword strictly equivalent to the&lt;em&gt; return&lt;/em&gt; keyword, it has been introduced to make the &lt;em&gt;return&lt;/em&gt; keyword use in sequences more natural. If you know generators in languages such as Python or C#, you probably understand why.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Windows 8, coming soon…&lt;/h2&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;As you probably know, Windows 8 is going to be released in a few weeks. If you already tried to look at how to develop Metro UI applications in C#, you probably encountered two new keywords (C# 5.0): The&lt;em&gt; async, &lt;/em&gt;and&lt;em&gt; await&lt;/em&gt; keywords.&lt;/p&gt;  &lt;p&gt;WinRT the native library behind Metro, has been designed to use and force the use of an asynchronous model for parallelism. Parallelism is necessary to handle time consuming operations outside the UI thread, and then avoiding freezing the UI and degrade the user experience.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The main asynchronous model advantage&lt;/strong&gt; is that it prevents most developers (95% of them?) to keep playing randomly with threads and synchronization object: This is indeed a fact, most developers don’t know how to correctly use parallelism and…&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;NON EXPERT DEVELOPER + THREADING + SIDE EFFECTS = CHAOS&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;You can replace chaos by:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Deadlocks&lt;/li&gt;    &lt;li&gt;Inability to debug programs&lt;/li&gt;    &lt;li&gt;Program crashing once or twice a month without any reason&lt;/li&gt;    &lt;li&gt;Computer crashes&lt;/li&gt;    &lt;li&gt;Train accidents&lt;/li&gt;    &lt;li&gt;Aircraft crashes&lt;/li&gt;    &lt;li&gt;…&lt;/li&gt;    &lt;li&gt;End of the world?&lt;/li&gt;  &lt;/ul&gt;  &lt;p&gt;And I wondering if it is not the reason why functional programming languages are recently coming back to the scene.&lt;/p&gt;  &lt;p&gt;Indeed&lt;strong&gt; &lt;/strong&gt;we are in the era of parallelism: multicore processors / distributed computations and shared resources with cloud computing / web and communication (part of the IOs in a program increasing). Functional languages by discouraging side effects make parallelism safer, and ease its use with a lot of powerful concepts.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The main asynchronous model drawback&lt;/strong&gt; is that it is not natural. Look at this C# example:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/4130.image_5F00_2158D1E1.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0820.image_5F00_thumb_5F00_78D9EFCC.png" width="816" height="390" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;With all this code, I am just reading three lines from a stream through the use of an asynchronous method, and displaying them. Look how intuitive this code can be… And be happy without closures in C#, you couldn’t write it so easily.&lt;/p&gt;  &lt;p&gt;Earlier when we were working on the path leading to Monads, I wrote something about the fact callbacks could be considered as a similar concept for continuations. Look at the code, it is moving to the right, and we are capturing continuations, the “which operation should be done next”. You guess it now, there is a Monad to deal with asynchronous operations.&lt;/p&gt;  &lt;p&gt;It is called&lt;em&gt; async,in F#&lt;/em&gt; and you use it like this:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/8816.image_5F00_4B98F348.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1641.image_5F00_thumb_5F00_231A1134.png" width="461" height="414" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The use of &lt;em&gt;let!&lt;/em&gt; here captures the current continuation and calls it with the result once the page is downloaded.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;C# the next functional programming language?&lt;/h2&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Believe it or not, but after borrowing the concept of garbage collector (LISP), the OOP paradigm (LISP), closures, Linq, C# is now borrowing an other powerful concept from the functional programming world: Monads, more precisely the async particular monad.&lt;/p&gt;  &lt;p&gt;WinRT designers made a choice, between allowing developers to use threads – should be read &lt;em&gt;“Making children play with bazooka” –&lt;/em&gt;&amp;#160; or make the move to a non natural programming model for imperative languages. To turn the asynchronous C# into something useable, they needed a powerful addition coming from the functional programming world.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;But we still have a problem here… Among C# developers, who knows about Monads?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Who knows that the await keyword is a disguised bind operation capturing the current continuation within an async method ?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;You could rightly argue it is irrelevant since what is important is to know how to use it. I would answer OS designers thought the same way when they introduced preemptive schedulers, and gave developers process/threading APIs.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10310474" width="1" height="1"&gt;</description></item><item><title>Bringing CLR’s Power to non .NET languages - Part 1</title><link>http://blogs.msdn.com/b/doriancorompt/archive/2012/04/25/bringing-the-power-of-the-clr-to-non-net-languages-part-1.aspx</link><pubDate>Wed, 25 Apr 2012 15:35:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10297656</guid><dc:creator>DorianCorompt</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/doriancorompt/rsscomments.aspx?WeblogPostID=10297656</wfw:commentRss><comments>http://blogs.msdn.com/b/doriancorompt/archive/2012/04/25/bringing-the-power-of-the-clr-to-non-net-languages-part-1.aspx#comments</comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The .NET Framework helps developer boost their productivity by giving them a set of tools and libraries they need to quickly start implementing the core of their software without loosing time on details. The idea behind the Common Language Infrastructure (CLI) is to allow programming languages to interoperate by sharing code through libraries.&lt;/p&gt;  &lt;p&gt;To take advantage of these libraries, a lot of languages got their .NET implementation counterparts, with for example: IronPython, IronRuby, or IronScheme. See &lt;a title="http://en.wikipedia.org/wiki/List_of_CLI_languages#CLI_languages" href="http://en.wikipedia.org/wiki/List_of_CLI_languages#CLI_languages"&gt;http://en.wikipedia.org/wiki/List_of_CLI_languages#CLI_languages&lt;/a&gt; for more implementations.&lt;/p&gt;  &lt;p&gt;Most of these languages when interpreted are implemented in .NET languages and can easily interact with the underlying platform. For compiled languages they generate IL and the result is a .NET assembly.&lt;/p&gt;  &lt;p&gt;In the same way you can dynamically load symbols from a shared library, there are times you would like to be able to interact with the .NET Framework from non .NET languages such as C, C++, Java, Perl or PHP.&lt;/p&gt;  &lt;p&gt;Through a short series of articles I am going to show you how to achieve this goal and get the power of .NET in all your favorite languages in less than 500 lines of code.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Back to the origin…&lt;/h2&gt;  &lt;p&gt;Like for software components at a higher level, the best way to make programming languages interoperate with each other is to define some kind of interfaces. If we look what has been done in the past with native languages we can consider as part of this interface: the processor architecture and its instruction set, the binary format with the way symbols are exported, the type sizes and the conventions to call code.&lt;/p&gt;  &lt;p&gt;Assuming that your binaries respect this same interface they can freely share symbols. We are going to respect this interface in order to build a bridge library between the native and the .NET worlds. This native library would be then accessible from non .NET languages.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;&lt;strong&gt;C+++&lt;/strong&gt;&lt;strong&gt;+++…&lt;/strong&gt;&lt;/h2&gt;  &lt;p&gt;Did I already tell you how much I like C++? (cf. &lt;a title="http://blogs.msdn.com/b/doriancorompt/archive/2012/04/25/6-sequences-being-lazy-is-allowed.aspx" href="http://blogs.msdn.com/b/doriancorompt/archive/2012/04/25/6-sequences-being-lazy-is-allowed.aspx"&gt;http://blogs.msdn.com/b/doriancorompt/archive/2012/04/25/6-sequences-being-lazy-is-allowed.aspx&lt;/a&gt;)&lt;/p&gt;  &lt;p&gt;But don’t worry I have good news for you, a nice version of C++ exists in this world! It is C++/CLI! As you probably guessed this version of C++ is targeting the Common Language Infrastructure. Besides the fact it fills some C++ lacks such as automatic memory management and boilerplate code generation for data member accessors, one of the good thing is that it provides you a seamless and simple way to mix native and managed code. Moreover once compiled the binary has both native and managed sections and can export native symbols such as functions using CLR objects.&lt;/p&gt;  &lt;p&gt;As a solution for our problem I then suggest to use C++/CLI to build an interoperability DLL, whose functions are dynamically loadable by non .NET languages and which will allow us to use the .NET Framework.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Bridge the two worlds&lt;/h2&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Exporting the API&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;To export our DLL symbols we define a macro&lt;em&gt; CLR_API&lt;/em&gt;:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/8233.clr_5F00_api_5F00_0EF62317.png"&gt;&lt;img title="clr_api" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="clr_api" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/6663.clr_5F00_api_5F00_thumb_5F00_0DB18A38.png" width="371" height="85" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Notice that we use &lt;em&gt;extern “C”&lt;/em&gt; to avoid C++ symbol names mangling.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Mapping primitive types&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;First let’s make some typdefs for primitive types. We do so to ensure that primitive types conserve the same size between architectures. For example a &lt;em&gt;long&lt;/em&gt; size is 32 bits on a 32 bits architecture while being 64 bits on a 64 bits architecture. A &lt;em&gt;long long&lt;/em&gt; is however always 64 bits which matches the CLR&lt;em&gt; long&lt;/em&gt; type alias to &lt;em&gt;Int64&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0882.clr_5F00_ptypes_5F00_24FC8EA9.png"&gt;&lt;img title="clr_ptypes" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="clr_ptypes" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/2538.clr_5F00_ptypes_5F00_thumb_5F00_78735EC2.png" width="282" height="449" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Note that&lt;em&gt; t_decimal, t_char,&lt;/em&gt; and &lt;em&gt;t_string&lt;/em&gt; don’t match their CLR counterparts, but exists only for the native to native interoperability interface. Bytes strings are used to ensure that languages that don’t support Unicode&amp;#160; can still use the interface. The &lt;em&gt;null &lt;/em&gt;macro gives C++ a C# flavor, however C++/CLI null pointer is not &lt;em&gt;0&lt;/em&gt; but the special value &lt;em&gt;nullptr&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Release dynamically allocated memory&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;C++/CLI will automatically to the marshaling necessary to map our primitive types however we need to handle the case of CLR object references. In C++/CLI CLR object references are accessed through handles. Our native interface to be exported has to hide these CLR object references. The best way to do it is to wrap CLR object handles within plain native C++ objects. Doing so brings a little complexity, since while CLR object are released by the CLR garbage collector, dynamically allocated native objects have to be released manually through the use of the &lt;em&gt;delete&lt;/em&gt; operator.&lt;/p&gt;  &lt;p&gt;We don’t know when the language using our library will need to release our objects so one of the best solution here, is to use an object reference tracking method such as reference counting used by COM objects. The class below defines the C++ methods needed for our reference counting.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1055.clr_5F00_ref_5F00_4FF47CAE.png"&gt;&lt;img title="clr_ref" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="clr_ref" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7823.clr_5F00_ref_5F00_thumb_5F00_6E5EBD97.png" width="308" height="533" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;We define two functions to use these references in our interface:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1440.clr_5F00_ref_5F00_api_5F00_6D1A24B8.png"&gt;&lt;img title="clr_ref_api" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="clr_ref_api" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/2626.clr_5F00_ref_5F00_api_5F00_thumb_5F00_7E1E529B.png" width="392" height="35" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Wrapping CLR objects&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Ok we can have a solution for references, we now need to find a way to wrap CLR objects into a native type inheriting from &lt;em&gt;CLRReference&lt;/em&gt;. This is done thanks to the use of the following C++ class template:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/6327.clr_5F00_wrapper_5F00_5D9712E9.png"&gt;&lt;img title="clr_wrapper" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="clr_wrapper" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7823.clr_5F00_wrapper_5F00_thumb_5F00_4356A9C5.png" width="300" height="388" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;CLRWrapper&amp;lt;T&amp;gt; &lt;/em&gt;wraps the CLR object handle in its private data member &lt;em&gt;value&lt;/em&gt;. The &lt;em&gt;gcroot&amp;lt;T&amp;gt;&lt;/em&gt; class template provided by C++/CLI allows us to use CLR object as members for native classes by pinning the handle and preventing the garbage collector to play with our reference in memory. As an example of use &lt;em&gt;CLRWrapper&amp;lt;System::Object&amp;gt;*&lt;/em&gt; is a wrapper pointer to a the CLR &lt;em&gt;Object &lt;/em&gt;type, and the&lt;em&gt; GetValue&lt;/em&gt; getter method can be used to get the wrapped CLR object value.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Boxing/Unboxing primitive types&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;As you will see in a few paragraph to be generic our interface is going to rely on the fact that all CLR objects inherit from the &lt;em&gt;Object &lt;/em&gt;class, and boxing and unboxing will be necessary for primitive types, so let define converter functions to do so:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/8468.clr_5F00_converters_5F00_421210E6.png"&gt;&lt;img title="clr_converters" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="clr_converters" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/8372.clr_5F00_converters_5F00_thumb_5F00_40614512.png" width="467" height="467" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Catch exceptions&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;While working within the CLR world exceptions might occur, we need a way to represent and handle possible exceptions in our interface, this is done thanks to the template CLRResult&amp;lt;T&amp;gt; class below which inherits from CLRWrapper&amp;lt;T&amp;gt;:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/3175.clr_5F00_result_5F00_25B4A8F9.png"&gt;&lt;img title="clr_result" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="clr_result" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/8865.clr_5F00_result_5F00_thumb_5F00_441EE9E2.png" width="600" height="576" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;We define two functions in our interface to work with this class instances:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/2086.image_5F00_3BBB148B.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0116.image_5F00_thumb_5F00_13A8656C.png" width="550" height="34" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;If the C function &lt;em&gt;CLRResultIsSuccess &lt;/em&gt;returns true, no exception occurred and the internal exception wrapper pointer will be &lt;em&gt;null&lt;/em&gt;, the latter value available otherwise.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Calling code in&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;In programming everything is basically code executed thanks to processor instructions, or data located in some memory. In the previous sections I mainly wrote about data with primitive types and objects, it is now time to talk about code.&lt;/p&gt;  &lt;p&gt;The main purpose is to be able to call CLR methods on CLR objects from a native interface. There are several way of doing this, I chose the solution which gave me the best performances. This solution consists in generating on the fly a delegate, which captures a call to the target method. Once this delegate created it can be cached, and reuse as much as you want without needing to resolve the method on the type again. Therefore once cached the performance are really good and the time needed to call the delegate is nearly the same as if you were calling it directly from .NET! The on the fly generation is possible thanks to the&lt;em&gt; System::Reflection::Emit&lt;/em&gt; namespace classes.&lt;/p&gt;  &lt;p&gt;Let’s have a look at the interface we are going to implement:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/8053.image_5F00_399E15C2.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/8446.image_5F00_thumb_5F00_18AAA31B.png" width="439" height="20" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1385.image_5F00_3CBFE826.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1362.image_5F00_thumb_5F00_0D8DFC8F.png" width="316" height="178" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The first function &lt;em&gt;CLRMethodGet&lt;/em&gt; is doing the job of resolving the method on a particular type with the specified name and accepting the given parameter types then it generates a delegate of type &lt;em&gt;Method&lt;/em&gt; invoking this particular method. Notice that it returns a&lt;em&gt; CLRResult&amp;lt;T&amp;gt;&lt;/em&gt; instance since an exception might occur if no method matching the prototype is found on type.&lt;/p&gt;  &lt;p&gt;The second function invokes the delegate of type&lt;em&gt; Method &lt;/em&gt;given an instance object and a list of arguments, the result of the call is then returned a possible exception might have occurred during the call and can be checked.&lt;/p&gt;  &lt;p&gt;To handle all possibilities with these two functions the rules are:&lt;/p&gt;  &lt;p&gt;For &lt;em&gt;CLRGetMethod&lt;/em&gt; the target method name should be:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;.ctor for a constructor.&lt;/li&gt;    &lt;li&gt;get_&amp;lt;name&amp;gt; for a property getter.&lt;/li&gt;    &lt;li&gt;set_&amp;lt;name&amp;gt; for a property setter.&lt;/li&gt;    &lt;li&gt;add_&amp;lt;name&amp;gt; for an event adder.&lt;/li&gt;    &lt;li&gt;remove_&amp;lt;name&amp;gt; for an event remover.&lt;/li&gt;  &lt;/ul&gt;  &lt;p&gt;For &lt;em&gt;CLRInvoke &lt;/em&gt;the instance object may be null for static methods.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Calling code out&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The above sections explains how to call .NET methods from a native interface. What about calling native functions from the .NET world? I mean let say we want one of our target language function to be an event handler to some .NET object events.&lt;/p&gt;  &lt;p&gt;For this purpose we declare the following function in the interface:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/8015.image_5F00_1C6847EB.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/2161.image_5F00_thumb_5F00_348BB246.png" width="594" height="23" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;It creates a delegate with the given type from the specified native function pointer.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;We are almost done !&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;As you perhaps noticed we are close to the end, we are however still missing something, the function GetMethod needs &lt;em&gt;Type&lt;/em&gt; and &lt;em&gt;Array&lt;/em&gt; of &lt;em&gt;Types&lt;/em&gt; wrappers as arguments, and CLRInvoke needs an &lt;em&gt;Array &lt;/em&gt;of&lt;em&gt; Objects&lt;/em&gt; wrappers as argument. Where are we getting CLR type from? From assemblies of course. So we need a way to load assemblies, get type from them based on their names and a way to create and populate CLR arrays.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/8228.image_5F00_5A51C325.png"&gt;&lt;img title="image" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1385.image_5F00_thumb_5F00_7FDB4086.png" width="646" height="272" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Done!&amp;#160; With these about 40 native C functions we are able to interoperate with the .NET Framework! And some of them could have been defined in term of CLRGetMethod and CLRInvoke within the target language rather than C++/CLI.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;End of part 1&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I am going to publish the complete sources as soon as possible on Codeplex.&lt;/p&gt;  &lt;p&gt;Although this interface can be used directly from C or C++. My next article will show an example of use of this interface from a non .NET exotic language: Racket.&lt;/p&gt;  &lt;p&gt;Thank you for reading !&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10297656" width="1" height="1"&gt;</description></item><item><title>6. Sequences – Being lazy is allowed</title><link>http://blogs.msdn.com/b/doriancorompt/archive/2012/04/25/6-sequences-being-lazy-is-allowed.aspx</link><pubDate>Wed, 25 Apr 2012 08:47:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10297506</guid><dc:creator>DorianCorompt</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/doriancorompt/rsscomments.aspx?WeblogPostID=10297506</wfw:commentRss><comments>http://blogs.msdn.com/b/doriancorompt/archive/2012/04/25/6-sequences-being-lazy-is-allowed.aspx#comments</comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I remember the day I got the mind shift about functional programming. I switched from being a fervent defender of&amp;#160; Java and C# to a passionate of LISP dialects. At the time I was convinced that there were nothing better than OOP and curly bracket syntaxes. I was then fighting for conventions and simplicity rather than language expressivity.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Why do not C and C++ have a module system ?&lt;/li&gt;    &lt;li&gt;Why do not have C and C++ a garbage collector ?&lt;/li&gt;    &lt;li&gt;Why C does not have namespaces ?&lt;/li&gt;    &lt;li&gt;Why C does not have safer string and array primitives ?&lt;/li&gt;    &lt;li&gt;Why aren’t Unicode/UTF-8 strings the default in C and C++ ? &lt;/li&gt;    &lt;li&gt;Why are the C and C++ libraries naming convention so bad ?&lt;/li&gt;    &lt;li&gt;Why is C++ so complex?&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;As a good ignorant I thought that languages evolved with the time, and that these problems were not addressed because nobody thought about them before. And I discovered LISP the second oldest programming language with none of these problems and even powerful features other languages don’t have such as macros or continuation capture. I got interested in functional programming languages and I learned Haskell, CAML, and finally F#.&lt;/p&gt;  &lt;p&gt;I don’t know why but the concept of sequences in functional programming languages amazed me.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;How do you perceive infinity?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;“How do we perceive infinity?” &lt;/em&gt;This is the way &lt;a href="http://en.wikipedia.org/wiki/Gerald_Jay_Sussman"&gt;Gerald Jay Sussman&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Guy_L._Steele"&gt;Guy L. Steele&lt;/a&gt; introduced their presentation about sequences (streams in LISP).&lt;/p&gt;  &lt;p&gt;I never thought about it before, since resources on a computer are finite. But interesting question, let say I want to represent the infinite sequence of numbers, what characterizes this infinity and makes it exist? Is it the fact that these numbers are all present in the computer memory at the same time (which is impossible) or the fact that I can get one of them when needed.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;How could we possibly define the infinite sequence of natural integers?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Starting from 0 we can recursively define these numbers just by adding one, however this recursion shouldn’t take place until needed, so that the whole number sequence is never in memory. There is no choice that to delay the evaluation of the recursion building the sequence. We could take advantage of the fact that list are pairs, the car of the pair would be a value and the cdr of the pair a delayed evaluation leading to a pair. Delaying a computation, it’s when laziness comes into play.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;What is laziness?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Wikipedia: &lt;em&gt;“&lt;b&gt;Laziness&lt;/b&gt; (also called &lt;b&gt;indolence&lt;/b&gt;) is a disinclination to activity or exertion despite having the ability to do so. It is often used as a pejorative; related terms for a person seen to be lazy include &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Sedentary_lifestyle"&gt;&lt;em&gt;couch potato&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Slacker"&gt;&lt;em&gt;slacker&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, and &lt;/em&gt;&lt;a href="http://en.wiktionary.org/wiki/bludger"&gt;&lt;em&gt;bludger&lt;/em&gt;&lt;/a&gt;&lt;em&gt;.”&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;We are close to what we need ! &lt;/p&gt;  &lt;p&gt;Although very useful to deal with infinity, as for human laziness is unfortunately not well considered by programming languages. Indeed most programming languages favor eager evaluation over lazy evaluation.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;“Never put off until tomorrow what you can do the day after tomorrow.”&lt;/em&gt; &lt;/p&gt;  &lt;p&gt;Eager evaluation consists in evaluating as soon as possible expressions whereas lazy evaluations consists in evaluating an expression only when it is actually needed.&lt;/p&gt;  &lt;p&gt;Below is an example:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1070.Laziness_5F00_vs_5F00_eagerness_5F00_0010CA46.png"&gt;&lt;img title="Laziness_vs_eagerness" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="Laziness_vs_eagerness" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/6747.Laziness_5F00_vs_5F00_eagerness_5F00_thumb_5F00_65D06121.png" width="275" height="257" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;In a language like C# this program does nothing valuable except crashing (by exploding the stack). In a language using lazy evaluation this program does nothing. Why? Because &lt;em&gt;DoNothing&lt;/em&gt; does nothing with its argument &lt;em&gt;x&lt;/em&gt;, so why computing it? However with eager evaluation we start computing immediately the value of &lt;em&gt;x &lt;/em&gt;although we don’t need it!&lt;/p&gt;  &lt;p&gt; If you think about it you already know some cases of laziness:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/5277.Laziness_5F00_369E758A.png"&gt;&lt;img title="Laziness" style="display: inline; background-image: none;" border="0" alt="Laziness" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1882.Laziness_5F00_thumb_5F00_1C5E0C66.png" width="278" height="323" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Indeed in all languages conditional expressions are lazy. Why? Because if they weren’t, both the consequence and the alternative branches of the condition would be evaluated! However what is expected from a conditional expression is to first evaluate its condition and then depending on the result either evaluate the consequence or the alternative branch. In the same way the&lt;em&gt; and&lt;/em&gt; and &lt;em&gt;or&lt;/em&gt; short-circuit operators are lazy.&lt;/p&gt;  &lt;p&gt;An other way of achieving laziness is to capture expressions into functions. By doing so we are capturing the future of an expression, the value in becoming of this expression, also called its continuation. The problem in languages which doesn’t favor laziness is that you would have to build all these continuations by hand and call them at the right place when needed.&lt;/p&gt;  &lt;p&gt;In F# you can get laziness using the&lt;em&gt; lazy&lt;/em&gt; keyword or by building sequences. This will ensure you that this expression will be evaluated only when needed.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/5102.Lazy_5F00_54305089.png"&gt;&lt;img title="Lazy" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="Lazy" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0728.Lazy_5F00_thumb_5F00_52EBB7AA.png" width="399" height="146" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Let’s do it&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Ok we know that we need to build an infinite sequence using laziness. F# has its own sequence type that I am going to present you later, now I want you to look at a simple implementation of infinite sequences (which is really inefficient due to boxing and unboxing needed to escape static typing), the only purpose is to show you how it works:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/6607.lazy_5F00_list_5F00_78E16800.png"&gt;&lt;img title="lazy_list" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="lazy_list" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/8738.lazy_5F00_list_5F00_thumb_5F00_3047792F.png" width="612" height="728" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Here is the result:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/6685.lazy_5F00_list_5F00_out_5F00_2A8C5F89.png"&gt;&lt;img title="lazy_list_out" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="lazy_list_out" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0827.lazy_5F00_list_5F00_out_5F00_thumb_5F00_3B245A77.png" width="456" height="258" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Look the infinite &lt;em&gt;integers &lt;/em&gt;sequence is a list which starts by&lt;em&gt; 0&lt;/em&gt; and whose next value is not computed yet: “Value is not created”.&lt;/p&gt;  &lt;p&gt;With &lt;em&gt;lazyMap, lazyFilter&lt;/em&gt; you can operate on&lt;em&gt; integers&lt;/em&gt; to create new infinite lists ! If you look at the &lt;em&gt;lazyMap&lt;/em&gt; and &lt;em&gt;lazyFilter&lt;/em&gt; function definitions you won’t find any break condition, these functions don’t need to stop, since they are working on data with infinite length !&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Why do we need lazy sequences?&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Resolve algorithms: Infinite sequences allow us to find results for some recursive algorithms especially on big numbers that we couldn’t resolve with traditional methods. We can for example use the sieve of Eratosthenes to get a list of all primes number without excessive computation.      &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;Performance: When using functional programming languages it is common to compose map, filter, fold functions to get a result, each time you apply one of these on a list you are browsing the list from start to end. Thus to map and filter a list you will browse the list twice. Through the use of sequences this could be avoided, since the sequence elements will be mapped and filtered at the same time once the computation is triggered.      &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;Design Patterns: Did you already think about how to model I/O operations? Most languages usually represents them with the concept of streams that can be read from or written to. That’s good but there is nothing revolutionary. What about representing binary files by possibly infinite sequences of bytes and textual files as sequences of characters, words or lines for example? It would mean all your functions working on list could be used naturally&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;Avoiding side effects: I already told you that side effects are bad, so how to model change for an object without relying on side effects? Sequences can be use to represent each state of an object through time, so no need for side effects.&lt;/li&gt;  &lt;/ul&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;  &lt;p&gt;&lt;strong&gt;What are F# sequences?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;F# sequences are created using the &lt;em&gt;seq &lt;/em&gt;computation expression (cf. my article to come on Workflows). They are represented by the generic type &lt;em&gt;seq&amp;lt;’a&amp;gt;&lt;/em&gt; with&lt;em&gt; ‘a&lt;/em&gt; being the type of the elements in the sequence. Under the hood they map the well known .NET type &lt;em&gt;IEnumerable&amp;lt;T&amp;gt;. &lt;/em&gt;The&lt;em&gt; Seq &lt;/em&gt;module provides for use with sequences most of the functions working with lists. Finally F# arrays and lists are instance of &lt;em&gt;seq&amp;lt;’a&amp;gt;.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/3122.seq_5F00_3EEF0D34.png"&gt;&lt;img title="seq" style="border: 0px currentcolor; display: inline; background-image: none;" border="0" alt="seq" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1067.seq_5F00_thumb_5F00_563A11A5.png" width="495" height="361"&gt;&amp;#160;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10297506" width="1" height="1"&gt;</description></item><item><title>5. Generic Parameters – Static typing without typing</title><link>http://blogs.msdn.com/b/doriancorompt/archive/2012/03/03/5-generic-parameters-static-typing-without-typing.aspx</link><pubDate>Sat, 03 Mar 2012 18:24:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10277065</guid><dc:creator>DorianCorompt</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/doriancorompt/rsscomments.aspx?WeblogPostID=10277065</wfw:commentRss><comments>http://blogs.msdn.com/b/doriancorompt/archive/2012/03/03/5-generic-parameters-static-typing-without-typing.aspx#comments</comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Typing system&lt;/h2&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The functional core of F# is statically typed, which means all checks concerning typing are done during compile time. Both Statically typed and dynamically typed system have pros and cons. Dynamic typing allows flexibility while static typing allows safeness and speed. Then a lot of programming languages try to get the benefits of both worlds.&lt;/p&gt;  &lt;p&gt;A lot of OOP languages uses both dynamic and static typing. So It is the case of course for C# but also for F# which allows imperative language style OOP to ensure a perfect interoperability with the classes and other languages of the .NET Framework.&lt;/p&gt;  &lt;p&gt;Compilers for these languages try to optimize and check the code as much as possible, so they favor static typing and allow dynamic typing only when necessary. The good thing about F# is that if you write functional code thanks to generic parameters that I am going to introduce in this article and don’t lay on the imperative style OOP system you don’t need dynamic typing to get flexibility !&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;The imperative way&lt;/h2&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Let’s see how C# before its 2.0 version worked with polymorphism to resolve the flexibility versus safeness and speed dilemma (before C# 2.0 generics didn’t exist). Below are the types of polymorphism it supported:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Inclusion&lt;/li&gt;    &lt;li&gt;Overloading &lt;/li&gt;    &lt;li&gt;Coercion &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Example of C# code using those types of polymorphism:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/4718.polymorphism_5F00_3283EC47.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="polymorphism" border="0" alt="polymorphism" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7357.polymorphism_5F00_thumb_5F00_7E6F4CF3.png" width="487" height="377" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;u&gt;&lt;strong&gt;Description:&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;1) The&lt;em&gt; IList Add&lt;/em&gt; method takes an object as a first argument. Since the &lt;em&gt;String &lt;/em&gt;class extends from &lt;em&gt;Object &lt;/em&gt;you can use a string, this is a static upcast.&lt;/p&gt;  &lt;p&gt;2) We convert &lt;em&gt;1582&lt;/em&gt; which is by default an &lt;em&gt;int&lt;/em&gt; to an &lt;em&gt;uint&lt;/em&gt; this operation is not safe, but can be statically done since the number is a constant.&lt;/p&gt;  &lt;p&gt;3) We use a virtual method on an object, a dynamic link is necessary to find the method implementation to use.&lt;/p&gt;  &lt;p&gt;4) The &lt;em&gt;Console&lt;/em&gt; class has several overloads for the static method &lt;em&gt;Write&lt;/em&gt;. We choose the one taking an &lt;em&gt;object&lt;/em&gt; at compile time, this is static.&lt;/p&gt;  &lt;p&gt;5) In this case of coercion we are doing a downcast to convert an instance of Object to an instance of the UInt32 class, this is dynamic.&lt;/p&gt;  &lt;p&gt;Even if C# could have used dynamic polymorphism all the time, the compiler limited its use to certain types of inclusion and coercion polymorphisms for performance and safeness reasons. This is the reason behind the use of the &lt;em&gt;virtual&lt;/em&gt; and &lt;em&gt;override&lt;/em&gt; keywords in C#, the developer has to tell the compiler when to make dynamic links for inclusion polymorphism to avoid doing it all the time like in Java. And it also justifies the&lt;em&gt; as&lt;/em&gt; keyword which allows you to do quicker static downcast.&lt;/p&gt;  &lt;p&gt;From C# v2.0 a new type of polymorphism appeared thanks to the addition of generics in the language: the parametric polymorphism. And guess what? Don Syme the designer and architect of F# worked on it. The advantage of parametric polymorphism is that it allows you a lot of flexibility dynamic typing does but in a statically typed manner, so you get safeness and speed for free.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;&lt;/h2&gt;  &lt;h2&gt;The functional way&lt;/h2&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;When you writes a function with F#, the compilers tries to infer types for the input(s) and the output if it can’t it gives by default a generic type, represented by variable parameters called generic parameters. Generic parameters are prefixed by a single quote.&lt;/p&gt;  &lt;p&gt;What if you want to build the identity function in C# (without generics)?&lt;/p&gt;  &lt;p&gt;Knowing all objects extends from &lt;em&gt;Object &lt;/em&gt;you could do:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7848.identity_5F00_cs_5F00_7194F6D5.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="identity_cs" border="0" alt="identity_cs" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0412.identity_5F00_cs_5F00_thumb_5F00_30F2AA66.png" width="182" height="63" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;But later at some point in your program you would need to make downcast to operate on the value, so using some dynamic behaviors. &lt;/p&gt;  &lt;p&gt;With F# your function is by default generic and everything is static:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/4212.identity_5F00_fs_5F00_42630B3E.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="identity_fs" border="0" alt="identity_fs" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7455.identity_5F00_fs_5F00_thumb_5F00_68C4EE89.png" width="133" height="16" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;This function type is&lt;em&gt; ‘a –&amp;gt; ‘a &lt;/em&gt;where &lt;em&gt;‘a&lt;/em&gt; can be int, string, obj, …&lt;/p&gt;  &lt;p&gt;F# allows you to build optimized and checked generic operations without any effort !&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1104.generics1_5F00_0F26D1D5.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="generics1" border="0" alt="generics1" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/5381.generics1_5F00_thumb_5F00_2DFD45B3.png" width="278" height="156" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The simple example above shows you how you can define a function which represents the generic concept of repeating an action three times whatever the action is.&lt;/p&gt;  &lt;h2&gt;&amp;#160;&lt;/h2&gt;  &lt;h2&gt;The practical way&lt;/h2&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Some generic concepts are really pure, it is the case of the identity function. It works for all input you could give it and you don’t need to define specialized implementation.&lt;/p&gt;  &lt;p&gt;As long as you play with generic parameters and generic values your algorithm stay generics. As soon as you introduce literal values or non generic functions in your function definition, it becomes tainted, and types are inferred at the expense of genericity.&lt;/p&gt;  &lt;p&gt;Sometimes you may believe that primitive functions such as the mathematics operators&lt;em&gt; (+), (-), (*), (/), (%) …&lt;/em&gt; use generic parameters because they work for different types and give you some flexibility, but they are using method overloading. The reason is simple, they need to be implemented at the machine level specifying different implementations. If &lt;em&gt;(+)&lt;/em&gt; was generic and that you would have used it to add functions what would be its behavior?&lt;/p&gt;  &lt;p&gt;Example of how to make a non generic number algorithm generic: &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/2642.generics2_5F00_70F91420.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="generics2" border="0" alt="generics2" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/5488.generics2_5F00_thumb_5F00_766784C4.png" width="552" height="258" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10277065" width="1" height="1"&gt;</description></item><item><title>4. 1st Class Functions – Power is in simplicity</title><link>http://blogs.msdn.com/b/doriancorompt/archive/2012/02/05/4-1st-class-functions-power-is-in-simplicity.aspx</link><pubDate>Sun, 05 Feb 2012 17:14:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10264168</guid><dc:creator>DorianCorompt</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/doriancorompt/rsscomments.aspx?WeblogPostID=10264168</wfw:commentRss><comments>http://blogs.msdn.com/b/doriancorompt/archive/2012/02/05/4-1st-class-functions-power-is-in-simplicity.aspx#comments</comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p align="left"&gt;One of the most interesting article I read about programming languages has been published by the author of Chibi, a small scheme implementation (&lt;a title="http://synthcode.com/blog/2009/06/Small_is_Beautiful" href="http://synthcode.com/blog/2009/06/Small_is_Beautiful"&gt;http://synthcode.com/blog/2009/06/Small_is_Beautiful&lt;/a&gt;). The latter tries to defend the idea that beauty and power of programming languages is achieved through simplicity.&lt;/p&gt;  &lt;p align="left"&gt;While people understand that hiding complexity behind abstractions is a fundamental concept helping mastering problem complexity, they tend to think that adding syntax, and features to programming languages makes them more powerful. I think it is wrong. Programming languages are before of all languages, what matters is their expressivity, not how much keywords they have. Some simple concepts can be really powerful and add a real value while others are insignificant and just add complexity.&lt;/p&gt;  &lt;p align="left"&gt;The Revised 5th Report on Scheme (R5RS) even states:&lt;/p&gt;  &lt;table border="0" cellspacing="0" cellpadding="2" width="568"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="566"&gt;         &lt;p align="center"&gt;&lt;i&gt;“Programming languages should be designed not by piling feature on top of feature, &lt;/i&gt;&lt;i&gt;but by removing the weaknesses and restrictions that make additional features appear necessary.”&lt;/i&gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="left"&gt;The simplicity, power, and expressiveness of functional programming languages come from their core: functions.&lt;/p&gt;  &lt;p align="left"&gt;Although they may look similar they are not the functions you have in imperative languages. They are lambdas (from the mathematical field of lambda calculus). As imperative functions they allow create a new scope and allow code reuse, however what makes lambdas so special is that:&lt;/p&gt;  &lt;p align="left"&gt;&amp;#160;&lt;/p&gt;  &lt;p align="left"&gt;&lt;strong&gt;1- They are 1st class values which means they can be bound, passed as arguments or return values from other functions like any other values :&lt;/strong&gt;&lt;/p&gt;  &lt;p align="left"&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7360.1st_5F00_class_5F00_fun_5F00_70114DFC.png"&gt;&lt;img 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="1st_class_fun" border="0" alt="1st_class_fun" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/4721.1st_5F00_class_5F00_fun_5F00_thumb_5F00_726ACFFA.png" width="500" height="314" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p align="left"&gt;&amp;#160;&lt;/p&gt;  &lt;p align="left"&gt;&lt;strong&gt;2- They have the interesting property of capturing a part of their environment of definition (we also call them closures) :&lt;/strong&gt;&lt;/p&gt;  &lt;p align="left"&gt;Let’s have a closer look at the definition of the &lt;em&gt;makeAdder&lt;/em&gt; function:&lt;/p&gt;  &lt;p align="left"&gt;&lt;strong&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/5710.closure_5F00_5EDD7059.png"&gt;&lt;img 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="closure" border="0" alt="closure" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7457.closure_5F00_thumb_5F00_0FFCAAFA.png" width="131" height="68" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="left"&gt;Look at the&lt;em&gt; x&lt;/em&gt; parameter, this parameter is used in the body of the addX function without being declared in its definition parameter list! What happens here is that the addX function definition captured this parameter from its parent scope, we says that x is a free variable in the context of addX. This lambda property may appear useless at the first sight but actually it is the more powerful!&lt;/p&gt;  &lt;p align="left"&gt;Thanks to the fact that lambda functions capture data, we don’t need data structures anymore! Functional programming languages make the gap between code and data disappear. If you know OOP, you know that the fundamental idea behind it, is to put data with code and encapsulate it, it is exactly what lambdas provide.&amp;#160; &lt;/p&gt;  &lt;p align="left"&gt;F# provides a complete object layer, but let me show you how you could build OOP objects just by using the concept of lambdas:&lt;/p&gt;  &lt;p align="left"&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/6862.lambda_5F00_oop_5F00_4F5A5E8A.png"&gt;&lt;img 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="lambda_oop" border="0" alt="lambda_oop" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/2642.lambda_5F00_oop_5F00_thumb_5F00_74E3DBEB.png" width="507" height="213" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p align="left"&gt;&lt;em&gt;makePerson &lt;/em&gt;is a constructor for person objects with the &lt;em&gt;getFirstName getLastName&lt;/em&gt; and &lt;em&gt;getFullName&lt;/em&gt; methods. &lt;em&gt;firstName and lastName&lt;/em&gt; are free variables in the context of their respective getter methods. I used the unit operator () as an argument in the let expressions in order to ensure that&amp;#160; I am binding functions and not just values, indeed &lt;em&gt;let getFirstName = firstName&lt;/em&gt; would have bound the value &lt;em&gt;firstName&lt;/em&gt; to the symbol &lt;em&gt;getFirstName&lt;/em&gt; not built a function.&lt;/p&gt;  &lt;p align="left"&gt;We can try to play with this code in the interactive console:&lt;/p&gt;  &lt;p align="left"&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/3225.lambda_5F00_oop_5F00_ic_5F00_01718908.png"&gt;&lt;img 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="lambda_oop_ic" border="0" alt="lambda_oop_ic" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0160.lambda_5F00_oop_5F00_ic_5F00_thumb_5F00_074C2CA1.png" width="278" height="170" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p align="left"&gt;The &lt;em&gt;p&lt;/em&gt; object represents a person, we can use method on it like this: &lt;em&gt;(p “methodName”)()&amp;#160; &lt;/em&gt;if the method doesn’t exist we get a failure exception. This object model is simple and well suited for non dynamically typed languages like LISP or even JavaScript, however it is heavy to use in statically typed languages such as F# or Haskell. Indeed, if you try to add a method with a type different than &lt;em&gt;unit –&amp;gt; string&lt;/em&gt; it won’t work (though you could use list of objects for arguments…).&lt;/p&gt;  &lt;p align="left"&gt;&amp;#160;&lt;/p&gt;  &lt;p align="left"&gt;&lt;strong&gt;3- They can be partially applied (in F# and Haskell for example) and composed :&lt;/strong&gt;&lt;/p&gt;  &lt;p align="left"&gt;Lambda functions are defined in mathematics as single input, single output functions, they have a type of ‘a –&amp;gt; ‘b with ‘a and ‘b generic types. However we can use several lambdas together to model a multiple inputs function from unary functions.&lt;/p&gt;  &lt;p align="left"&gt;For example if we look at the type of the &lt;em&gt;List.map&lt;/em&gt; function in the interpreter we get: &lt;/p&gt;  &lt;p align="left"&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/3113.map_5F00_type_5F00_4903622F.png"&gt;&lt;img 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="map_type" border="0" alt="map_type" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/2211.map_5F00_type_5F00_thumb_5F00_482AFC45.png" width="425" height="45" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p align="left"&gt;Each time you see the &lt;em&gt;(–&amp;gt;) &lt;/em&gt;operator it means you are dealing with an unary function whose input is on the left and output on the right of this operator.&lt;/p&gt;  &lt;p align="left"&gt;The type ((‘a –&amp;gt; ‘b) –&amp;gt; ‘a list –&amp;gt; b’ list) tells us that &lt;em&gt;List.map&lt;/em&gt; is an&lt;strong&gt; unary&lt;/strong&gt; function which takes as first parameter an &lt;strong&gt;unary&lt;/strong&gt; function of type&lt;em&gt; (‘a –&amp;gt; b)&lt;/em&gt; and returns an &lt;strong&gt;unary&lt;/strong&gt; function &lt;em&gt;list&lt;/em&gt; of &lt;em&gt;‘b&lt;/em&gt; and then returns a &lt;em&gt;list&lt;/em&gt; of &lt;em&gt;‘b&lt;/em&gt;. When no parenthesis are present in the type, unary functions are considered&amp;#160; as forming a multiple inputs function, the right most type being the type of the output. So List.map is a function taking an unary function of type&lt;em&gt; (‘a –&amp;gt; ‘b)&lt;/em&gt; as &lt;strong&gt;first&lt;/strong&gt; parameter and a&lt;em&gt; list&lt;/em&gt; of&lt;em&gt; ‘a&lt;/em&gt; as &lt;strong&gt;second&lt;/strong&gt; parameter and returning a&lt;em&gt; list&lt;/em&gt; of type &lt;em&gt;‘b&lt;/em&gt;.&lt;/p&gt;  &lt;p align="left"&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7418.unary_5F00_decomposition_5F00_753FE913.png"&gt;&lt;img 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="unary_decomposition" border="0" alt="unary_decomposition" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/2605.unary_5F00_decomposition_5F00_thumb_5F00_66290A39.png" width="533" height="101" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p align="left"&gt;By the way it is interesting to notice that just by reasoning about the type you can discover what a function does, in the case of map you have a&amp;#160; function from &lt;em&gt;‘a&lt;/em&gt; to &lt;em&gt;‘b&lt;/em&gt; and a list of &lt;em&gt;‘a&lt;/em&gt; as input and a list of &lt;em&gt;‘b&lt;/em&gt; as output, you can guess that the unary function is applied to each element of the list, and the result collected in the output list.&lt;/p&gt;  &lt;p align="left"&gt;Ok now that I tell you that, you may think that it is rather complicated for nothing. Except the fact it fits the mathematical model, from a practical programmer point of view what are the benefits of decomposing multiple parameters functions into unary functions? &lt;/p&gt;  &lt;p align="left"&gt;It allows you to do something powerful called partial application (left currying), which consists in creating new specialized functions from older ones by omitting parameters:&lt;/p&gt;  &lt;p align="left"&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0552.partial_5F00_application_5F00_6C03ADD2.png"&gt;&lt;img 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="partial_application" border="0" alt="partial_application" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/4846.partial_5F00_application_5F00_thumb_5F00_58764E31.png" width="723" height="171" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p align="left"&gt;An other interesting feature is composition, function composition and partial application allow you to write really concise code. To compose functions from right to left (the mathematical way) use the &lt;em&gt;(&amp;lt;&amp;lt;)&lt;/em&gt; operator function, to compose functions from left to right (can be more natural) use the &lt;em&gt;(&amp;gt;&amp;gt;)&lt;/em&gt; operator function.&lt;/p&gt;  &lt;p align="left"&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0143.composition_5F00_16FB9BD8.png"&gt;&lt;img 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="composition" border="0" alt="composition" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/3683.composition_5F00_thumb_5F00_35D20FB6.png" width="699" height="112" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p align="left"&gt;Focus on the processData function definition, can you guess what this short line of code does?&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;     &lt;div align="left"&gt;It reads data from a file&lt;/div&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;div align="left"&gt;It tokenizes the text of the file using the ‘:’ separator&lt;/div&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;div align="left"&gt;It converts textual numbers to integers&lt;/div&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;div align="left"&gt;It multiplies them by 2&lt;/div&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;div align="left"&gt;Removes those greater than 30&lt;/div&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;div align="left"&gt;Returns the list of numbers&lt;/div&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p align="left"&gt;Do you see how short this function definition in term of code is! That’s a part of the magic coming with functional programming. Composition works with function, in case you would like you start expression to be your value, you can use the pipe operator we saw previously:&lt;/p&gt;  &lt;p align="left"&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/6087.pipeline_5F00_71A0D634.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="pipeline" border="0" alt="pipeline" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1682.pipeline_5F00_thumb_5F00_1BA0D45D.png" width="594" height="19" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p align="left"&gt;Here, we call &lt;em&gt;readData&lt;/em&gt; at the beginning of the pipeline, so we are chaining values. Of course the result is the same.&lt;/p&gt;  &lt;p align="left"&gt;&amp;#160;&lt;/p&gt;  &lt;p align="left"&gt;&lt;strong&gt;4- They are anonymous, and can be defined when needed.&lt;/strong&gt;&lt;/p&gt;  &lt;p align="left"&gt;Lambdas are anonymous values, if they have a name it is only because you bound them to this name. if you want to declare an anonymous lambda you can use the &lt;em&gt;fun&lt;/em&gt; keywords. The advantage of anonymous function is that you can create and use them immediately when you need it (see more about it in my article on Sequences and infinite streams).&lt;/p&gt;  &lt;p align="left"&gt;Below an example showing three ways of defining the same function:&lt;/p&gt;  &lt;p align="left"&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/4263.anonymous_5F00_7E27446E.png"&gt;&lt;img 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="anonymous" border="0" alt="anonymous" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7801.anonymous_5F00_thumb_5F00_435F9B98.png" width="379" height="136" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p align="left"&gt;You can also use the &lt;em&gt;function&lt;/em&gt; keyword to build an anonymous unary function which applies pattern matching on its parameter (see my article on pattern matching for more information).&lt;/p&gt;  &lt;p align="left"&gt;&amp;#160;&lt;/p&gt;  &lt;p align="left"&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="left"&gt;&lt;strong&gt;5- They are optimized for tail recursion in a lot of functional programming languages implementations including F#.&lt;/strong&gt;&lt;/p&gt;  &lt;p align="left"&gt;If you read my article on Recursion you probably understand how important recursion is in functional programming languages. Since recursion is omnipresent it better has to be optimized. This is the case in F#, the compiler translates tail recursion into loops, avoiding to pileup useless context information on the stack. So well written functional recursive code runs as fast as iterative imperative code without exhausting the stack memory. &lt;/p&gt;  &lt;p align="left"&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="left"&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="left"&gt;&lt;strong&gt;Pure functions&lt;/strong&gt;&lt;/p&gt;  &lt;p align="left"&gt;If your functions&amp;#160; are pure (aka they don’t have side effects), you can reason following a simple substitution model: you can substitute a function application with its body if you replace parameters correctly with the arguments of the application. If you do that you can easily understand how functions act. It is a model all programmers use naturally, however this substitution model is&lt;strong&gt; only true when there is no side effect. &lt;/strong&gt;It is one of the main cause of bugs in imperative code which encourages side effects, since you have to remember all the side effects&amp;#160; in your code and how they cascade&amp;#160; to reason about it!&lt;/p&gt;  &lt;p align="left"&gt;&amp;#160;&lt;/p&gt;  &lt;p align="left"&gt;&lt;strong&gt;Catamorphism and anamorphism&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Don’t worry I am not cursing you with these two words. Do you remember I told you that &lt;em&gt;map &lt;/em&gt;and&lt;em&gt; filter&lt;/em&gt; are two very important functions to work with lists (and sequences in general). Actually there are functions more powerful than them.&lt;/p&gt;  &lt;p&gt;Look at these definitions:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0550.foldables_5F00_0FB72F3A.png"&gt;&lt;img 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="foldables" border="0" alt="foldables" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0363.foldables_5F00_thumb_5F00_601910AD.png" width="287" height="403" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Do you see what they have in commons?&lt;/p&gt;  &lt;p&gt;They browse lists from start to end while performing an action on the &lt;em&gt;car&lt;/em&gt; and the &lt;em&gt;cdr&lt;/em&gt;. This is a pattern, and in programming we like to capture and abstract patterns to reuse them. The function under this pattern is fold which types is &lt;em&gt;(('a -&amp;gt; 'b -&amp;gt; 'a) -&amp;gt; 'a -&amp;gt; 'b list -&amp;gt; 'a).&lt;/em&gt; This type tells us that fold takes a function with an arity of two whose parameters are of types&lt;em&gt; ‘a&lt;/em&gt; and&lt;em&gt; ‘b&lt;/em&gt; and which returns an&lt;em&gt; ‘a&lt;/em&gt; as well as an &lt;em&gt;‘a&lt;/em&gt; and a list of&lt;em&gt; ‘b&lt;/em&gt; and then returns an&lt;em&gt; ‘a&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;This is how this functions works, we give it an action as first argument, this action is applied on an accumulator value and the first element of the list , we also provides fold a starting value for the accumulator and finally the list we want to fold.&lt;/p&gt;  &lt;p&gt;This is how fold is implemented:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/4251.foldl_5F00_1374D40A.png"&gt;&lt;img 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="foldl" border="0" alt="foldl" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/5241.foldl_5F00_thumb_5F00_2B983E65.png" width="276" height="88" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;This how fold is used:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7317.foldl_5F00_samples_5F00_2ABFD87B.png"&gt;&lt;img 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="foldl_samples" border="0" alt="foldl_samples" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1440.foldl_5F00_samples_5F00_thumb_5F00_746EB06B.png" width="493" height="216" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The 21 lines of code you wrote earlier are now 9 lines of code and you got two new functions for free! that the power of fold and partial application!&lt;em&gt; xcons &lt;/em&gt;is a flipped version of the &lt;em&gt;cons&lt;/em&gt; operator and&lt;em&gt; reverse&lt;/em&gt; a function to reverse a list. &lt;/p&gt;  &lt;p&gt;Ok you could say, I specific examples on purpose, so let’s add new functions you would code in&amp;#160; about 10 lines of code in imperative languages that you can now write in one:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/8877.max_5F00_min_5F00_47E58085.png"&gt;&lt;img 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="max_min" border="0" alt="max_min" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/5734.max_5F00_min_5F00_thumb_5F00_11945876.png" width="589" height="186" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;In these examples we fold from left to right so when we build list results, the list are reversed and we need to reverse them back. If you want to fold a list from right to left you can use another function traditionally called fold right or (foldBack in F#):&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1031.foldr_5F00_371DD5D7.png"&gt;&lt;img 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="foldr" border="0" alt="foldr" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1856.foldr_5F00_thumb_5F00_6AE5CC28.png" width="502" height="150" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;So what are the differences between fold left (fold) and fold right (foldBack), which one should I choose?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;As you can see from its definition fold left is a tail recursive function, so you are sure that it will be optimized and that it will not make your stack overflow, however when working on lists fold left generates the elements in reverse, forcing you to reverse them back if you want to keep the ordering. Fold right is not tail recursive, however it generates elements in the right order when building lists, so using fold right for small lists and fold left for medium or long list is a good compromise, if you don’t know opt for the left version. As you will see in my article on Sequences, choosing fold right is meaningful when working on infinite lists.&lt;/p&gt;  &lt;p&gt;Guess what? &lt;em&gt;fold &lt;/em&gt;and &lt;em&gt;foldBack&lt;/em&gt; are catamorphisms! You don’t need to re-implement these functions since they are available in the List module.&amp;#160; What about anamorphisms? Anamorphisms represent the opposite process, we are now talking about unfolding not folding. The unfold function helps you build a list from a base element, and a generator function applied until a predicate is satisfied.&lt;/p&gt;  &lt;p&gt;Here the unfold implementation:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/8461.unfold_5F00_5BCEED4E.png"&gt;&lt;img 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="unfold" border="0" alt="unfold" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/3582.unfold_5F00_thumb_5F00_0230D09A.png" width="486" height="18" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;This function is really powerful when you want to build list, you can for example extract an entire stream of values from a file into a list in one line of code! Below two example functions:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0844.unfold_5F00_samples_5F00_2892B3E5.png"&gt;&lt;img 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="unfold_samples" border="0" alt="unfold_samples" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/4274.unfold_5F00_samples_5F00_thumb_5F00_3277A550.png" width="519" height="170" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Not completely the end…&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;All these possible features in a simple function concept, and there would be a lot more to say!&amp;#160; I hope that thanks to this article you have started to understand how expressive and powerful functions are in functional programming languages. And with these functions, as a consequence to be concise, highly reusable and modular, statically typed, without side effect, your code is likely to be bug free!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10264168" width="1" height="1"&gt;</description></item><item><title>3. Pattern Matching – Be Explicit</title><link>http://blogs.msdn.com/b/doriancorompt/archive/2012/01/26/3-pattern-matching-be-explicit.aspx</link><pubDate>Thu, 26 Jan 2012 18:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10260943</guid><dc:creator>DorianCorompt</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/doriancorompt/rsscomments.aspx?WeblogPostID=10260943</wfw:commentRss><comments>http://blogs.msdn.com/b/doriancorompt/archive/2012/01/26/3-pattern-matching-be-explicit.aspx#comments</comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Pattern matching is a way of writing conditional code by mainly decomposing data into subcomponents. It is a high level construct allowing programmers to think naturally about their algorithms rather than in programming terms. Under the hood pattern matching expressions just are empowered if/switch expressions, so don’t worry to use them, they are efficient.&lt;/p&gt;  &lt;p&gt;In F# you can do pattern matching on the terms before the&lt;em&gt; (=)&lt;/em&gt; operator in a &lt;em&gt;let &lt;/em&gt;expression, using the &lt;em&gt;function&lt;/em&gt; syntax or the &lt;em&gt;match with&lt;/em&gt; syntax. You can decompose most data structures which have a literal representation in the code such as lists, tuples (kind of array containing values of different types), numbers, strings, booleans etc.…&lt;/p&gt;  &lt;p&gt;So you can do:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/8154.pattern_5F00_matching_5F00_6BFE9997.png"&gt;&lt;img 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="pattern_matching" border="0" alt="pattern_matching" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/3252.pattern_5F00_matching_5F00_thumb_5F00_4A9EF3FB.png" width="237" height="252" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Let see an examples for each of these constructs:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7282.pm_5F00_tuple1_5F00_472A568F.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="pm_tuple1" border="0" alt="pm_tuple1" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/5557.pm_5F00_tuple1_5F00_thumb_5F00_64BC318E.png" width="384" height="19" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Here the pattern is &lt;em&gt;firstName, lastName, age,&lt;/em&gt; which matches a tuple of three values, indeed the comma operator &lt;em&gt;(,)&lt;/em&gt; helps to build tuples. Tuples are defined by their size, and type, which is an union of other types. On the right side of the &lt;em&gt;(=) &lt;/em&gt;operator, an other tuple &lt;em&gt;“Dorian”, “Corompt”, 23 &lt;/em&gt;of size 3 and of type &lt;em&gt;string * string * int&lt;/em&gt; , the (*) operator indicating an union of types. Thanks to pattern matching we decompose this tuples, giving the value &lt;em&gt;“Dorian”&lt;/em&gt; to&lt;em&gt; firstName&lt;/em&gt;, &lt;em&gt;“Corompt”&lt;/em&gt; to &lt;em&gt;lastName&lt;/em&gt; and &lt;em&gt;23&lt;/em&gt; to &lt;em&gt;age&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;You could also have written:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0842.pm_5F00_tuple2_5F00_63E3CBA4.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="pm_tuple2" border="0" alt="pm_tuple2" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/3580.pm_5F00_tuple2_5F00_thumb_5F00_77FCE82D.png" width="266" height="37" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;You can interpret even simple value binding as pattern matching, making it a more general concept. In the example above &lt;em&gt;person&lt;/em&gt; is bound to a tuple, you can think &lt;em&gt;person&lt;/em&gt; as a named pattern to match everything, and in this precise case it is a tuple.&lt;/p&gt;  &lt;p&gt;In the previous example we named the subcomponents in the pattern to get their values, but in some case you don’t need all subcomponents, in these case you can use the joker/wildcard pattern (&lt;em&gt;_)&lt;/em&gt; to mach the values that should be ignored.&lt;/p&gt;  &lt;p&gt;Let’s say we want to represent a 3D point with a tuple of type &lt;em&gt;float * float * float&lt;/em&gt; we need functions to access each coordinates of this structure:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/4403.pm_5F00_func_5F00_52867090.png"&gt;&lt;img 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="pm_func" border="0" alt="pm_func" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1680.pm_5F00_func_5F00_thumb_5F00_63F6D168.png" width="166" height="57" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;In functional languages functions are standard values (see my article on 1st Class Functions), so you can define them with &lt;em&gt;let &lt;/em&gt;since &lt;em&gt;let&lt;/em&gt; helps to bind names to values.The syntax is &lt;em&gt;let functionName argument1 argument2 … = body. &lt;/em&gt;In our example each functions take one argument (a tuple of three values), we used the &lt;em&gt;(_)&lt;/em&gt; joker pattern to represent coordinates we don’t need.&lt;/p&gt;  &lt;p&gt;In the interactive console we can try the functions we just defined:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0272.pm_5F00_func_5F00_res_5F00_235484F9.png"&gt;&lt;img 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="pm_func_res" border="0" alt="pm_func_res" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/3326.pm_5F00_func_5F00_res_5F00_thumb_5F00_571C7B4A.png" width="383" height="158" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;It works as expected.&lt;/p&gt;  &lt;p&gt;You already saw a function using pattern matching on numbers:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/2742.fact_5F00_fs_5F00_rec_5F00_6E677FBB.png"&gt;&lt;img 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="fact_fs_rec" border="0" alt="fact_fs_rec" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/5852.fact_5F00_fs_5F00_rec_5F00_thumb_5F00_14C96307.png" width="193" height="58" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The&lt;em&gt; function&lt;/em&gt; construct returns as value a function taking a single argument on which pattern matching is applied, even if you don’t see it, this argument exists. The pipe &lt;em&gt;(|)&lt;/em&gt; allows to define possible branches for the match, the branches patterns are tested sequentially from top to bottom. In the &lt;em&gt;fact&lt;/em&gt; definition the first branch matches the pattern 0 which is the same constant value and attributes an expression value to it, 1. The second branch matches everything (with some constraints on the type which is inferred by the compiler) and name the value matched n so it can be used in the expressions associated to this match which is &lt;em&gt;n * fact(n – 1).&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;The &lt;em&gt;match with&lt;/em&gt; construct allows to apply pattern matching on a specific expression. Actually the previous fact definition is equivalent to the following:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7522.pm_5F00_fact_5F00_match_5F00_3B2B4652.png"&gt;&lt;img 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="pm_fact_match" border="0" alt="pm_fact_match" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0160.pm_5F00_fact_5F00_match_5F00_thumb_5F00_21571023.png" width="188" height="67" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The &lt;em&gt;function construct&lt;/em&gt; is therefore just a syntax sugar to allow concise definitions of particular pattern matching functions.&lt;/p&gt;  &lt;p&gt;I told you that a named pattern of the form &lt;em&gt;n&lt;/em&gt; (aka only a name) matches everything, so you may have questions:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Does it mean that if I call &lt;em&gt;fact “test”&lt;/em&gt; this will call factorial on a string, matching it with the &lt;em&gt;n&lt;/em&gt; pattern?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;No, in F# everything is typed, in the first branch you specify &lt;em&gt;0 –&amp;gt; 1,&lt;/em&gt; the compiler infers that your function returns an int and that it takes an &lt;em&gt;int&lt;/em&gt; so that&lt;em&gt; x&lt;/em&gt; should be an &lt;em&gt;int&lt;/em&gt;. The second branch has to confirm this hypothesis. So the compiler looks at &lt;em&gt;n –&amp;gt; n * fact(n - 1),&lt;/em&gt; it looks first at the result &lt;em&gt;n * fact(n - 1)&lt;/em&gt; , &lt;em&gt;n&lt;/em&gt; is of unknown type, fact is of type: &lt;em&gt;int –&amp;gt; int&lt;/em&gt; and the &lt;em&gt;(*) &lt;/em&gt;operator is of type &lt;em&gt;int –&amp;gt; int&lt;/em&gt; so the whole expression should have type &lt;em&gt;int -&amp;gt;int&lt;/em&gt; meaning that &lt;em&gt;n&lt;/em&gt; and &lt;em&gt;x&lt;/em&gt; are of type &lt;em&gt;int&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;What if I give a negative integer to this fact definition?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;This function will make your program crash. Indeed, take &lt;em&gt;fact –1&lt;/em&gt; one for example, &lt;em&gt;-1&lt;/em&gt; it will be matched against n and lead to &lt;em&gt;n * fact(n - 1),&lt;/em&gt; it continues recursion with&lt;em&gt; fact –2; fact –3,… x&lt;/em&gt; being negative will never be matched against 0, so this function will loop indefinitely and because&amp;#160; this definition is not recursive it will push its context on the stack on each loop iteration, leading to a stack overflow. Of course there is a solution to this problem, we can use condition (or guards) on pattern to put constraints on them. Pattern conditions are specified through the use of the &lt;em&gt;when&lt;/em&gt; keyword:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/3005.pm_5F00_fact_5F00_guard1_5F00_207EAA39.png"&gt;&lt;img 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="pm_fact_guard1" border="0" alt="pm_fact_guard1" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7510.pm_5F00_fact_5F00_guard1_5F00_thumb_5F00_38A21494.png" width="361" height="80" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;After adding this pattern condition you will get a warning which tells you that your pattern matching is incomplete. What does it mean? It simply means that your matching expression doesn’t take into account all input it could be given. Indeed if you call now fact –1, where does it branch to? If you don’t define an additional pattern it will leads to a match failure runtime exception.&lt;/p&gt;  &lt;p&gt;Let’s just add a third branch to correct this and encompass all possible values:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/8015.pm_5F00_fact_5F00_guard2_5F00_6235DFC7.png"&gt;&lt;img 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="pm_fact_guard2" border="0" alt="pm_fact_guard2" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0576.pm_5F00_fact_5F00_guard2_5F00_thumb_5F00_0EDE99A1.png" width="463" height="68" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The third branch uses the &lt;em&gt;(_) &lt;/em&gt;joker pattern to match everything else (negative integers) and throw a failure exception with the given message thanks to the failwith function.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;What if while decomposing list of the form h :: t, I want to keep a reference of the entire list not only the head and the tail?&lt;/strong&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;You can use the as keyword to name pattern components for example&amp;#160; &lt;em&gt;(h :: t) as ls –&amp;gt; ls&lt;/em&gt; matches a non empty list (it has a head and a tail)&amp;#160; and returns its value.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Real life functions&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;When I speak about functional programming languages with imperative programmer colleagues, they tend to consider&amp;#160; them as unusable.&lt;/p&gt;  &lt;p&gt;I always hear &lt;em&gt;“They are toys good for maths, experiments, but not real life applications…”&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;When I hear these words, it makes me smile. The basis of OOP concept, as well as important programming technics such as garbage collection, exception handling, generics, software threads, closures, predicates, sequences, most list manipulation functions or technologies (LINQ for example) they are proud to use in their imperative languages come from functional programming languages…&lt;/p&gt;  &lt;p&gt;I recently noticed that after closures, and LINQ the next “big” feature coming to C# is asynchronous operations handling through the use of the &lt;em&gt;await&lt;/em&gt; keyword, a new concept heavily used in Windows 8 Metro Style UI programming. As you will see this feature is just a particular application of F# workflows (Haskell Monads) that I will introduce in a next article.&lt;/p&gt;  &lt;p&gt;They try to interpret and criticize functional programming from their imperative programming knowledge. As you cannot judge a culture you don’t know or understand you cannot judge a paradigm you never tried. Be open.&lt;/p&gt;  &lt;p&gt;I am going to show you how are implemented some “real life” functions that are the pillars of most functional programming languages. And explain you how these functions relates to the .NET LINQ technology.&lt;/p&gt;  &lt;p&gt;Let’s say you have a list employees aliases, you want to get their list of emails, where each email is of the form &lt;a href="mailto:alias@contoso.com"&gt;alias@contoso.com&lt;/a&gt;. First, what you need&amp;#160; is a function taking an alias and returning an email, then a way of mapping this function to each alias in the list.&lt;/p&gt;  &lt;p&gt;C# (without LINQ):&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/8171.map_5F00_sample_5F00_cs_5F00_1F76948F.png"&gt;&lt;img 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="map_sample_cs" border="0" alt="map_sample_cs" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/8255.map_5F00_sample_5F00_cs_5F00_thumb_5F00_72ED64A8.png" width="683" height="336" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;And F#:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0245.map_5F00_sample_5F00_fs_5F00_3BC3D6AF.png"&gt;&lt;img 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="map_sample_fs" border="0" alt="map_sample_fs" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1881.map_5F00_sample_5F00_fs_5F00_thumb_5F00_68D8C37D.png" width="484" height="87" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Thinking imperatively you would try to decompose the second step in something iterative by thinking at the computer level: “I need to iterate through the list, call the function to create the email from the current iteration alias and then adding to a second list”. With a functional programming you think at a high level, you just do what you said!&lt;/p&gt;  &lt;p&gt;The&lt;em&gt; map&lt;/em&gt; function represents the pattern of applying a function in a context (a list in our case) and returning a new context containing the result. Its non tail recursive implementation for lists is trivial using pattern matching:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/8156.map_5F00_nontailrec_5F00_6727F7A9.png"&gt;&lt;img 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="map_nontailrec" border="0" alt="map_nontailrec" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/5187.map_5F00_nontailrec_5F00_thumb_5F00_38623F07.png" width="217" height="75" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Now that you have this email list, let say you want to sort it alphabetically. You would like to sort it efficiently, looking for a solution you found the quicksort algorithm on Wikipedia (you could use the List.sort function, the purpose is to teach you how to think functionally):&lt;/p&gt;  &lt;h4&gt;Algorithm&lt;/h4&gt;  &lt;p&gt;Quicksort is a &lt;a href="http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm"&gt;divide and conquer algorithm&lt;/a&gt;. Quicksort first divides a large &lt;a href="http://en.wikipedia.org/wiki/List_(computing)"&gt;list&lt;/a&gt; into two smaller sub-lists: the low elements and the high elements. Quicksort can then recursively sort the sub-lists.&lt;/p&gt;  &lt;p&gt;The steps are:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Pick an element, called a &lt;a href="http://en.wikipedia.org/wiki/Pivot_element"&gt;&lt;i&gt;pivot&lt;/i&gt;&lt;/a&gt;, from the list. &lt;/li&gt;    &lt;li&gt;Reorder the list so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the &lt;b&gt;partition&lt;/b&gt; operation. &lt;/li&gt;    &lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Recursion_(computer_science)"&gt;Recursively&lt;/a&gt; sort the sub-list of lesser elements and the sub-list of greater elements. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;The &lt;a href="http://en.wikipedia.org/wiki/Base_case#Recursive_programming"&gt;base case&lt;/a&gt; of the recursion are lists of size zero or one, which never need to be sorted.&lt;/p&gt;  &lt;p&gt;So we need to choose an element as a pivot in the list, let’s take the first element of the list. Then the result is a recursion on the elements of the list smaller than the pivot, the pivot, and a recursion on the element greater than the pivot.&lt;/p&gt;  &lt;p&gt;Here a simple and concise implementation in F#:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0702.qsort_5F00_4C7B5B90.png"&gt;&lt;img 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="qsort" border="0" alt="qsort" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1362.qsort_5F00_thumb_5F00_36B17333.png" width="393" height="103" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I did exactly what is stated above. I took the first element of the list&lt;em&gt; p&lt;/em&gt; as a pivot thanks to pattern matching, I applied &lt;em&gt;qsort&lt;/em&gt; recursively on a list composed of the elements smaller than &lt;em&gt;p&lt;/em&gt; thanks to the &lt;em&gt;List.filter&lt;/em&gt; function, and I concatenated the result thanks to the list concatenation operator &lt;em&gt;(@) &lt;/em&gt;with a list containing the pivot p and a list composed of the elements greater than p on which&lt;em&gt; qsort&lt;/em&gt; has been recursively applied.&lt;/p&gt;  &lt;p&gt;The &lt;em&gt;fun … –&amp;gt; … &lt;/em&gt;construct is a way of creating anonymous functions, learn more about closures with my article on 1st Class Functions.&lt;/p&gt;  &lt;p&gt;The &lt;em&gt;filter&lt;/em&gt; function represents the pattern of filtering elements in a sequence satisfying a condition, and returning a new sequence of retained elements. Its non tail recursive implementation for lists is trivial using pattern matching:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/2352.filter_5F00_nontailrec_5F00_7536C0D9.png"&gt;&lt;img 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="filter_nontailrec" border="0" alt="filter_nontailrec" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7142.filter_5F00_nontailrec_5F00_thumb_5F00_745E5AEF.png" width="291" height="87" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Functions like&lt;em&gt; f &lt;/em&gt;are called predicates, they represent conditions and return a boolean value. We use f in the guard to know if we need to keep the element in the resulting list, or switch to the next branch and keep filtering the remaining elements discarding the first element.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;map&lt;/em&gt; and &lt;em&gt;filter&lt;/em&gt; are amongst the most important functions in functional programming languages.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Do you know LINQ?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;In my first example I showed you how creating a list of emails from a list of aliases in F# was concise and clear compared to C#, but you could quite rightly argue that I didn’t use LINQ.&lt;/p&gt;  &lt;p&gt;For example if you want to create a list of emails with aliases that contains the ‘o’ letter and then sort it alphabetically in C# with LINQ you could do it easily:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/3823.linq_5F00_cs_5F00_2ED95AC4.png"&gt;&lt;img 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="linq_cs" border="0" alt="linq_cs" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/8304.linq_5F00_cs_5F00_thumb_5F00_2675856D.png" width="626" height="211" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I wrote earlier, that LINQ was coming from functional programming, do you see the link with the functions map, filter and qsort, I have shown you?&lt;/p&gt;  &lt;p&gt;Look:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0876.linq_5F00_fs_5F00_5D6F63A6.png"&gt;&lt;img 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="linq_fs" border="0" alt="linq_fs" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1351.linq_5F00_fs_5F00_thumb_5F00_4E5884CC.png" width="469" height="129" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The&lt;em&gt; Select&lt;/em&gt; method is just a &lt;em&gt;map&lt;/em&gt;, the &lt;em&gt;Where &lt;/em&gt;method a &lt;em&gt;filter&lt;/em&gt;, and &lt;em&gt;OrderBy &lt;/em&gt;method a sorting function!&lt;/p&gt;  &lt;p&gt;The&lt;em&gt; (|&amp;gt;)&lt;/em&gt; operator is called the pipe operator (same role as the Powershell or UNIX shell pipe), it allows you to chain expressions by calling the function on its right with its right most parameter equals to the result of the expression on the left. If you know Haskell, it is equivalent to a right currying of the function. You will learn more about it when talking about partial function application and composition in my article on 1st Class Function.&lt;/p&gt;  &lt;p&gt;It is not a coincidence if LINQ, inferred typing &lt;em&gt;(var)&lt;/em&gt; and closures (F# functions are closures) appeared together in C# 3.0. They all are functional concepts.&lt;/p&gt;  &lt;p&gt;So if some of your C# developer colleagues tell you how great is C# with LINQ compared to your toys, don’t tell them you could recode LINQ with your toys in 1 line of code, it could be vexing. &lt;img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7217.wlEmoticon_2D00_smile_5F00_417E2EAE.png" /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10260943" width="1" height="1"&gt;</description></item><item><title>2. List – Cons, Car, Cdr &amp; Co</title><link>http://blogs.msdn.com/b/doriancorompt/archive/2012/01/13/2-list-cons-car-cdr-amp-co.aspx</link><pubDate>Fri, 13 Jan 2012 10:33:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10256266</guid><dc:creator>DorianCorompt</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/doriancorompt/rsscomments.aspx?WeblogPostID=10256266</wfw:commentRss><comments>http://blogs.msdn.com/b/doriancorompt/archive/2012/01/13/2-list-cons-car-cdr-amp-co.aspx#comments</comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Lists are the most important data structures in functional programming languages. It is not for nothing that the first functional programming language, that is by the way the second oldest programming language (1958) is called LISP which stands for “LISt Processing languages”. LISP is all about working with lists to the point that even its source code is written with lists, so the border between data and code is blur, this strange property called homoconicity is really powerful! No more need for serialization, no need for remote procedure call systems, meta-programming is easy, creating new domain specific languages (DSL) becomes easy! To learn more about LISP (see my article about Macros).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;What are the terms “cons”, “car” and “cdr”?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;“cons” stands for “construct”, “car” for “Contents of the Address part of Register number” and “cdr” for “Contents of the Decrement part of Register number”. “car” and “cdr” were the names given to registers in the first LISP machines.&amp;#160; They are now the names of basic list operations available in LISP. “cons” is still in use in most functional languages to name this same operation.&lt;/p&gt;  &lt;p&gt;These operators roles are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The &lt;em&gt;cons&lt;/em&gt; function allocates a pair with two value references. &lt;/li&gt;    &lt;li&gt;The &lt;em&gt;car&lt;/em&gt; function takes a pair and return its first value. &lt;/li&gt;    &lt;li&gt;The &lt;em&gt;cdr&lt;/em&gt; function takes a pair and return its second value. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;What is the relationship between these operations and lists?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;If you think more carefully about it, I wrote that the &lt;em&gt;cons&lt;/em&gt; operation makes pairs from two values. So what if you build a first pair holding in its first cell (the car cell) a value and in its second cell (the cdr cell) an other pair which contains as its first cell a value and as its second cell a pair and so on…&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/2543.pair_5F00_list_5F00_437651DA.png"&gt;&lt;img 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="pair_list" border="0" alt="pair_list" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7485.pair_5F00_list_5F00_thumb_5F00_45AF883E.png" width="240" height="58" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;As the image above shows it, this would build a single linked list! However we need to know when the list ends. We could just say that if the last value is not a pair then it ends, but in this case it would be a somewhat non regular, and there would be a problem, how to represent an empty list? To solve this problem a special value exists, and surprisingly enough it is called “the empty list” ! So the image above represents a list of integers containing the values 42, 69, 613, the NIL at the end represents the empty list. If you read my article about recursion you may already have an idea about where this list definition is going to lead us.&lt;/p&gt;  &lt;p&gt;Time to do it in F#!&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1205.list_5F00_pairs_5F00_4A356D2B.png"&gt;&lt;img 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="list_pairs" border="0" alt="list_pairs" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7450.list_5F00_pairs_5F00_thumb_5F00_25742FE7.png" width="227" height="21" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;In F# the &lt;em&gt;cons&lt;/em&gt; operator is&amp;#160; &lt;em&gt;(::),&lt;/em&gt; it is an infix operator&lt;em&gt;. car&lt;/em&gt; and&lt;em&gt; cdr&lt;/em&gt; are respectively &lt;em&gt;head&lt;/em&gt; and &lt;em&gt;tail&lt;/em&gt; from the &lt;em&gt;List&lt;/em&gt; module, and finally the empty list is &lt;em&gt;[].&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;You can use the F# interpreter fsi.exe in interactive mode to see the result of the command you type directly, you need to use &lt;em&gt;;;&lt;/em&gt; to end a command within the interpreter. In Visual Studio pressing Ctrl + Alt + F brings the F# Interactive Console.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/2821.list_5F00_pairs_5F00_int_5F00_560FE8A6.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="list_pairs_int" border="0" alt="list_pairs_int" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/3513.list_5F00_pairs_5F00_int_5F00_thumb_5F00_42EEBBFA.png" width="260" height="133" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;You should notice that:&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;After executing a command the result is displayed. In functional programming languages there is always a result. Everything is expression. To be functional code should avoid side effects. If there is no side effects and a function doesn’t return a value, we can assume that from the outside this function when called does nothing, since it cannot modify the environment (side effects), and it can’t return a result. Functional languages are classified by the notion of purity, which is how much they prevent programmers to use side effects. Haskell is for example a really pure language, F# allows imperative programming so it is less pure, but the way your code in F# can be pure. In non-pure functional programming languages a special value is used to represent the result of a function which does nothing else than side effects, in F# this value is the &lt;em&gt;unit&lt;/em&gt; and is represented by&amp;#160; the &lt;em&gt;()&lt;/em&gt; literal.&amp;#160; &lt;br /&gt;      &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;When the result is displayed &lt;em&gt;val list&lt;/em&gt; indicates we were evaluating a value named “list”, the &lt;em&gt;(:)&lt;/em&gt; operator specifies the type which is &lt;em&gt;int list&lt;/em&gt; so a list of integer (this type could also have been written &lt;em&gt;list&amp;lt;int&amp;gt;)&lt;/em&gt;, finally the value is indicated after the &lt;em&gt;(=)&lt;/em&gt; operator. The list we built using&amp;#160; the &lt;em&gt;cons&lt;/em&gt; operation is now written in the result&amp;#160; as &lt;em&gt;[42; 69; 613],&lt;/em&gt; which the way you usually use to declare list literals in F#. So instead of using the &lt;em&gt;cons&lt;/em&gt; operation in our sample we could have directly used the latter syntax.       &lt;br /&gt;      &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;The &lt;em&gt;car &lt;/em&gt;or the &lt;em&gt;head &lt;/em&gt;of&lt;em&gt; list&lt;/em&gt; is &lt;em&gt;42, &lt;/em&gt;and the &lt;em&gt;cdr &lt;/em&gt;or &lt;em&gt;tail&lt;/em&gt; is&lt;em&gt; [69; 613]&lt;/em&gt; which is a list, since the list is made by pairs of pairs.       &lt;br /&gt;      &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;List types are parameterized, the generic type is &lt;em&gt;‘a list&lt;/em&gt; (or &lt;em&gt;list&amp;lt;’a&amp;gt;&lt;/em&gt;) with &lt;em&gt;‘a&lt;/em&gt; being a type parameter. Type parameters are prefixed with a single quote. Thanks to this you can create different list types, for example &lt;em&gt;int list, float list&lt;/em&gt; or &lt;em&gt;string list&lt;/em&gt;. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;What are the advantages of using single linked list?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Using the concept of pairs linked to each others for lists, makes them perfectly fit with the concept of recursion. Therefore you can easily create function working with lists in functional programming languages.&lt;/p&gt;  &lt;p&gt;Look at this F# definition for function to compute the length of list for example:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/4314.length_5F00_45EB49CC.png"&gt;&lt;img 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="length" border="0" alt="length" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/6557.length_5F00_thumb_5F00_3E5FDA5F.png" width="179" height="49" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;If you remember the definition of &lt;em&gt;fact &lt;/em&gt;in my previous article on recursion, you can describe this function as:&lt;em&gt; length&lt;/em&gt; is a recursive function doing pattern matching on its first argument, if this first argument is the empty list then the result is &lt;em&gt;0&lt;/em&gt;, if it is a pair whose head is &lt;em&gt;h&lt;/em&gt; and tail is&lt;em&gt; t&lt;/em&gt; then add &lt;em&gt;1&lt;/em&gt; to the length of the tail &lt;em&gt;t&lt;/em&gt; (remaining of the list).&lt;/p&gt;  &lt;p&gt;An other advantage of using pairs in functional programming is about side effects. If you want to make the list grows, you can just prepend a value to it. You use the &lt;em&gt;cons&lt;/em&gt; operation with the element you want to add to the list as a first value, and the old list value as a second value for the pair, then without side effect you make the list grow. In the same way if you want to remove a value from a list, just drop the pair holding the concerned value, and its previous pair from the list and create a new pair with the previous pair value as first argument and the next pair as second argument to recreate the link.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;How is this list related to the &lt;em&gt;List&lt;/em&gt; class from &lt;em&gt;System.Collections.Generic&lt;/em&gt;?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;F# lists are not related to this class, since they are implemented using pairs whereas the &lt;em&gt;List&lt;/em&gt; class from &lt;em&gt;System.Collections.Generic&lt;/em&gt; uses a resizable array internally. To avoid the confusion a type alias &lt;em&gt;ResizeArray&lt;/em&gt; has been created in F#. Accessing elements or appending elements in single linked list are not efficient operations since they have a complexity of O(n). In an array or array based list these operations are addressed in constant time. So in these cases you may consider to use ResizeArray or the F# arrays that I will introduce in my article on Sequences.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10256266" width="1" height="1"&gt;</description></item><item><title>1. Recursion - Where are my for/while loops?</title><link>http://blogs.msdn.com/b/doriancorompt/archive/2012/01/08/1-recursion-where-are-my-for-while-loops.aspx</link><pubDate>Sun, 08 Jan 2012 14:05:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10254362</guid><dc:creator>DorianCorompt</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/doriancorompt/rsscomments.aspx?WeblogPostID=10254362</wfw:commentRss><comments>http://blogs.msdn.com/b/doriancorompt/archive/2012/01/08/1-recursion-where-are-my-for-while-loops.aspx#comments</comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Most imperative programmers who start learning functional programming are lost because they don’t see loop constructs in code. Indeed even if some functional languages such as F# provide looping constructs they rarely or sometimes never use them. Why? Because actually they are not necessary, we can get ride of them thanks to a powerful concept: recursion. We say that a function is recursive when it calls itself within its body.&lt;/p&gt;  &lt;p&gt;Let’s have a look at an example using the famous factorial function defined by:&lt;/p&gt;  &lt;p&gt;   &lt;table border="0" cellspacing="0" cellpadding="2" width="402"&gt;&lt;tbody&gt;       &lt;tr&gt;         &lt;td valign="top" width="92"&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1376.fact_5F00_wiki_5F00_it_5F00_185CFE19.png"&gt;&lt;img 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="fact_wiki_it" border="0" alt="fact_wiki_it" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/1057.fact_5F00_wiki_5F00_it_5F00_thumb_5F00_509B7531.png" width="82" height="49" /&gt;&lt;/a&gt;&lt;/td&gt;          &lt;td valign="top" width="143"&gt;           &lt;p align="center"&gt;&amp;#160;&lt;/p&gt;            &lt;p align="center"&gt;or&lt;/p&gt;         &lt;/td&gt;          &lt;td valign="top" width="165"&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/2047.fact_5F00_wiki_5F00_rec_5F00_76FD587C.png"&gt;&lt;img 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="fact_wiki_rec" border="0" alt="fact_wiki_rec" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/3034.fact_5F00_wiki_5F00_rec_5F00_thumb_5F00_6133701F.png" width="240" height="57" /&gt;&lt;/a&gt;&lt;/td&gt;       &lt;/tr&gt;     &lt;/tbody&gt;&lt;/table&gt; &lt;/p&gt;  &lt;p&gt;The definition on the left is iterative, factorial &lt;em&gt;(!)&lt;/em&gt; is defined by a product. The definition on the right is recursive, &lt;em&gt;n! = (n x (n – 1)!)&lt;/em&gt; since the function factorial &lt;em&gt;(!)&lt;/em&gt; appears before and after the symbol &lt;em&gt;(=).&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;The C# code below shows how you could implement this function iteratively using a for loop construct:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/7266.image4_5F00_07374B45.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/8244.image4_5F00_thumb_5F00_1AE434D9.png" width="243" height="155" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;This function is using an accumulator to store the intermediate computation during each iteration. &lt;/p&gt;  &lt;p&gt;Let’s see how you could have implement this recursively in C#:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/4276.fact_5F00_cs_5F00_rec_5F00_2B6E04F8.png"&gt;&lt;img 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="fact_cs_rec" border="0" alt="fact_cs_rec" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/0447.fact_5F00_cs_5F00_rec_5F00_thumb_5F00_0A7A9251.png" width="229" height="118" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;We can notice that:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;We don’t need the for loop anymore. &lt;/li&gt;    &lt;li&gt;We don’t need side-effects (aka no assignment) &lt;/li&gt;    &lt;li&gt;The function body is slightly shorter. &lt;/li&gt;    &lt;li&gt;We don’t need an explicit accumulator variable to store the intermediate computation, instead the n parameter (allocated implicitly on the stack) is used to hold it. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;It seems that recursion is more clear and concise so why don’t we use it in C# instead of loops like in functional programming languages?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The answer is simple, developers should not assume that C# as most other imperative languages (C, C++, …) is optimized for recursion. When a function is called it allocates a block of memory called a frame on the stack to store its local variables and return address (if not stored in a register), nested function calls cause the frames to pile up on the stack. If too many nested calls occur, which is likely to happen with some kind of recursion, the stack gets out&amp;#160; of memory and the program crashes. Most functional programming languages implementations optimize the recursion process. One type of recursion can be easily optimized, it occurs when a function returns with its own call, we say that the function is tail recursive. An optimized tail recursion has the same performances than a looping construct.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;So what about non tail recursive calls? Isn’t it better to use a loop then?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;All non tail recursive calls can be converted to tail recursive calls either explicitly or implicitly (through the language implementation) by using a transformation known as “Continuation Passing Style” (CPS)&lt;/p&gt;  &lt;p&gt;So the first iterative factorial definition is better in the case of C#.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Below a way of implementing it recursively in F#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/5238.fact_5F00_fs_5F00_rec_5F00_5BB4D9AE.png"&gt;&lt;img 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="fact_fs_rec" border="0" alt="fact_fs_rec" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-50-18-metablogapi/3187.fact_5F00_fs_5F00_rec_5F00_thumb_5F00_0216BCFA.png" width="193" height="58" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Look how it is concise and clear! It looks like the mathematical definition! You will notice that this function is not tail recursive, indeed the last operation to happen in the second branch is not a direct call to &lt;em&gt;fact&lt;/em&gt;, but a call to the multiplication operator &lt;em&gt;(*)&lt;/em&gt; with arguments &lt;em&gt;n&lt;/em&gt; and &lt;em&gt;fact(n – 1).&lt;/em&gt; (see my article on 1st Class Functions to know how to make this function tail recursive)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;About the syntax:&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The &lt;em&gt;let &lt;/em&gt;keyword allows you to bind symbol/names to values. for example&lt;em&gt; let pi = 3.14&lt;/em&gt; will bind the value &lt;em&gt;3.14&lt;/em&gt; to the name&lt;em&gt; pi&lt;/em&gt;. Binding a value is different from assigning a value to a variable, since in functional languages you should not modify the value once it has been bound. Avoiding side effects is great, it protects us from a lot of bugs especially in a parallel environment. You don’t need to precise a type for the &lt;em&gt;pi&lt;/em&gt;, like the Haskell compiler, the F# compiler is smart enough to infer it, F# float type in this case, which corresponds to C# double type. Values defined by let are visible in the let body, which is here visible thanks to its indentation, we speak about lexical scope.       &lt;br /&gt;      &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;The &lt;em&gt;rec&lt;/em&gt; keyword specifies that this binding is recursive, so even if &lt;em&gt;fact&lt;/em&gt; is not defined yet it will be visible in its lexical scope.       &lt;br /&gt;      &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;The function keywords allows to create a function which takes a single argument and does pattern matching on it (see my article on pattern matching for more information). It says that:      &lt;ul&gt;       &lt;li&gt;if the value of the function is &lt;em&gt;0&lt;/em&gt; then the result should be &lt;em&gt;1&lt;/em&gt; &lt;/li&gt;        &lt;li&gt;else if the value of the function is &lt;em&gt;n&lt;/em&gt; then the result should be &lt;em&gt;n * fact(n - 1)&lt;/em&gt; &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;F# has a for and while construct, but you will rarely use them, since recursion should be preferred, however they are useful in some cases, when generating sequences for example (see my article on Sequences for more information)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10254362" width="1" height="1"&gt;</description></item><item><title>An Overview of Functional Programming</title><link>http://blogs.msdn.com/b/doriancorompt/archive/2012/01/08/an-overview-of-functional-programming.aspx</link><pubDate>Sun, 08 Jan 2012 13:19:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10254358</guid><dc:creator>DorianCorompt</dc:creator><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/doriancorompt/rsscomments.aspx?WeblogPostID=10254358</wfw:commentRss><comments>http://blogs.msdn.com/b/doriancorompt/archive/2012/01/08/an-overview-of-functional-programming.aspx#comments</comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Obscure syntax, missing looping constructs, unknown programming patterns, the lack of libraries or documentation, there are a lot of reasons discouraging imperative programmers to start learning and using functional programming languages. &lt;/p&gt;  &lt;p&gt;Microsoft released in 2002 F#, a multi-paradigm programming language, targeting the .NET Framework, that encompasses functional programming as well as imperative and object-oriented programming disciplines to help address these issues.&lt;/p&gt;  &lt;p&gt;F# is first class .NET language, it has a complete access to the .NET Framework, can consume and generate classes, can be either interpreted or compiled to .NET assemblies, and it is well integrated to the Visual Studio Microsoft IDE.&lt;/p&gt;  &lt;p&gt;Through a series of articles I would like to introduce some of the powerful F# features and features common to functional languages:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;strong&gt;Recursion -&amp;#160; Where are my for/while loops?&lt;/strong&gt;       &lt;br /&gt;Discussion about recursion and why loops are rarely used in functional programming.       &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;List – Cons, Car, Cdr &amp;amp; Co        &lt;br /&gt;&lt;/strong&gt;Showing the importance and the power of list processing in functional languages.       &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Pattern Matching – Be Explicit&lt;/strong&gt;       &lt;br /&gt;A great feature to write clear and concise code.       &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;1st Class Functions – Power is in simplicity&lt;/strong&gt;       &lt;br /&gt;What does it mean to have first class functions, partial application, how to build abstraction with functions.       &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Generic Parameters – Static typing without typing        &lt;br /&gt;&lt;/strong&gt;Presentation of generic parameters which will help you to make generalist and reusable algorithms.       &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Sequences – Being lazy is allowed&lt;/strong&gt;       &lt;br /&gt;Usage of laziness to create on demand computations, and infinite streams of data.       &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Workflows – A Monad alias &lt;/strong&gt;      &lt;br /&gt;Introduction to the concept of monads, how they can be built in F# and what the power they provide.&amp;#160; &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Quotations – Say “your name”&lt;/strong&gt;       &lt;br /&gt;An overview of meta-programming in F#.       &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Continuations – Future is yours&lt;/strong&gt;       &lt;br /&gt;Continuations allow non lexical escapes, it is then possible to get coroutines , exception handling, recursion optimizations and more.       &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Macros – Do you like parenthesis?&lt;/strong&gt;       &lt;br /&gt;This article will presents IronScheme, a Scheme implementation for the .NET, in order to speak about macros in functional programming &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Learning functional programming when coming from the imperative world is not something easy, it takes time, but I encourage all of you to try it, since this mind shift will not only help you become better programmers by mastering new concepts, but it will also bring you a lot of fun!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10254358" width="1" height="1"&gt;</description></item><item><title>First Entry !</title><link>http://blogs.msdn.com/b/doriancorompt/archive/2012/01/04/first-entry.aspx</link><pubDate>Wed, 04 Jan 2012 13:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10253029</guid><dc:creator>DorianCorompt</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/doriancorompt/rsscomments.aspx?WeblogPostID=10253029</wfw:commentRss><comments>http://blogs.msdn.com/b/doriancorompt/archive/2012/01/04/first-entry.aspx#comments</comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Hello future readers,&lt;/p&gt;  &lt;p&gt;I am Dorian Corompt, I am a new developer consultant employee at Microsoft France.&lt;/p&gt;  &lt;p&gt;As a good resolution for this new year, I decided to start writing a blog to share with you my passion for technologies.&lt;/p&gt;  &lt;p&gt;What I intend to publish on this blog:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Comments and feedbacks about new Microsoft products/technologies:     &lt;br /&gt;There will be a lot to write here since Microsoft is releasing a lot of new exciting technologies every years.      &lt;br /&gt;My first posts under this section will probably target Windows 8 which is coming really soon!      &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;Quick start/How to tutorials, advices and experience concerning our technologies:     &lt;br /&gt;Sometimes we browse the internet for hours trying to resolve a little problem we could resolve in 5 minutes with some hints.      &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;.NET programming languages articles:     &lt;br /&gt;I like communication and languages, natural languages as well as programming languages or even music and mathematics.      &lt;br /&gt;The .NET framework allows a programming languages abstraction, opening new perspective and new way of elegantly solve problems.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I will try to write most of my posts in English to enlarge the audience, so I apologize in advance for the mistakes. I may write some articles in Japanese. And depending on the subject complexity I could switch back to French.&lt;/p&gt;  &lt;p&gt;I hope this first entry will be the first of many,&lt;/p&gt;  &lt;p&gt;Thanks&lt;/p&gt;  &lt;p&gt;Dorian&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10253029" width="1" height="1"&gt;</description></item></channel></rss>