<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title type="html">The Mellow Musings of Dr. T</title><subtitle type="html" /><id>http://blogs.msdn.com/b/madst/atom.aspx</id><link rel="alternate" type="text/html" href="http://blogs.msdn.com/b/madst/" /><link rel="self" type="application/atom+xml" href="http://blogs.msdn.com/b/madst/atom.aspx" /><generator uri="http://telligent.com" version="5.6.50428.7875">Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><updated>2006-08-16T15:58:00Z</updated><entry><title>Recursive lambda expressions</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/b/madst/archive/2007/05/11/recursive-lambda-expressions.aspx" /><id>http://blogs.msdn.com/b/madst/archive/2007/05/11/recursive-lambda-expressions.aspx</id><published>2007-05-11T22:55:00Z</published><updated>2007-05-11T22:55:00Z</updated><content type="html">&lt;FONT face=Verdana color=#000080&gt;
&lt;P&gt;This is a very geeky post. The tiny piece of useful information comes right at the bottom. The rest of it is all artifacts of &lt;I style="mso-bidi-font-style: normal"&gt;the obscure art of doing lambda calculus in C#&lt;/I&gt;, which can also be characterized as &lt;EM&gt;doing very much with very little, sacrificing only readability&lt;/EM&gt;. &lt;/P&gt;
&lt;P&gt;People sometimes complain that you cannot write a lambda expression that is recursive. Good old factorial, for instance, how to write that as a lambda expression? Well the fathers of lambda calculus, who invented the lambda expressions in the 1930’s struggled with that, too, and as you might have guessed they came up with a solution.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;In this post let us stand on the shoulders of those giants and see how you can get recursion into your own lambda expressions in C#. It may not be practical but it is fun!&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;FONT face=Verdana color=#000080&gt;How to write factorial&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;So what you really want is to be able to write the lambda expression:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;x =&amp;gt; x == 0 ? 1 : x * fac(x-1)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;But that won’t work – we’re using fac to define fac, but we haven’t defined it yet! Usually when you want to use something you don’t know, you abstract over it at let someone pass it to you later. So we could abstract over the factorial function itself and write:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;fac =&amp;gt; x =&amp;gt; x == 0 ? 1 : x * fac(x-1)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Now what we are saying is, if someone could please hand us the &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;fac&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; function, then we can tell them what the &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;fac&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; function should do, in terms of itself.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;At the face of it that seems immediately useless; but in fact it is not; we just need to find a way to “wire the function back to itself”.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;FONT face=Verdana color=#000080&gt;Fixed points&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Some functions on a domain have one or more fixed points. A fixed point for a function &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;f&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; is a value &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;x&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; such that &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;f(x) = x&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;. For instance a fixed point of the function &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;MultiplyByTwo&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; is &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;0&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; because &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;MultiplyByTwo(0) = 0&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Our lambda above is also a function &lt;I style="mso-bidi-font-style: normal"&gt;on &lt;/I&gt;a domain; i.e., one that maps from a type to the same type. The type of it is&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Func&amp;lt;Func&amp;lt;int,int&amp;gt;,Func&amp;lt;int,int&amp;gt;&amp;gt; F = &lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;fac =&amp;gt; x =&amp;gt; x == 0 ? 1 : x * fac(x-1)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;In other words it maps from &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Func&amp;lt;int,int&amp;gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; to &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Func&amp;lt;int,int&amp;gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;. If we could find a fixed point for F, that would be the factorial function! Why? Because, by the definition of fixed points, that fixed point, let us optimistically call it &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Factorial&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; would satisfy that &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;F(Factorial)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; = &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Factorial&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;. Which for any input x would mean that &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Factorial(x)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; &lt;/FONT&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 12.95pt"&gt;&lt;FONT face=Verdana color=#000080&gt;= &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;F(Factorial)(x)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; &lt;/FONT&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 12.95pt"&gt;&lt;FONT face=Verdana color=#000080&gt;= &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;x == 0 ? 1 : x * Factorial(x-1)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;This is the very definition of what it means to be the factorial function!&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Finding fixed points for functions can be very hard. Finding fixed points for functions over &lt;I style="mso-bidi-font-style: normal"&gt;functions&lt;/I&gt; however, is known territory, and in fact guys such as Church, Curry and Turing, who all dabbled in the lambda calculus back in the 1930’s and 40’s came up with nice ways of doing it automatically.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;Self replication through self application&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;To see how we can get something recursive going using just lambda expression, let us look at the simplest form of &lt;I style="mso-bidi-font-style: normal"&gt;self application&lt;/I&gt;. Imagine that we have a function that takes a function and applies it to itself, like this:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;f =&amp;gt; f(f)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;First let us see how we can type this in C#. This is one of the few odd lambda expressions that you can’t really give a Func&amp;lt;…&amp;gt; type. Try it and see why. Instead let’s define a special delegate type for self-applicable functions:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;delegate T SelfApplicable&amp;lt;T&amp;gt;(SelfApplicable&amp;lt;T&amp;gt; self);&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face="Lucida Console" color=#008000&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;We can now define the self application function above like this:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;SelfApplicable&amp;lt;int&amp;gt; omega = f =&amp;gt; f(f);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;What happens if we apply &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;omega&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; to itself?&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;omega(omega);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;well &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;omega&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; applies &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;omega&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; to itself, which will apply &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;omega&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; to itself, which will … infinite recursion. While not particularly useful in this setting, the example demonstrates that you can in fact have recursive application in a lambda expression. Now all we have to do is to make it do something useful as it goes, such as call functions recursively.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;Y combinators&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;What both Curry and Turing came up with was ways to write a lambda expression that has the same self-replicating structure as &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;omega&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;, but which additionally replicates something else – the application of a given function.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;For some reason the things they came up with are often called Y combinators. Just take that as a curious fact or look up the history yourself. The contract for a Y combinator is roughly this: &lt;I style="mso-bidi-font-style: normal"&gt;“Give me &lt;U&gt;myself&lt;/U&gt; and a higher order function f, and I’ll return to you a function that is a fixed point of f.”&lt;/I&gt; So:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;SelfApplicable&amp;lt;Func&amp;lt;Func&amp;lt;Func&amp;lt;int,int&amp;gt;,Func&amp;lt;int,int&amp;gt;&amp;gt;,Func&amp;lt;int,int&amp;gt;&amp;gt;&amp;gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;Y = y =&amp;gt; f =&amp;gt; …;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;If we can write the body of &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Y&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; so that it fulfills the contract, that means that &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Y(Y)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; is actually a fixed point finder: Give it a higher order function and it returns a fixed point. For instance, use it on the factorial-related higher-order function &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;F&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; from above, &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Y(Y)(F)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;, and you get – the factorial function!&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;But that is the case also &lt;I style="mso-bidi-font-style: normal"&gt;inside&lt;/I&gt; the body of &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Y&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;. Because the parameter (lower-case) &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;y&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; represents &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Y&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; itself, &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;y(y)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; is &lt;I style="mso-bidi-font-style: normal"&gt;also&lt;/I&gt; a fixed point finder, which we can then apply inside the body. In particular, inside the body we can use it to take the fixed point of the second argument, &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;f&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;! Can we then just return &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;y(y)(f)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; as the result? &lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;SelfApplicable&amp;lt;Func&amp;lt;Func&amp;lt;Func&amp;lt;int,int&amp;gt;,Func&amp;lt;int,int&amp;gt;&amp;gt;,Func&amp;lt;int,int&amp;gt;&amp;gt;&amp;gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Y = y =&amp;gt; f =&amp;gt; y(y)(f);// Not good&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Not so fast. That would have us go around in circles forever, not making any progress. We’d never actually use &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;f&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; for anything. But we can recursively apply &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;y(y)(f)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; to effect the &lt;I style="mso-bidi-font-style: normal"&gt;next&lt;/I&gt; level of the recursion inside the body of &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Y&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;. More specifically, remember that &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;f&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; is a higher order function that expects to be passed the fixed point. So let’s pass &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;y(y)(f)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; – the fixed point – to &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;f&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; itself:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;SelfApplicable&amp;lt;Func&amp;lt;Func&amp;lt;Func&amp;lt;int,int&amp;gt;,Func&amp;lt;int,int&amp;gt;&amp;gt;,Func&amp;lt;int,int&amp;gt;&amp;gt;&amp;gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Y = y =&amp;gt; f =&amp;gt; f(y(y)(f));// Still not quite good&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;This would actually work if we were in a lazy language such as Haskell. However, in an eager call-by-value language like C#, it would go off and immediately try to compute an infinitely deep factorial function where the recursion is endlessly unfolded. So you never get to actually apply it because you have to wait forever first.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Instead, the right thing to do is to do the unfolding gradually as the arguments to the resulting function are passed. We can do it like this:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;SelfApplicable&amp;lt;Func&amp;lt;Func&amp;lt;int,int&amp;gt;,Func&amp;lt;int,int&amp;gt;&amp;gt;, Func&amp;lt;int,int&amp;gt;&amp;gt; Y =&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;y =&amp;gt; f =&amp;gt; x =&amp;gt; f(y(y)(f))(x);// Quite good indeed&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;In this way we defer the recursive call of &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;y(y)(f)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; until someone has actually called the resulting function – e.g., factorial – with a value. This turns out to work nicely.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;Putting it together&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;We can now go ahead and define a fixed point finder (or rather builder) function Fix as simply:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Func&amp;lt;Func&amp;lt;Func&amp;lt;int,int&amp;gt;,Func&amp;lt;int,int&amp;gt;&amp;gt;,Func&amp;lt;int,int&amp;gt;&amp;gt; Fix = Y(Y);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;And finally we can get our factorial function by writing&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Func&amp;lt;int,int&amp;gt; factorial = Fix(F);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Or if you want to write it out:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Func&amp;lt;int,int&amp;gt; factorial = Fix(fac =&amp;gt; x =&amp;gt; x == 0 ? 1 : x * fac(x-1));&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;The whole code looks like this:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;public class Program&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;{&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;delegate T SelfApplicable&amp;lt;T&amp;gt;(SelfApplicable&amp;lt;T&amp;gt; self);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face="Lucida Console" color=#008000&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;static void Main(string[] args)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;{&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;// The Y combinator&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;SelfApplicable&amp;lt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 3"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Func&amp;lt;Func&amp;lt;Func&amp;lt;int,int&amp;gt;,Func&amp;lt;int,int&amp;gt;&amp;gt;,Func&amp;lt;int,int&amp;gt;&amp;gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&amp;gt; Y = y =&amp;gt; f =&amp;gt; x =&amp;gt; f(y(y)(f))(x);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face="Lucida Console" color=#008000&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;// The fixed point generator&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Func&amp;lt;Func&amp;lt;Func&amp;lt;int, int&amp;gt;, Func&amp;lt;int, int&amp;gt;&amp;gt;, Func&amp;lt;int, int&amp;gt;&amp;gt; Fix = &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 3"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Y(Y);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face="Lucida Console" color=#008000&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;// The higher order function describing factorial&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Func&amp;lt;Func&amp;lt;int,int&amp;gt;,Func&amp;lt;int,int&amp;gt;&amp;gt; F = &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 3"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;fac =&amp;gt; x =&amp;gt; x == 0 ? 1 : x * fac(x-1);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face="Lucida Console" color=#008000&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;// The factorial function itself&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Func&amp;lt;int,int&amp;gt; factorial = Fix(F);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face="Lucida Console" color=#008000&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;for (int i = 0; i &amp;lt; 12; i++) {&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 3"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Console.WriteLine(factorial(i));&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;}&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;}&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;So how does this work again? &lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;It is instructive to look at function evaluation as a gradual in-place replacement of terms with what they evaluate to. In fact that is how the semantics of the lambda calculus are typically described. So let’s go ahead and “execute” the program ourselves, to observe how the recursion comes about.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;First, &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Y&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; is a lambda expression, so there’s nothing to execute (well in C# a delegate is generated and stored, but that’s it).&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 12.95pt"&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Y&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; = &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;y =&amp;gt; f =&amp;gt; x =&amp;gt; f(y(y)(f))(x)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Next, what happens when we build the &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Fix&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; function? Let’s evaluate:&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 12.95pt"&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Fix&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; = &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Y(Y)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; = &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;f =&amp;gt; x =&amp;gt; f(Y(Y)(f))(x)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;The higher order function F is again a lambda so nothing to evaluate:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&lt;FONT face=Verdana color=#000080&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;F&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; = &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;fac =&amp;gt; x =&amp;gt; x == 0 ? 1 : x * fac(x-1)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;In the following we will allow ourselves to use the local variable names above as abbreviations of the functions themselves. Now we are ready to build our &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;factorial&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; function:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&lt;FONT face=Verdana color=#000080&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;factorial&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; = &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Fix(F)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; = &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;x =&amp;gt; F(Y(Y)(F))(x)&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Now let’s apply &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;factorial&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; to a value:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&lt;FONT face=Verdana color=#000080&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;factorial(5)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; &lt;BR&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;= &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;F(Y(Y)(F))(5)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; &lt;BR&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;= &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;(x =&amp;gt; x == 0 ? 1 : x * Y(Y)(F)(x-1))(5)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; &lt;BR&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;= &lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN class=Codeinline&gt;5 == 0 ? 1 : 5 * Y(Y)(F)(5 – 1)&lt;/SPAN&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;= &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN class=Codeinline&gt;5 * Y(Y)(F)(4)&lt;/SPAN&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;= &lt;/FONT&gt;&lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;5 * (y =&amp;gt; f =&amp;gt; x =&amp;gt; f(y(y)(f))(x))(Y)(F)(4))&lt;BR&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;= &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;5 * F(Y(Y)(F))(4)&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;See how the recursion occurs? The third-to-last line is equivalent to &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;5 * Fix(F)(4)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;. So we take the fixed point of F once again, so that the last line is equivalent to &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;5 * factorial(4)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;! The factorial function has completed its self replication and is ready to party on the next argument. Of course it will go around and around from here, and all we need to see is how it ends. At some point we’ll get to:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&lt;FONT face=Verdana color=#000080&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN class=Codeinline&gt;5 * 4 * 3 * 2 * 1 * F(Y(Y)(F))(0)&lt;/SPAN&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;= &lt;/FONT&gt;&lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;5 * 4 * 3 * 2 * 1 * (x =&amp;gt; x == 0 ? 1 : x * Y(Y)(F)(x-1))(0)&lt;BR&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;= &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN class=Codeinline&gt;5 * 4 * 3 * 2 * 1 * 0 == 0 ? 1 : 0 * Y(Y)(F)(0 – 1)&lt;/SPAN&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;= &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN class=Codeinline&gt;5 * 4 * 3 * 2 * 1 * 1&lt;/SPAN&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;= &lt;/FONT&gt;&lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;120&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Magic! We’re done.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;Recursive lambdas&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Now you may argue that the definition above of &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;factorial&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; isn’t actually a lambda expression, and besides it was created using all sorts of helper functions. But look at the step above where we compute what &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;factorial&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; really “is”. This step is interesting because it shows how a recursive function such as factorial can be written directly as a pure lambda expression without any other predefined scaffolding than a few predefined types: Substitute the actual lambdas designated by Y and F directly in there (surrounded by a few explicit delegate creation expressions when the compiler can’t figure them out, and a renaming of x to i to avoid name collisions) and you have a fully self-contained recursive function directly described as a lambda. Admitted it ain’t pretty:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;i =&amp;gt; new Func&amp;lt;Func&amp;lt;int,int&amp;gt;,Func&amp;lt;int,int&amp;gt;&amp;gt;(fac =&amp;gt; x =&amp;gt; x == 0 ? 1 : x * fac(x - 1))(new SelfApplicable&amp;lt;Func&amp;lt;Func&amp;lt;Func&amp;lt;int,int&amp;gt;,Func&amp;lt;int,int&amp;gt;&amp;gt;,Func&amp;lt;int,int&amp;gt;&amp;gt;&amp;gt;(y =&amp;gt; f =&amp;gt; x =&amp;gt; f(y(y)(f))(x))(y =&amp;gt; f =&amp;gt; x =&amp;gt; f(y(y)(f))(x))(fac =&amp;gt; x =&amp;gt; x == 0 ? 1 : x * fac(x - 1)))(i)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Neat, huh? I can’t even figure out how to line break it so that it approaches readable, so I haven’t. &lt;I style="mso-bidi-font-style: normal"&gt;But!&lt;/I&gt; It is proof of concept that you can write real recursive lambda expressions if you really really want to! This means you can do it in expression trees, too, although I wouldn’t &lt;I style="mso-bidi-font-style: normal"&gt;really&lt;/I&gt; recommend it. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;So the next time you hear someone complain that you can’t write a recursive lambda expression, just throw them a fixed point generator!&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;Enough already&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Of course you would like to generalize so &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Y&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; and &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Fix&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; don’t only work on functions over &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;int&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;. You can’t have generic lambdas, so what do you do? A trick is to put &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Y&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; and &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Fix&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; as static members of a generic class:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;public class Fun&amp;lt;T&amp;gt; {&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;public static … Y = … ;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;public static … Fix = Y(Y);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Then you can access the fixed point operator for int functions as &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Fun&amp;lt;int&amp;gt;.Fix&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;For practical purposes, you can also choose to write your fixed point generator as an ordinary named generic recursive method, which is much easier and works just as well:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Func&amp;lt;T, T&amp;gt; Fix&amp;lt;T&amp;gt;(Func&amp;lt;Func&amp;lt;T,T&amp;gt;, Func&amp;lt;T,T&amp;gt;&amp;gt; F) {&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;return t =&amp;gt; F(Fix(F))(t);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Then you also don’t need the intermediate Y combinator and the freak &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;SelfApplicable&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; delegate type. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Regardless of how you write your fixed point generator, it is a useful thing whenever you want to pass a recursive function that you cook up on the fly.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;If you want to play some more with this stuff, I recommend the following two exercises:&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1"&gt;&lt;FONT color=#000080&gt;&lt;SPAN style="mso-bidi-font-family: Verdana; mso-fareast-font-family: Verdana"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT face=Verdana&gt;1)&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana&gt;Make a fixed point generator for functions of two arguments&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1"&gt;&lt;FONT color=#000080&gt;&lt;SPAN style="mso-bidi-font-family: Verdana; mso-fareast-font-family: Verdana"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT face=Verdana&gt;2)&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana&gt;Figure out how to make two mutually recursive functions in this way&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Anyway, enough lambda calculus for one day. Now get back to work!&lt;/FONT&gt;&lt;/P&gt;&lt;A href="http://www.dotnetkicks.com/kick/?url=http://blogs.msdn.com/madst/archive/2007/05/11/recursive-lambda-expressions.aspx" mce_href="http://www.dotnetkicks.com/kick/?url=http://blogs.msdn.com/madst/archive/2007/05/11/recursive-lambda-expressions.aspx"&gt;&lt;IMG alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://blogs.msdn.com/madst/archive/2007/05/11/recursive-lambda-expressions.aspx" border=0 mce_src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://blogs.msdn.com/madst/archive/2007/05/11/recursive-lambda-expressions.aspx"&gt;&lt;/A&gt; &lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2552088" width="1" height="1"&gt;</content><author><name>Mads Torgersen - MSFT</name><uri>http://blogs.msdn.com/madstorgersen/ProfileUrlRedirect.ashx</uri></author></entry><entry><title>Is C# becoming a functional language?</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/b/madst/archive/2007/01/23/is-c-becoming-a-functional-language.aspx" /><id>http://blogs.msdn.com/b/madst/archive/2007/01/23/is-c-becoming-a-functional-language.aspx</id><published>2007-01-23T22:05:00Z</published><updated>2007-01-23T22:05:00Z</updated><content type="html">&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;As many of you will be aware, C#3.0 is adding a significant number of new language features.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;While the overall driving force behind putting these features in is the support of LINQ (Language INtegrated Query), the way we do it is strongly inspired by functional programming techniques. Moreover, we strive to make the new features as general-purpose as possible (with the exception of &lt;I style="mso-bidi-font-style: normal"&gt;query expressions&lt;/I&gt; – they are undeniably tied to the querying domain!), so they do come out as features that will enable more of a functional style of programming.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;This begs the question: Are we making C# a functional language?&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;This of course is a vague question. First of all, what exactly &lt;I style="mso-bidi-font-style: normal"&gt;is&lt;/I&gt; a functional programming language? What does it take to qualify? Rather than trying to define the canonical list of language features that you have to have to be functional, I think it makes more sense to define it as “a language that supports a functional style of programming.”&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Given that, my just as vague answer would be “to some degree”: If you’re a dedicated functional programmer, is this the time to throw out your &lt;/FONT&gt;&lt;A href="http://www.haskell.org/"&gt;&lt;FONT face=Verdana color=#800080&gt;Haskell&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana color=#000080&gt;, &lt;/FONT&gt;&lt;A href="http://www.schemers.org/"&gt;&lt;FONT face=Verdana color=#800080&gt;Scheme&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana color=#000080&gt;, &lt;/FONT&gt;&lt;A href="http://research.microsoft.com/fsharp/"&gt;&lt;FONT face=Verdana&gt;F#&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana color=#000080&gt; or &lt;/FONT&gt;&lt;A href="http://en.wikipedia.org/wiki/OCaml"&gt;&lt;FONT face=Verdana color=#800080&gt;OCaml&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana color=#000080&gt; compiler and join the party? Probably not. C# doesn’t go the whole way. In this post I’ll explore in more detail the goodie bag of functional programming, in a kind of willy-nilly fashion, not focusing on a particular functional language, and then examine to what extent C# is supporting each of the features or styles of programming.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Please be aware that Visual Basic is adding most of the same language features as C# in the Orcas release.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;Expression-based programming&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/wesdyer/"&gt;&lt;FONT face=Verdana color=#800080&gt;Wes Dyer&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana color=#000080&gt; has &lt;/FONT&gt;&lt;A href="http://blogs.msdn.com/wesdyer/archive/2007/01/18/why-functional-programming-is-important-in-a-mixed-environment.aspx"&gt;&lt;FONT face=Verdana color=#800080&gt;a great post&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana color=#000080&gt; about expression-based versus statement-based programming. When you construct things the expression-based way, you build them bottom-up from their constituents, as opposed to the statements based way, where you create things top down by modifying them.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Instead of &lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Point p1 = new Point();&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;P1.X = 3; P1.Y = 5;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Point p2 = new Point();&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;p2.X = -5; p2.Y = 4;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Point p3 = new Point();&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;p3.X = -1; p3.Y = -6;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Polygon triangle = new Polygon();&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Triangle.Add(p1);&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Triangle.Add(p2);&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Triangle.Add(p3);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Given the right constructors you could do it the expression-oriented way:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Polygon triangle = &lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;new Polygon(&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;new Point(3,5), &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;new Point(-5,4), &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;new Point(-1,-6));&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Much nicer, and composes really well. Trouble is you have to have the right constructors, but sometimes you don’t, or sometimes it’s impractical or annoying to write all the constructors that correspond to reasonable initialization patterns of your type. Furthermore they may get big, and the caller then has to remember the order of all the arguments.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;To this end, C#3.0 introduces object initializers and constructor initializers. Assuming that Polygon is a collection type (see my previous post for a definition of that term), you can now write this:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Polygon triangle =&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;new Polygon {&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 3"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;new Point { X = 3, Y = 5 },&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;new Point { X = -5, Y = 4 },&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;new Point { X = -1, Y = -6 }};&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;In LINQ, queries are all about expressions. In order to produce the right output of a query, you often need to construct new objects out of old ones as in&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;from m in members&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;group m by m.Address.PostalCode into group&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;select new Entry { PostalCode = group.Key, Number = group.Count() };&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;The subexpressions of the query expressions are, well, &lt;I style="mso-bidi-font-style: normal"&gt;expressions&lt;/I&gt;, and it is really important that you can create and initialize new objects in that context, or we don’t have projection in our query story.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;Value based programming&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Expression based programming limits the need for mutation operations (assignment) to such a degree that some functional languages (the “pure” ones) don’t even have them. Instead all data, whether simple or complex, is considered immutable values.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Even if they do allow mutation, many functional languages rely more on value types. The lack of mutability is really quite a feature. No-one can mess with the stuff you pass to them, and concurrent access is really easy to synchronize! You can have sound value-based equality and hashing. Implementations can optimize to their heart’s contend by copying, parallelizing, etc.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;In C# you can certainly create user defined types that are immutable, but it is far from as easy as in a functional language. Making properties immutable is quite easy – don’t write a setter - &lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;but the associated value based semantics for equality and hashcode computation are a pain to write.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;C# 3.0 doesn’t change that a lot, but this is something we may want to look into in the future, as the need for parallelism increases with the number of cores in our CPUs, and the number of people wanting to use value-based techniques increases.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;Types on the fly&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Many functional programming languages rely to a larger degree on structural types that do not have to be declared but are just there when you need them: You have type constructors that allow you to build types from other types, and corresponding value constructors to built values of those types. In a fully structurally typed world you never declare a new type: any type declaration would just introduce an alias for an existing type. (And you need that, because these types can get &lt;I style="mso-bidi-font-style: normal"&gt;large&lt;/I&gt;!)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;We don’t quite go there in C#3.0, but we do take you further in the direction of not having to declare so many trivial little types. With anonymous types, for instance, the above query can be written as&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;from m in members&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;group m by m.Address.PostalCode into group&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 12.95pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;select new { PostalCode = group.Key, Number = group.Count() };&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;eliminating the need to author a trivial type &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Entry&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; to hold your results. The limitation here is that you cannot designate the type in any way, so it cannot be used &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Another thing that takes you in the direction of structural types is generics: You still have to declare a generic type, but once declared you can construct new types from it over and over. We utilize that in the LINQ libraries to make a set of fully generic delegate types called &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Func&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;, one for each number of arguments, e.g.:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 12.95pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;public delegate TResult Func&amp;lt;TArg0, TResult&amp;gt;(TArg0 arg0);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;From this you can create a delegate for almost any one argument function type, e.g. &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Func&amp;lt;int,bool&amp;gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; for functions from &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;int&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; to &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;bool&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;. Because we use the Func types consistently within LINQ, in that context they essentially act as structural function types. You never need to write your own delegate type again. Essentially, &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Func&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; is a type constructor for delegate types.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;Tuples&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;One kind of on-the-fly types heavily used in functional programming is tuples: strongly typed lists of values; just like the set of arguments to a method, only as a value in itself that you can pass around.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;In popular syntax &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;(1,”one”)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; is an example of a tuple value of the type &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;(int,string)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;. Some functional languages don’t even allow you to specify functions of more than one value; instead that value can be a tuple. Where tuples would be really interesting would be for multiple results of a function or method. Imagine&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;(int,int) NextFib(int curr, int prev) { return (curr+prev,curr); }&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Tuples is one really useful kind of type constructor that it might be useful to add in the future. Like the &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Func&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; types above, you could imagine .NET having centrally defined generic &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Tuple&amp;lt;…&amp;gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; types predeclared up to a certain arity, e.g.:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;public class Tuple&amp;lt;TFirst,TSecond&amp;gt; {&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;readonly TFirst first;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;readonly TSecond second;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public TFirst First { get { return first; } }&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public TSecond Second { get { return second; } }&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public Tuple(TFirst first, TSecond second) { … }&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;}&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;You get the idea. It would then be a matter of syntactic desugaring to translate &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;(5,3)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; into &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;new Tuple&amp;lt;int,int&amp;gt;(5,3)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;There are currently no plans of shared tuple types on .NET, but languages such as F# on top of .NET roll their own.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;Pattern matching&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;To really make use of constructed values, you need a good way of deconstructing them. For the functional programmer, pattern matching is the tool of choice. With tuples for instance, rather than “dotting your way” into the individual values, as in:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;(int,int) result = NextFib(5,3);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;int current = result.First;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;int previous = result.Second;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;you can create and assign in one fell swoop the variables to hold the individual component value of the tuple, by “matching” its structure:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;(int current, int previous) = NextFib(5,3);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Pattern matching can be used to declare a function by cases – as in:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;FibHelp(0) = (1,0)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;FibHelp(n) = NextFib(FibHelp(n–1))&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;This defines one function, but saves the &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;if-then-else&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; logic by matching on the arguments.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;A final common use of type constructors and pattern matching is with built-in immutable list types. Let’s say that &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;[]&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; is the empty list. You can the “cons up” a list as e.g. &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;1 :: 2 :: 3 :: []&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;. Each occurrence of :: creates a new list value with the left hand side value prepended to the right hand side list. The cool thing is that you can pattern match the elements back off when you see them:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;AddAll([]) = 0;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;AddAll(n :: l) = n + AddAll(l);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Pattern matching is a whole different approach to plucking values apart, and we are nowhere near adding such features to C#.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;Higher-order programming&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;When you want to have a piece of code executed conditionally, or repeatedly, C# has a nice set of built-in control structures. This is, however, a very closed regime: No one gets to add their own control structures as new language features! In order to write your own control structures you need the ability to be parameterized with functionality, with chunks of code.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Any object oriented programming language actually facilitates that. You can write a class&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;class ChunkOfCode {&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public abstract void DoIt();&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;}&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;which people can subclass to override the &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;DoIt&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; method with the right functionality. &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;ChunkOfCode&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; objects can then be passed to “control structure methods” that execute the &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;DoIt&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; methods if and where they want.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;On .NET we make this a whole lot easier with delegate types. You can grab a delegate to any old method and pass it around.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;In C#2.0 we made it even easier by allowing you to construct a delegate “on the fly” with anonymous methods. The major improvement here is not so much avoiding to declare a method, but the fact that anonymous methods are “closures” that allow you to refer to arguments and local variables in the enclosing method body. This is crucial when the functionality you want to pass depends on your local state, as is so often the case.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;At the core of LINQ you find the standard query operators, such as &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Where&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;, &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Select&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;, &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;GroupBy&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;, &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Join&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;, etc. These are really user defined control structures, parameterized by little bit of functionality represented as delegate types. Thus to make the passing of these delegates really neat we’ve given anonymous methods a face lift in the form of lambda expressions:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;members&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;.GroupBy(m =&amp;gt; m.Address.PostalCode)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;.Select(group =&amp;gt; new { PostalCode=group.Key, Number=group.Count() });&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Lambda expressions in C# are not entirely like anonymous functions in a functional language because they don’t have a type in and off themselves. Instead they can be converted to delegate types, and with the &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Func&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; types sufficiently standardized, the effect is pretty much the same. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;Currying&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;We talk above about tuples as a way of making input and output more symmetric on methods or functions. However, whereas a C# programmer today only has the option of writing multiple &lt;I style="mso-bidi-font-style: normal"&gt;parameters&lt;/I&gt;, not multiple results, a functional programmer will often choose to do the opposite – use tuples &lt;I style="mso-bidi-font-style: normal"&gt;only&lt;/I&gt; for compound result types. The reason is that functions of multiple arguments are often written using &lt;I style="mso-bidi-font-style: normal"&gt;currying&lt;/I&gt;, where the original function only takes one argument, and then returns a function that takes the rest. Example: Instead of&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;Add (x,y) = x + y&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;You write &lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;Add (x) (y) = x + y&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;which is a shorthand for&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;Add x = y =&amp;gt; x + y&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;So instead of calling Add(3,2) you can call Add(3) to get a function that will take an integer and return the result of adding 3 to it! Hardcore functional programmers will use this technique extensively and take great care to arrange their curried arguments in such an order that they will lead to the most useful “derived functions” when only some of the arguments are passed.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;You have to question how useful this is in practice, but enough functional programmers swear by it that I guess I’m willing to believe it will save me some day. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Strictly speaking, with anonymous methods or lambda expressions C# methods can also be curried, but it ain’t pretty:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;Func&amp;lt;int,int&amp;gt; Add(int x) { return y =&amp;gt; x + y; }&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;However currying of lambda expressions themselves gets pretty agreeable:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;x =&amp;gt; y =&amp;gt; x + y;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;Type inference&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;One convenient feature of statically typed functional languages is that you can have most or all of your type annotations inferred for you so that typing is really the compiler’s problem. In an object-oriented world we don’t have that option in the large, and in statically typed object-oriented languages such as C# we sort of take pride in the fact that our APIs are in fact annotated with types that users can &lt;I style="mso-bidi-font-style: normal"&gt;see&lt;/I&gt;. At the architectural level, having explicit contracts is important.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;However, in the small there are often cases where types are just in the way.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Already in C# 2.0 we introduced type inference for generic method calls, so that most of the time you do not have to be explicit about the type arguments to a method – they are inferred from the types of the method arguments.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;In C# 3.0 we take this idea further, by being really clever about the way we use lambda expressions for type inference when they are used as arguments to a call of a generic method. Thus the types “flow” through the method call in and out of the lambdas and out in the return type. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Furthermore we’re adding type inference for local variables with the &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;var&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; keyword. Thus in&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;var query = customers&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;.Where(c =&amp;gt; c.City == “London”)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;.SelectMany(c =&amp;gt; c.Orders)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;.GroupBy(o =&amp;gt; o.OrderDate.Year);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Without writing a simple type the compiler can figure out that &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;query&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; is of type &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;IGrouping&amp;lt;int,Order&amp;gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;In C# we have deliberately kept the type inferencing algorithm simple, in the sense that it never infers a type that is not already there in the constituent values: We don’t &lt;I style="mso-bidi-font-style: normal"&gt;synthesize&lt;/I&gt; types. This keeps the types under control, so the user has a chance of understanding what is going on if they get an error message etc.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;Comprehensions&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Oftentimes the passing around of lambdas gets too messy even for a functional programmer, and so many functional languages introduce syntactic sugar for common patterns involving the passing of functions. Such sugar is called a comprehension (presumably because it makes the code comprehensible) and we snatched that idea right in with query expressions in C# and VB – indeed in the early design days we did call them query &lt;I style="mso-bidi-font-style: normal"&gt;comprehensions&lt;/I&gt;. In C# these are really just syntactic sugar which enables people to write most queries without lambda expressions. The above query (or one very much like it), for instance is generated by the query expression&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;var query = &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;from c in customers&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;where c.City == “London”&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;from o in c.Orders&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;group o by o.OrderDate.Year;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;The extreme &lt;I style="mso-bidi-font-style: normal"&gt;syntacticness&lt;/I&gt; of comprehensions is central in LINQ; it means that we just generate the method calls to the query operators syntactically, but anyone can implement their own methods with the right names on a given type and have it be the source of query expressions.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;Conclusions&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;C# 3.0 and LINQ owe a lot to the functional programming languages in terms of the mechanisms we adopt. No doubt this will rub off to some degree on non-LINQ scenarios where a more expression-oriented style of programming becomes possible. People who are functional programmers by night and have to write C# by day will find that the daily headache sets in a little later in the afternoon.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;However, as you can see from the above, there is some way to go before full-blown functional programming becomes totally natural in C#. There is probably a limit as to how far you can straddle before the pants rip, and we may never get to a point where C# is a true multiparadigm language with equal treatment of object-oriented and functional programming. Still I think there is room for more of this kind.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;We have this thing about listening to customers, so I guess it is also up to you!&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;o:p&gt;&lt;FONT face=Verdana color=#000080&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1516246" width="1" height="1"&gt;</content><author><name>Mads Torgersen - MSFT</name><uri>http://blogs.msdn.com/madstorgersen/ProfileUrlRedirect.ashx</uri></author></entry><entry><title>What is a collection?</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/b/madst/archive/2006/10/10/what-is-a-collection_3F00_.aspx" /><id>http://blogs.msdn.com/b/madst/archive/2006/10/10/what-is-a-collection_3F00_.aspx</id><published>2006-10-11T00:36:00Z</published><updated>2006-10-11T00:36:00Z</updated><content type="html">&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Admitted, we blew it in the first version of the framework with &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;System.Collections.ICollection&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;, which is next to useless. But we fixed it up pretty well when generics came along in .NET framework 2.0: &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;System.Collections.Generic.ICollection&amp;lt;T&amp;gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; lets you &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Add&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; and &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Remove&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; elements, enumerate them, &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Count&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; them and check for membership.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Obviously from then on, everyone would implement &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;ICollection&amp;lt;T&amp;gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; every time they make a collection, right? Not so. Here is how we used LINQ to learn about what collections &lt;I&gt;really&lt;/I&gt; are, and how that made us change our language design in C# 3.0.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Heading3web style="MARGIN: auto 0in"&gt;&lt;STRONG&gt;&lt;FONT face=Verdana color=#000080&gt;Collection initializers&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;With LINQ, the &lt;B style="mso-bidi-font-weight: normal"&gt;&lt;U&gt;L&lt;/U&gt;&lt;/B&gt;anguage &lt;B style="mso-bidi-font-weight: normal"&gt;&lt;U&gt;IN&lt;/U&gt;&lt;/B&gt;tegrated &lt;B style="mso-bidi-font-weight: normal"&gt;&lt;U&gt;Q&lt;/U&gt;&lt;/B&gt;uery framework that we're shipping in Orcas, we're enabling a more expression-oriented style of programming. For instance it should be possible to create and intialize an object within one expression. For collections, initialization typically amounts to adding an initial set of elements. Hence collection initializers in C# 3.0 look like this:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;new MyNames { "&lt;?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /&gt;&lt;st1:PersonName w:st="on"&gt;Luke Hoban&lt;/st1:PersonName&gt;", "&lt;st1:PersonName w:st="on"&gt;Karen Liu&lt;/st1:PersonName&gt;", "&lt;st1:PersonName w:st="on"&gt;Charlie Calvert&lt;/st1:PersonName&gt;" }&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;The meaning of this new syntax is simply to create an instance of &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;MyNames&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; using its no-arg constructor (constructor arguments can be supplied if necessary) and call its Add method with each of the strings.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;So what types do we allow collection initializers &lt;I&gt;on&lt;/I&gt;? Easy: collection types. What are those? Obvious: types that implement &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;ICollection&amp;lt;T&amp;gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;. This is a nice and easy design - &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;ICollection&amp;lt;T&amp;gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; ensures that you have&amp;nbsp;an &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Add&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; method so obviously that is the one that gets called for each element in the collection initializer. It is strongly typed, too - the initializer can contain only elements of the appropriate element type. In the above new expression, &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;MyNames&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; would be a class that implements &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;ICollection&amp;lt;string&amp;gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; and everything works smoothly from there.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;There's just one problem: &lt;I&gt;Nobody implements &lt;/I&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;ICollection&amp;lt;T&amp;gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;!&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Heading3web style="MARGIN: auto 0in"&gt;&lt;STRONG&gt;&lt;FONT face=Verdana color=#000080&gt;LINQ to LINQ&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Well, &lt;I&gt;nobody&lt;/I&gt; is a strong word. But we did an extensive study of our own framework classes, and found only a few that did. How? Using LINQ of course. The following query does the trick:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;from name in assemblyNames&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;select Assembly.LoadWithPartialName(name) into a&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;from c in a.GetTypes()&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;where c.IsPublic &amp;amp;&amp;amp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;c.GetConstructors().Any(m =&amp;gt; m.IsPublic) &amp;amp;&amp;amp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;GetInterfaceTypes(c).Contains(typeof(ICollection&amp;lt;&amp;gt;)) &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;select c.FullName;&lt;SPAN style="COLOR: navy; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Let’s go through this query a little bit and see what it does. For each &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;name&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; in a list of &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;assemblyNames&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; that we pre-baked for the purpose, load up the corresponding assembly:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;from name in assemblyNames&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;select Assembly.LoadWithPartialName(name)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;One at a time, put the reflection objects representing these assemblies &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;into a&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;, and for each assembly &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;a&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; run through the types &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;c&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; defined in there:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;from c in a.GetTypes()&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Filter through, keeping each type only if it &lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; TEXT-INDENT: -0.25in; tab-stops: list .5in; mso-list: l1 level1 lfo1"&gt;&lt;FONT color=#008000&gt;&lt;SPAN class=Codeinline&gt;&lt;SPAN style="mso-fareast-font-family: 'Lucida Console'; mso-bidi-font-family: 'Lucida Console'"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT face="Lucida Console"&gt;a)&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console"&gt;IsPublic&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; TEXT-INDENT: -0.25in; tab-stops: list .5in; mso-list: l1 level1 lfo1"&gt;&lt;FONT color=#000080&gt;&lt;SPAN style="mso-fareast-font-family: Verdana; mso-bidi-font-family: Verdana"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT face=Verdana&gt;b)&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana&gt;has &lt;/FONT&gt;&lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Any&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; constructor that &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;IsPublic&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; TEXT-INDENT: -0.25in; tab-stops: list .5in; mso-list: l1 level1 lfo1"&gt;&lt;FONT color=#000080&gt;&lt;SPAN style="mso-fareast-font-family: Verdana; mso-bidi-font-family: Verdana"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT face=Verdana&gt;c)&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana&gt;implements &lt;/FONT&gt;&lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;ICollection&amp;lt;T&amp;gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; for some &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;T&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;where c.IsPublic &amp;amp;&amp;amp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;c.GetConstructors().Any(m =&amp;gt; m.IsPublic) &amp;amp;&amp;amp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;GetInterfaceTypes(c).Contains(typeof(ICollection&amp;lt;&amp;gt;)) &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;For those that pass this test, select out their full name:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;select c.FullName;&lt;SPAN style="COLOR: navy; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Nothing to it, really.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Heading3web style="MARGIN: auto 0in"&gt;&lt;STRONG&gt;&lt;FONT face=Verdana color=#000080&gt;What is a collection?&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;What did we find then? Only 14 of our own (public) classes (with public constructors) implement &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;ICollection&amp;lt;T&amp;gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;! Obviously there are a lot more collections in the framework, so it was clear that we needed some other way of telling whether something is a collection class. LINQ to the rescue once more: With modified versions of the query it was easy to establish that among our public classes with public constructors there are:&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.2in; TEXT-INDENT: -0.2in; tab-stops: list .2in; mso-list: l4 level1 lfo2"&gt;&lt;FONT color=#000080&gt;&lt;SPAN style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;·&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana&gt;189 that have a public &lt;/FONT&gt;&lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Add&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; method and implement &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;System.Collections.IEnumerable&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.2in; TEXT-INDENT: -0.2in; tab-stops: list .2in; mso-list: l4 level1 lfo2"&gt;&lt;FONT color=#000080&gt;&lt;SPAN style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;·&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana&gt;42 that have a public &lt;/FONT&gt;&lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Add&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; method but do not implement &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;System.Collections.IEnumerable&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;If you look at the classes returned by these two queries, you realize that there are essentially two fundamentally different meanings of the name “&lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Add&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;”:&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; TEXT-INDENT: -0.25in; tab-stops: list .5in; mso-list: l3 level1 lfo3"&gt;&lt;FONT color=#000080&gt;&lt;SPAN style="mso-fareast-font-family: Verdana; mso-bidi-font-family: Verdana"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT face=Verdana&gt;a)&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana&gt;Insert the argument into a collection, or &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; TEXT-INDENT: -0.25in; tab-stops: list .5in; mso-list: l3 level1 lfo3"&gt;&lt;FONT color=#000080&gt;&lt;SPAN style="mso-fareast-font-family: Verdana; mso-bidi-font-family: Verdana"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT face=Verdana&gt;b)&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana&gt;Return the arithmetic sum of the argument and the receiver. &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;People are actually very good at (directly or indirectly) implementing the nongeneric &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;IEnumerable&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; interface when writing collection classes, so that turns out to be a pretty reliable indicator of whether an &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Add&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; method is the first or the second kind. Thus for our purposes the operational answer to the headline question becomes:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;I style="mso-bidi-font-style: normal"&gt;&lt;FONT face=Verdana color=#000080&gt;A collection is a type that implements IEnumerable and has a public &lt;/FONT&gt;&lt;/I&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Add&lt;/FONT&gt;&lt;/SPAN&gt;&lt;I style="mso-bidi-font-style: normal"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt; method&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=Heading3web style="MARGIN: auto 0in"&gt;&lt;STRONG&gt;&lt;FONT face=Verdana color=#000080&gt;Which Add to call?&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;We ain’t done yet, though. Further LINQ queries over the 189 collection types identified above show:&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.2in; TEXT-INDENT: -0.2in; tab-stops: list .2in; mso-list: l0 level1 lfo4"&gt;&lt;FONT color=#000080&gt;&lt;SPAN style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;·&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana&gt;28 collection types have more than one &lt;/FONT&gt;&lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Add&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; method&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.2in; TEXT-INDENT: -0.2in; tab-stops: list .2in; mso-list: l0 level1 lfo4"&gt;&lt;FONT color=#000080&gt;&lt;SPAN style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;·&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana&gt;30 collection types have no &lt;/FONT&gt;&lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Add&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; method with just one argument&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;So, given that our collection initializers are supposed to call “the” &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Add&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; method which one should they call? It seems that there will be some value in collection initializers allowing you to:&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; TEXT-INDENT: -0.25in; tab-stops: list .5in; mso-list: l2 level1 lfo5"&gt;&lt;FONT color=#000080&gt;&lt;SPAN style="mso-fareast-font-family: Verdana; mso-bidi-font-family: Verdana"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT face=Verdana&gt;a)&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana&gt;choose which overload to call&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; TEXT-INDENT: -0.25in; tab-stops: list .5in; mso-list: l2 level1 lfo5"&gt;&lt;FONT color=#000080&gt;&lt;SPAN style="mso-fareast-font-family: Verdana; mso-bidi-font-family: Verdana"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT face=Verdana&gt;b)&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana&gt;call Add methods with more than one argument&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Our resolution to this is to refine our understanding of collection initializers a little bit. The list you provide is not a “list of elements to add”, but a “list of sets of arguments to &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Add&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; methods”. If an entry in the list consists of multiple arguments to an &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Add&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; method, these are enclosed in &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;{&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; curly braces &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt;. This is actually immensely useful. For example, it allows you to&lt;SPAN style="mso-spacerun: yes"&gt; &lt;/SPAN&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Add&lt;/FONT&gt;&lt;/SPAN&gt; key/value pairs to a dictionary, something we have had a number of requests for as a separate feature.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;The initializer list does not have to be homogenous; we do separate overload resolution against &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Add&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; methods for each entry in the list.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;So given a collection class&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;public class Plurals : IDictionary&amp;lt;string,string&amp;gt; {&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;public void Add(string singular, string plural); // implements IDictionary&amp;lt;string,string&amp;gt;.Add&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;public void Add(string singular); // appends an “s” to the singular form&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;public void Add(KeyValuePair&amp;lt;string,string&amp;gt; pair); // implements ICollection&amp;lt;KeyValuePair&amp;lt;string,string&amp;gt;&amp;gt;.Add&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Lucida Console"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/SPAN&gt;…&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;We can write the following collection initializer:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Plurals myPlurals = new Plurals{ “collection”, { “query”, “queries” }, new KeyValuePair(“child”, “children”) };&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;which would make use of all the different &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Add&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; methods on our collection class.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Heading3web style="MARGIN: auto 0in"&gt;&lt;STRONG&gt;&lt;FONT face=Verdana color=#000080&gt;Is this right?&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;The resulting language design is a “pattern based” approach. We rely on users using a particular name for their methods in a way that is not checked by the compiler when they write it. If they go and change the name of &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;Add&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; to &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;AddPair&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; in one assembly, the compiler won’t complain about that, but instead about a collection initializer sitting somewhere else suddenly missing an overload to call.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Here I think it is instructive to look at our history. We already have pattern-based syntax in C# - the &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;foreach&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; pattern. Though not everybody realizes it, you can actually write a class that does &lt;I style="mso-bidi-font-style: normal"&gt;not&lt;/I&gt; implement IEnumerable and have &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;foreach&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; work over it; as long as it contains a GetEnumerator method. What happens though is that people overwhelmingly &lt;I style="mso-bidi-font-style: normal"&gt;choose&lt;/I&gt; to have the compiler help them keep it right by implementing the &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;IEnumerable&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; interface. In the same way we fully expect people to recognize the additional benefit of implementing &lt;/FONT&gt;&lt;SPAN class=Codeinline&gt;&lt;FONT face="Lucida Console" color=#008000&gt;ICollection&amp;lt;T&amp;gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080&gt; in the future – not only can your collection be initialized, but the compiler checks it too. So while we are currently in a situation where very few classes implement ICollection&amp;lt;T&amp;gt; this is likely to change over time, and with the new tweaks to our collection initializer design we hope to have ensured that the feature adds value both now and in that future.&lt;/FONT&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=814637" width="1" height="1"&gt;</content><author><name>Mads Torgersen - MSFT</name><uri>http://blogs.msdn.com/madstorgersen/ProfileUrlRedirect.ashx</uri></author></entry><entry><title>Welcome to the language designer's workshop</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/b/madst/archive/2006/08/16/welcome-to-the-language-designer_2700_s-workshop.aspx" /><id>http://blogs.msdn.com/b/madst/archive/2006/08/16/welcome-to-the-language-designer_2700_s-workshop.aspx</id><published>2006-08-16T17:58:00Z</published><updated>2006-08-16T17:58:00Z</updated><content type="html">&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Hi there,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Ever tried designing a programming language? If you got far enough you'll know that it is fun, exciting, exhausting, mind boggling, frustrating and utterly surprising.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;I'm the language PM on the C# team here at Microsoft. I am part of the language design group, take notes from our meetings, communicate decisions to the production team and write some of the specs.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;This is a wonderful team. Everyone has a strong sense of responsibility for the quality of our product, and not least for the design of our language. We all have our own opinions of course, and we happily volunteer them. Ever so often, profound discussions erupt, sometimes as galactic mail threads slowly revolving around a fiery core, sometimes as violent tornados of creativity sucking up all productivity in their devastating trail for a couple of days.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Some issues just keep coming back. We get fed up with them, make some decision and throw them out the window, only to see them boomerang back and whack us squarely between the eyes a couple of months later.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Most baffling is the complexity. Sometimes what seems like a small or arbitrary decision has vast consequences. Oftentimes we happily settle on a design, believing that we've thought it all through, only to find weeks later when someone has to implement it that the whole thing is impractical or impossible in its current form.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;On this blog I want to share some of that with you; some of the hard issues, cool ideas, crazy mistakes and whirling discussions that are part of my life here at Microsoft. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Thanks for visiting our workshop, and come back soon. And, er... please don't run that query over there - it has only been tested on Customers.&lt;/FONT&gt;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=702641" width="1" height="1"&gt;</content><author><name>Mads Torgersen - MSFT</name><uri>http://blogs.msdn.com/madstorgersen/ProfileUrlRedirect.ashx</uri></author></entry></feed>