<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title type="html">.NET at 9.400 ft above sea level</title><subtitle type="html">Programming in Quito, 2.860 m above sea level</subtitle><id>http://weblogs.asp.net/esanchez/atom.aspx</id><link rel="alternate" type="text/html" href="http://weblogs.asp.net/esanchez/default.aspx" /><link rel="self" type="application/atom+xml" href="http://weblogs.asp.net/esanchez/atom.aspx" /><generator uri="http://communityserver.org" version="3.0.20510.895">Community Server</generator><updated>2007-12-02T14:22:00Z</updated><entry><title>F# 1.9.4.19 runs out of the box with Mono in Linux</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/esanchez/archive/2008/07/14/f-1-9-4-19-runs-out-of-the-box-with-mono-in-linux.aspx" /><id>http://weblogs.asp.net/esanchez/archive/2008/07/14/f-1-9-4-19-runs-out-of-the-box-with-mono-in-linux.aspx</id><published>2008-07-15T04:25:39Z</published><updated>2008-07-15T04:25:39Z</updated><content type="html">&lt;p&gt;Don Syme just &lt;a href="http://blogs.msdn.com/dsyme/archive/2008/07/14/1-9-4-19-release.aspx" target="_blank"&gt;announced a minor update to the F# environment&lt;/a&gt;, minor may be but of great interest to a certain community: it so happens that at some point F# stopped working properly in Linux, a &lt;a href="http://laurent.le-brun.eu/site/index.php/2008/06/05/36-how-to-use-fsharp-1-9-4-17-on-mono" target="_blank"&gt;workaround&lt;/a&gt; was published (and it actually works, but you've got to follow the instructions carefully). Well, not anymore, 1.9.4.19 works out of the box with Mono in Linux, you just have to download it, unzip it, and then happily type "mono fsi.exe":&lt;/p&gt; &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/esanchez/WindowsLiveWriter/F1.9.4.19runsoutoftheboxwithMonoinLinux_14947/FSharpMonoLinux_2.jpg"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="685" alt="FSharpMonoLinux" src="http://weblogs.asp.net/blogs/esanchez/WindowsLiveWriter/F1.9.4.19runsoutoftheboxwithMonoinLinux_14947/FSharpMonoLinux_thumb.jpg" width="824" border="0"&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;So now I've got one less pretext for not writing that book "Learning to program the functional way in an open source environment using a cool Microsoft technology" that will make me famous...&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6400869" width="1" height="1"&gt;</content><author><name>Edgar Sánchez</name><uri>http://weblogs.asp.net/members/Edgar-S_E100_nchez.aspx</uri></author><category term="Functional Programming" scheme="http://weblogs.asp.net/esanchez/archive/tags/Functional+Programming/default.aspx" /><category term="Mono" scheme="http://weblogs.asp.net/esanchez/archive/tags/Mono/default.aspx" /><category term="F#" scheme="http://weblogs.asp.net/esanchez/archive/tags/F_2300_/default.aspx" /><category term="Programming Languages" scheme="http://weblogs.asp.net/esanchez/archive/tags/Programming+Languages/default.aspx" /></entry><entry><title>Cloning objects in .NET</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/esanchez/archive/2008/05/18/cloning-objects-in-net.aspx" /><id>http://weblogs.asp.net/esanchez/archive/2008/05/18/cloning-objects-in-net.aspx</id><published>2008-05-18T06:27:38Z</published><updated>2008-05-18T06:27:38Z</updated><content type="html">&lt;p&gt;In an interesting project where I'm giving a hand, we need to clone objects of a number of different types, perhaps surprisingly the CLR doesn't offer a general cloning method, of course you could use &lt;a href="http://msdn.microsoft.com/en-us/system.object.memberwiseclone.aspx" target="_blank"&gt;MemberwiseClone()&lt;/a&gt; but this is a protected method, so it can be invoked only from inside the class of the object being cloned, which makes it difficult to use it in a general method, besides, MemberWiseClone() does just a &lt;a href="http://en.wikipedia.org/wiki/Object_copy#Shallow_copy" target="_blank"&gt;shallow copy&lt;/a&gt; and what we really need is a &lt;a href="http://en.wikipedia.org/wiki/Object_copy#Deep_copy" target="_blank"&gt;deep copy&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;There is a good reason for not having such a general method: object cloning is one of those problems which have a simple solution for simple scenarios but that resist a satisfactory solution for all the scenarios, for example the objects may have references to other objects and even to themselves be it directly or after a long chain, for example a customer has invoices that have payments that refer to the customer, a general cloning algorithm for a web several times more complex than that is anything but trivial. But the need of moving objects (and their web) is inescapable in distributed environments, because you have to move the invoices and the customers from the business layer to the presentation layer, so there are indeed mechanisms, albeit with some limitations, for serializing and deserializing object graphs, with the help of these mechanisms we can try and build a general object cloner:&lt;/p&gt;  &lt;div style="font-size: 10pt; background: #101010; color: white; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160;&amp;#160; 1&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160;&amp;#160; 2&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #8ac5ff"&gt;public&lt;/span&gt; &lt;span style="color: #8ac5ff"&gt;static&lt;/span&gt; T BinaryClone&lt;span style="color: silver"&gt;&amp;lt;&lt;/span&gt;T&lt;span style="color: silver"&gt;&amp;gt;&lt;/span&gt;(&lt;span style="color: #8ac5ff"&gt;this&lt;/span&gt; T originalObject)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160;&amp;#160; 3&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160;&amp;#160; 4&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #8ac5ff"&gt;using&lt;/span&gt; (&lt;span style="color: #8ac5ff"&gt;var&lt;/span&gt; stream &lt;span style="color: silver"&gt;=&lt;/span&gt; &lt;span style="color: #8ac5ff"&gt;new&lt;/span&gt; System&lt;span style="color: silver"&gt;.&lt;/span&gt;IO&lt;span style="color: silver"&gt;.&lt;/span&gt;&lt;span style="color: #64b1ff"&gt;MemoryStream&lt;/span&gt;())&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160;&amp;#160; 5&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160;&amp;#160; 6&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #8ac5ff"&gt;var&lt;/span&gt; binaryFormatter &lt;span style="color: silver"&gt;=&lt;/span&gt; &lt;span style="color: #8ac5ff"&gt;new&lt;/span&gt; System&lt;span style="color: silver"&gt;.&lt;/span&gt;Runtime&lt;span style="color: silver"&gt;.&lt;/span&gt;Serialization&lt;span style="color: silver"&gt;.&lt;/span&gt;Formatters&lt;span style="color: silver"&gt;.&lt;/span&gt;Binary&lt;span style="color: silver"&gt;.&lt;/span&gt;&lt;span style="color: #64b1ff"&gt;BinaryFormatter&lt;/span&gt;();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160;&amp;#160; 7&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; binaryFormatter&lt;span style="color: silver"&gt;.&lt;/span&gt;Serialize(stream, originalObject);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160;&amp;#160; 8&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; stream&lt;span style="color: silver"&gt;.&lt;/span&gt;Position &lt;span style="color: silver"&gt;=&lt;/span&gt; &lt;span style="color: #ffff80"&gt;0&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160;&amp;#160; 9&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #8ac5ff"&gt;return&lt;/span&gt; (T)binaryFormatter&lt;span style="color: silver"&gt;.&lt;/span&gt;Deserialize(stream);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160; 10&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160; 11&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160; 12&lt;/span&gt;&amp;#160;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;We just convert, using as intermediary a memory stream, our object to a bit string and then we synthesize a *new object* from that bit string. The use of BinaryClone() flows nicely:&lt;/p&gt;  &lt;div style="font-size: 10pt; background: #101010; color: white; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160;&amp;#160; 1&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160;&amp;#160; 2&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #8ac5ff"&gt;var&lt;/span&gt; clone &lt;span style="color: silver"&gt;=&lt;/span&gt; person&lt;span style="color: silver"&gt;.&lt;/span&gt;BinaryClone();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160;&amp;#160; 3&lt;/span&gt;&amp;#160;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Easy to write, but we have to be careful of the performance and the corner cases. I run a few *very informal* performance tests with this object:&lt;/p&gt;  &lt;div style="font-size: 10pt; background: #101010; color: white; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160;&amp;#160; 1&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160;&amp;#160; 2&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #8ac5ff"&gt;var&lt;/span&gt; person &lt;span style="color: silver"&gt;=&lt;/span&gt; &lt;span style="color: #8ac5ff"&gt;new&lt;/span&gt; &lt;span style="color: #64b1ff"&gt;Person&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160;&amp;#160; 3&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160;&amp;#160; 4&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Id &lt;span style="color: silver"&gt;=&lt;/span&gt; &lt;span style="color: #ffff80"&gt;101&lt;/span&gt;,&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160;&amp;#160; 5&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Name &lt;span style="color: silver"&gt;=&lt;/span&gt; &lt;span style="color: #ff8040"&gt;&amp;quot;S&amp;#225;nchez, Sebasti&amp;#225;n&amp;quot;&lt;/span&gt;,&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160;&amp;#160; 6&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Salary &lt;span style="color: silver"&gt;=&lt;/span&gt; &lt;span style="color: #ffff80"&gt;590.20m&lt;/span&gt;,&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160;&amp;#160; 7&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; BirthDate &lt;span style="color: silver"&gt;=&lt;/span&gt; &lt;span style="color: #8ac5ff"&gt;new&lt;/span&gt; &lt;span style="color: #ffffaa"&gt;DateTime&lt;/span&gt;(&lt;span style="color: #ffff80"&gt;2000&lt;/span&gt;, &lt;span style="color: #ffff80"&gt;6&lt;/span&gt;, &lt;span style="color: #ffff80"&gt;15&lt;/span&gt;),&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160;&amp;#160; 8&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Address &lt;span style="color: silver"&gt;=&lt;/span&gt; &lt;span style="color: #8ac5ff"&gt;new&lt;/span&gt; &lt;span style="color: #64b1ff"&gt;Address&lt;/span&gt; { Number &lt;span style="color: silver"&gt;=&lt;/span&gt; &lt;span style="color: #ff8040"&gt;&amp;quot;N24-78&amp;quot;&lt;/span&gt;, Street &lt;span style="color: silver"&gt;=&lt;/span&gt; &lt;span style="color: #ff8040"&gt;&amp;quot;Pasaje C&amp;#243;rdova&amp;quot;&lt;/span&gt; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160;&amp;#160; 9&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; };&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160; 10&lt;/span&gt;&amp;#160;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;And I found that doing a hundred thousand clones takes some nine thousand milliseconds, that is each cloning takes less than one ten-thousandth of a second, which is adequate to our needs. BinaryFormatter has been with us since .NET Framework 1.0 so I'm not like saying anything even remotely new or unknown, but tomorrow (or the day after, or the day after...) I'll talk about a small refinement to BinaryClone().&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6200321" width="1" height="1"&gt;</content><author><name>Edgar Sánchez</name><uri>http://weblogs.asp.net/members/Edgar-S_E100_nchez.aspx</uri></author><category term="C#" scheme="http://weblogs.asp.net/esanchez/archive/tags/C_2300_/default.aspx" /><category term=".NET" scheme="http://weblogs.asp.net/esanchez/archive/tags/.NET/default.aspx" /></entry><entry><title>A cool way to find out whether a number is palindromic</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/esanchez/archive/2008/05/06/a-cool-way-to-find-out-whether-a-number-is-palindromic.aspx" /><id>http://weblogs.asp.net/esanchez/archive/2008/05/06/a-cool-way-to-find-out-whether-a-number-is-palindromic.aspx</id><published>2008-05-06T06:40:08Z</published><updated>2008-05-06T06:40:08Z</updated><content type="html">&lt;p&gt;In &lt;a href="http://weblogs.asp.net/esanchez/archive/2008/04/22/nested-sequences-and-palindrome-numbers.aspx" target="_blank"&gt;this blog entry I proposed a solution&lt;/a&gt; to &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=4" target="_blank"&gt;Problem 4 at Project Euler&lt;/a&gt;, a crucial element of the problem is to find out whether a number is a palindrome (909 is, 809 isn't), a bit out of laziness and a bit in order to reuse existing methods, I decided to verify the palindrome by converting the number into a char array and then comparing this array with its mirror version, it works but it's not really that mathematical... &lt;a href="http://diditwith.net/2008/05/02/YAPESProblemFour.aspx" target="_blank"&gt;Dustin Campbell proposed a solution&lt;/a&gt; kind of similar to mine (alas, more elegant and, above that, in F#) and using the same trick of converting the number to chars, as he didn't like this part of the solution, in &lt;a href="http://diditwith.net/2008/05/06/YAPESProblemFourAlternateSolution.aspx" target="_blank"&gt;this new blog entry he proposes the detection of a palindrome by mirroring the number one digit at a time&lt;/a&gt;. A translation of his F# code to C# 3.0 could be:&lt;/p&gt; &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; TailReverseNumber = &lt;span style="color: blue"&gt;null&lt;/span&gt;;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TailReverseNumber = (n, res) =&amp;gt; n == 0 ? res : TailReverseNumber(n / 10, 10 * res + n % 10);&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;/span&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt; IsPalindrome = n =&amp;gt; n == TailReverseNumber(n, 0);&lt;/p&gt;&lt;/div&gt; &lt;p&gt;TailReverseNumber takes a number n and "mirrors" it, one digit at a time, for example: TailReverseNumber(237, 0) -&amp;gt; TailReverseNumber(23, 7) -&amp;gt; TailReverseNumber(2, 73) -&amp;gt; TailReverseNumber(0, 732), the big trick is that res works as an accumulator that is multiplied by 10, therefore moving its value to the left, and putting in the "hole" that appears at the right the least significant digit of n. As it names implies, TailReverseNumber() uses &lt;a href="http://en.wikipedia.org/wiki/Tail_recursion" target="_blank"&gt;tail recursion&lt;/a&gt;, so that no extra call stack space is used to save any intermediate results, which makes the process pretty efficient. Actually, in my PC it's four times faster than the initial solution. More efficient and more elegant, a smart guy Dustin.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6161587" width="1" height="1"&gt;</content><author><name>Edgar Sánchez</name><uri>http://weblogs.asp.net/members/Edgar-S_E100_nchez.aspx</uri></author><category term="C#" scheme="http://weblogs.asp.net/esanchez/archive/tags/C_2300_/default.aspx" /><category term="Functional Programming" scheme="http://weblogs.asp.net/esanchez/archive/tags/Functional+Programming/default.aspx" /><category term="F#" scheme="http://weblogs.asp.net/esanchez/archive/tags/F_2300_/default.aspx" /><category term="C# 3.0" scheme="http://weblogs.asp.net/esanchez/archive/tags/C_2300_+3.0/default.aspx" /><category term="Project Euler" scheme="http://weblogs.asp.net/esanchez/archive/tags/Project+Euler/default.aspx" /></entry><entry><title>New version of F# just released</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/esanchez/archive/2008/05/03/new-version-of-f-just-released.aspx" /><id>http://weblogs.asp.net/esanchez/archive/2008/05/03/new-version-of-f-just-released.aspx</id><published>2008-05-03T06:26:07Z</published><updated>2008-05-03T06:26:07Z</updated><content type="html">&lt;p&gt;In its way from research language to commercial language, &lt;a href="http://blogs.msdn.com/dsyme/archive/2008/05/02/f-1-9-4-now-available-making-f-simpler-and-more-consistent.aspx" target="_blank"&gt;Don Syme just announced&lt;/a&gt; that, silently, on May the 1st &lt;a href="http://research.microsoft.com/research/downloads/Details/7ac148a7-149b-4056-aa06-1e6754efd36f/Details.aspx" target="_blank"&gt;version 1.9.4.15 of F# was released&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;a href="http://weblogs.asp.net/blogs/esanchez/WindowsLiveWriter/NewversionofFjustreleased_1422/FSharp19415_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="259" alt="F# 1.9.4.15" src="http://weblogs.asp.net/blogs/esanchez/WindowsLiveWriter/NewversionofFjustreleased_1422/FSharp19415_thumb.png" width="724" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;This new version incorporates a number of specific enhancements (F# is now basically in stabilization mode, so we really shouldn't expect significative changes to the language). By the way, the example in the picture shows my idea for solving &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=2" target="_blank"&gt;Problema 2 of Project&amp;#160; Euler&lt;/a&gt;: find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million. For an explanation of how it works, I suggest you this &lt;a href="http://diditwith.net/blog/Default.aspx#a8fc9ed17-94a5-440d-95be-c831f1e1a1de" target="_blank"&gt;Dustin Campbell blog&lt;/a&gt;, which proposes a solution similar to mine with a couple of elegant touches and, above all, with a detailed and engaging explanation.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6152555" width="1" height="1"&gt;</content><author><name>Edgar Sánchez</name><uri>http://weblogs.asp.net/members/Edgar-S_E100_nchez.aspx</uri></author><category term="Functional Programming" scheme="http://weblogs.asp.net/esanchez/archive/tags/Functional+Programming/default.aspx" /><category term="F#" scheme="http://weblogs.asp.net/esanchez/archive/tags/F_2300_/default.aspx" /><category term="Project Euler" scheme="http://weblogs.asp.net/esanchez/archive/tags/Project+Euler/default.aspx" /></entry><entry><title>Which is the ten thousand first prime?</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/esanchez/archive/2008/05/02/which-is-the-ten-thousand-first-prime.aspx" /><id>http://weblogs.asp.net/esanchez/archive/2008/05/02/which-is-the-ten-thousand-first-prime.aspx</id><published>2008-05-02T05:09:56Z</published><updated>2008-05-02T05:09:56Z</updated><content type="html">&lt;p&gt;Prime numbers have a good deal of practical applications (for example in cryptography) but let's face it, even if they would have none, they would still be the &lt;a title="Prime Obsession" href="http://olimu.com/Riemann/Riemann.htm" target="_blank"&gt;favorite toy of mathematicians&lt;/a&gt;. In &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=7" target="_blank"&gt;Problem 7 of Project Euler&lt;/a&gt;, we are asked to find the 10001st element of the famous list, my approach was this:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Define the *infinite* sequence of the prime numbers &lt;/li&gt;    &lt;li&gt;From this sequence, throw away the first 10000 items and then take the first of the remaining &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Creating an infinite sequence in C# is easy (since version 2) thanks to IEnumerables and, above all, the yield statement:&lt;/p&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 1&lt;/span&gt;&amp;#160;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; Primes()&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 2&lt;/span&gt; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 3&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;yield&lt;/span&gt; &lt;span style="color: blue"&gt;return&lt;/span&gt; 2;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 4&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 5&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; primesSoFar = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 6&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; primesSoFar.Add(2);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 7&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 8&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt; IsPrime = n =&amp;gt; primesSoFar.TakeWhile(p =&amp;gt; p &amp;lt;= (&lt;span style="color: blue"&gt;int&lt;/span&gt;)&lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Sqrt(n)).FirstOrDefault(p =&amp;gt; n % p == 0) == 0;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 9&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 10&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;for&lt;/span&gt; (&lt;span style="color: blue"&gt;int&lt;/span&gt; i = 3; &lt;span style="color: blue"&gt;true&lt;/span&gt;; i += 2)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 11&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 12&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (IsPrime(i))&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 13&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 14&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;yield&lt;/span&gt; &lt;span style="color: blue"&gt;return&lt;/span&gt; i;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 15&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; primesSoFar.Add(i);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 16&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 17&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 18&lt;/span&gt; }&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;The yield at line 3 returns the first item of the sequence: the always excepcional 2 (the only even prime). Then at line 5 we create a list where we will be saving the generated primes as we progress in the sequence (this way we will gain speed at the cost of memory). The IsPrime(n) function defined at line 9, proposes a method -pretty crude actually- of verifying whether a number is prime: we take all primes generated so far which are lower or equal than the square root of n, and we look for the first among them that divides n evenly, if such a divisor exists then n is not a prime, that is: if none of the primes already generated is an exact divisor of n then FirstOrDefault() returns a 0, signaling the fact that n is indeed a prime. Finally at line 10 starts the loop that, every time the Prime() invoker asks for an item, it progresses thru 3, 5, 7, 9, 11, 13, ... stopping and returning, thru yield, when it finds a new prime.&lt;/p&gt;  &lt;p&gt;With this sequence in our hands, step 2 of my plan is utterly simple:&lt;/p&gt;  &lt;blockquote&gt;   &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;     &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;return&lt;/span&gt; Primes().Skip(nth - 1).First();&lt;/p&gt;   &lt;/div&gt; &lt;/blockquote&gt;  &lt;p&gt;We take the Primes() sequence, ignore the first nth - 1 (in our case nth = 100001) and then take the first of the remaining. This little code returns the answer in far less than a second.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6149321" width="1" height="1"&gt;</content><author><name>Edgar Sánchez</name><uri>http://weblogs.asp.net/members/Edgar-S_E100_nchez.aspx</uri></author><category term="C#" scheme="http://weblogs.asp.net/esanchez/archive/tags/C_2300_/default.aspx" /><category term="Functional Programming" scheme="http://weblogs.asp.net/esanchez/archive/tags/Functional+Programming/default.aspx" /><category term="LINQ" scheme="http://weblogs.asp.net/esanchez/archive/tags/LINQ/default.aspx" /><category term="Math" scheme="http://weblogs.asp.net/esanchez/archive/tags/Math/default.aspx" /><category term="C# 3.0" scheme="http://weblogs.asp.net/esanchez/archive/tags/C_2300_+3.0/default.aspx" /><category term="Project Euler" scheme="http://weblogs.asp.net/esanchez/archive/tags/Project+Euler/default.aspx" /></entry><entry><title>The square of the sum vs. the sum of the squares</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/esanchez/archive/2008/04/27/the-square-of-the-sum-vs-the-sum-of-the-squares.aspx" /><id>http://weblogs.asp.net/esanchez/archive/2008/04/27/the-square-of-the-sum-vs-the-sum-of-the-squares.aspx</id><published>2008-04-28T02:56:32Z</published><updated>2008-04-28T02:56:32Z</updated><content type="html">&lt;p&gt;The &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=6" target="_blank"&gt;sixth Project Euler problem&lt;/a&gt; poses an exercise that, to me, offers no major hurdles:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;What is the difference between the sum of the squares and the square of the sums [of a sequence of natural numbers]?&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The functional C# solution is fairly easy to write and read:&lt;/p&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 1&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;int&lt;/span&gt; SquareSumsLessSumSquares(&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; sequence)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 2&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 3&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; sum = sequence.Sum();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 4&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; sumSquares = sequence.Select(i =&amp;gt; i * i).Sum();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 5&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; sum * sum - sumSquares;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 6&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;We get any sequence of integers (list, array, etc.) and we find the sum of its elements at line 3. The Select() in line 4 generates a new sequence with the square of each item from the original sequence and then we add them up. From there, getting the answer to Problem 6 is easy. Actually, the specific value that the problem asks for is to find out this difference for the first 100 natural numbers, so the corresponding call is:&lt;/p&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; result006 = SquareSumsLessSumSquares(&lt;span style="color: #2b91af"&gt;Enumerable&lt;/span&gt;.Range(1, 100));&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Now, given that the sequence we are focused on is 1, 2, 3, ..., 100, we can leverage a couple of math formulas:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a title="The sum of the first n natural numbers" href="http://en.wikipedia.org/wiki/1_%2B_2_%2B_3_%2B_4_%2B_%C2%B7_%C2%B7_%C2%B7" target="_blank"&gt;&lt;img src="http://upload.wikimedia.org/math/f/1/a/f1a147372db0aa2d5cafc82164ea518c.png" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;y&lt;/p&gt;  &lt;p&gt;&lt;a title="The sum of the *squares* of the first n natural numbers" href="http://en.wikipedia.org/wiki/Square_pyramidal_number" target="_blank"&gt;&lt;img src="http://upload.wikimedia.org/math/7/7/9/779324dc10f4e762a3eb95f1c4977a6b.png" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Isn't the &lt;a href="http://en.wikipedia.org" target="_blank"&gt;Wikipedia&lt;/a&gt; something? With this information in our hands we can re-write our solution this way:&lt;/p&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 1&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;int&lt;/span&gt; SquareSumsLessSumSquaresFirstNumbers(&lt;span style="color: blue"&gt;int&lt;/span&gt; n)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 2&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 3&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; sum = n * (n + 1) / 2;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 4&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; sumSquares = n * (n + 1) * (2 * n + 1) / 6;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 5&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; sum * sum - sumSquares;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 6&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Clearly this latter way of solving the problem uses far less memory and CPU time. My first solution follows what is called a &amp;quot;brute force&amp;quot; approach, contemption intended. It works, for sure, but there are more elegant and efficient ways of solving the problem. The moral of this story is that a little research can take us a long way towards fresh solutions, probably better than our first approach. How frequently have you seen brute force solutions in production environments?&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6137746" width="1" height="1"&gt;</content><author><name>Edgar Sánchez</name><uri>http://weblogs.asp.net/members/Edgar-S_E100_nchez.aspx</uri></author><category term="C#" scheme="http://weblogs.asp.net/esanchez/archive/tags/C_2300_/default.aspx" /><category term="Functional Programming" scheme="http://weblogs.asp.net/esanchez/archive/tags/Functional+Programming/default.aspx" /><category term="C# 3.0" scheme="http://weblogs.asp.net/esanchez/archive/tags/C_2300_+3.0/default.aspx" /><category term="Project Euler" scheme="http://weblogs.asp.net/esanchez/archive/tags/Project+Euler/default.aspx" /></entry><entry><title>Recursive lambdas and sequence aggregations</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/esanchez/archive/2008/04/24/recursive-lambdas-and-sequence-aggregations.aspx" /><id>http://weblogs.asp.net/esanchez/archive/2008/04/24/recursive-lambdas-and-sequence-aggregations.aspx</id><published>2008-04-24T05:22:45Z</published><updated>2008-04-24T05:22:45Z</updated><content type="html">&lt;p&gt;The &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=5" target="_blank"&gt;fifth problem at Project Euler&lt;/a&gt; proposes this nostalgic primary school exercise: find the smallest quantity that is evenly divisible by some numbers, the &lt;a href="http://en.wikipedia.org/wiki/Least_common_multiple" target="_blank"&gt;least common multiple&lt;/a&gt; of 1, 2, 3, ..., 20 to be precise. To begin with, let's remember the old arithmetic formula:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;img src="http://upload.wikimedia.org/math/4/d/2/4d244548521249d1b5a71941506d5f41.png" /&gt; &lt;/p&gt;  &lt;p&gt;Where gcd is the &lt;a href="http://en.wikipedia.org/wiki/Greatest_common_divisor" target="_blank"&gt;greatest common divisor&lt;/a&gt; of a and b. It's also useful to remember that the lcm is associative, that is lcm(1,2,3) is the same as lcm(lcm(1,2), 3), in other words, we can calculate the lcm of 1 and 2, then the lcm of this result and 3, then the lcm of this result and 4, and so on. In functional C#, these ideas can be expressed like so:&lt;/p&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 1&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; gcd = &lt;span style="color: blue"&gt;null&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 2&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; gcd = (x, y) =&amp;gt; x % y == 0 ? y : gcd(y, x % y);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 3&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 4&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Enumerable&lt;/span&gt;.Range(1, 20).Aggregate(1, (x,y) =&amp;gt; x * (y / gcd(x, y)));&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Line 2 finds the gcd of x and y like this: if the remainder of dividing x by y is zero, then y is the gcd, if not, we must find the gcd of y and such remainder. Note that gcd is defined recursively. The only interesting point here is that in order to define a recursive lambda expression, it is mandatory to first declare the expression variable (which we do in line 1), and only after this the compiler allows us to define a lambda expression in terms of itself. Odd, granted, but easy to get used to.&lt;/p&gt;  &lt;p&gt;Line 4 takes the sequence 1, 2, ..., 20 and accumulates the lcm as the sequence is traversed, that is: it starts calculating lcm(1, 1) (let's call this accum1), then it calculates lcm(accum1, 2) (let's call this accum2), then it calculates lcm(accum2, 3), ..., and finishes with lcm(accum19, 20) which is precisely lcm(1, 2, ..., 20). The Aggregate() function is frequently used for chores like adding each term of a sequence, or multiplying them all, in our case we are using Aggregate() to get the succesive lcm's, which answers the question of Project Euler in two and a half lines of code, cool, eh?&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6125982" width="1" height="1"&gt;</content><author><name>Edgar Sánchez</name><uri>http://weblogs.asp.net/members/Edgar-S_E100_nchez.aspx</uri></author><category term="C#" scheme="http://weblogs.asp.net/esanchez/archive/tags/C_2300_/default.aspx" /><category term="Functional Programming" scheme="http://weblogs.asp.net/esanchez/archive/tags/Functional+Programming/default.aspx" /><category term="Math" scheme="http://weblogs.asp.net/esanchez/archive/tags/Math/default.aspx" /><category term="C# 3.0" scheme="http://weblogs.asp.net/esanchez/archive/tags/C_2300_+3.0/default.aspx" /><category term="Project Euler" scheme="http://weblogs.asp.net/esanchez/archive/tags/Project+Euler/default.aspx" /></entry><entry><title>Nested sequences and palindrome numbers</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/esanchez/archive/2008/04/22/nested-sequences-and-palindrome-numbers.aspx" /><id>http://weblogs.asp.net/esanchez/archive/2008/04/22/nested-sequences-and-palindrome-numbers.aspx</id><published>2008-04-22T06:41:44Z</published><updated>2008-04-22T06:41:44Z</updated><content type="html">&lt;p&gt;&lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=4" target="_blank"&gt;Problem 4 of Project Euler&lt;/a&gt; poses and impractical albeit intriguing problem: given all three digit numbers (100, 101, 102, ..., 998, 999), find the largest product of 2 of those numbers, where the &lt;strong&gt;product is a palindrome&lt;/strong&gt;. For example, 580.085 is the product of two three-digit numbers (995 x 583) but it isn't the largest of such products. One possible functional C# solution is:&lt;/p&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 1&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;,&lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt; IsPalindrome = n =&amp;gt; { &lt;span style="color: blue"&gt;var&lt;/span&gt; nchar = n.ToString().ToCharArray(); &lt;span style="color: blue"&gt;return&lt;/span&gt; nchar.SequenceEqual(nchar.Reverse()); };&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 2&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 3&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Enumerable&lt;/span&gt;.Range(100, 900)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 4&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .SelectMany(i =&amp;gt; &lt;span style="color: #2b91af"&gt;Enumerable&lt;/span&gt;.Range(i, 900 - (i - 100)).Select(j =&amp;gt; i * j))&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 5&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Where (p =&amp;gt; IsPalindrome(p))&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 6&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Max();&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;To begin with, let's focus on the interesting part: at line 3, Enumerable.Range() generates the sequence 100, 101, ..., 998, 999; SelectMany() at line 4 does several things, first &lt;strong&gt;for every number i from the previous line&lt;/strong&gt; it generates the sequence i, i + 1, i + 2, ..., 998, 999, and then the inner Select() generates the products {100 x 100, 100 x 101, ..., 100 x 999}, {101 x 101, 101 x 102, ..., 101 x 999 }, ... As you can see, there are 999 sequences generated (are all the same size?) finally SelectMany() kindly &amp;quot;flattens&amp;quot; those 999 sequences in one large sequence containing each and every product; line 5 then is easy, the Where() just keeps those products that are palindromes; the Max() in line 6 isn't worth any explanation.&lt;/p&gt;  &lt;p&gt;Back to line 1, I took the lazy path to check whether a number is a palindrome: I just converted it to a string and then to a char array, finally I checked whether that array is equal, item by item, to its reversed version (and all that work just to leverage IEnumerable.Reverse() ;-)&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6120740" width="1" height="1"&gt;</content><author><name>Edgar Sánchez</name><uri>http://weblogs.asp.net/members/Edgar-S_E100_nchez.aspx</uri></author><category term="C#" scheme="http://weblogs.asp.net/esanchez/archive/tags/C_2300_/default.aspx" /><category term="Functional Programming" scheme="http://weblogs.asp.net/esanchez/archive/tags/Functional+Programming/default.aspx" /><category term="C# 3.0" scheme="http://weblogs.asp.net/esanchez/archive/tags/C_2300_+3.0/default.aspx" /><category term="Project Euler" scheme="http://weblogs.asp.net/esanchez/archive/tags/Project+Euler/default.aspx" /></entry><entry><title>Finding the largest prime factor of a number</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/esanchez/archive/2008/04/19/finding-the-largest-prime-factor-of-a-number.aspx" /><id>http://weblogs.asp.net/esanchez/archive/2008/04/19/finding-the-largest-prime-factor-of-a-number.aspx</id><published>2008-04-19T05:40:00Z</published><updated>2008-04-19T05:40:00Z</updated><content type="html">&lt;P&gt;This is another classic problem at &lt;A class="" href="http://projecteuler.net/index.php?section=problems&amp;amp;id=3" mce_href="http://projecteuler.net/index.php?section=problems&amp;amp;id=3"&gt;Project Euler&lt;/A&gt; that can be solved with the old trick of top down programming, like so:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PrimeFactors(number).DefaultIfEmpty(number).Max();&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;It's a nice solution, supposing PrimeFactors() actually returns all prime factors of any given number. But before getting there, it's a good idea to discuss what DefaultIfEmpty() is doing there. The thing is that somebody could make this innocent call: PrimeFactors(1), which of course returns an empty collection, which in turn makes PrimeFactors(1).Max() throw an exception (trying to get the maximum out of nothing, eh?) DefaultIfEmpty(x) to the rescue: when DefaultIfEmpty(x) is applied to a "reasonable" collection it returns it untouched, but when it's applied to an empty collection it returns a new collection with just un item: x. So, in our case, PrimeFactors(1).DefaultIfEmpty(1).Max() is just a looooong alias for 1. DefaultIfEmpty() is very useful for those special cases with empty collections.&lt;/P&gt;
&lt;P&gt;With that behind, let's see how we get all the prime factors of a number:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IEnumerable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;ulong&lt;/SPAN&gt;&amp;gt; PrimeFactors(&lt;SPAN style="COLOR: blue"&gt;ulong&lt;/SPAN&gt; number)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;ulong&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;ulong&lt;/SPAN&gt;&amp;gt; FindDivisor = n =&amp;gt; n == 1UL ? 1UL : CandidateFactors(n).First(c =&amp;gt; n % c == 0);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;var&lt;/SPAN&gt; factors = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;HashSet&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;ulong&lt;/SPAN&gt;&amp;gt;();&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 6&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;while&lt;/SPAN&gt; (number &amp;gt; 1UL)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 7&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8&lt;/SPAN&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; &lt;SPAN style="COLOR: blue"&gt;ulong&lt;/SPAN&gt; divisor = FindDivisor(number);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 9&lt;/SPAN&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; factors.Add(divisor);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp; 10&lt;/SPAN&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; number /= divisor;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp; 11&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp; 12&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp; 13&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; factors;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp; 14&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;At line 3 we define a function (we are using lambdas just for fun) that finds a divisor for a number, for example FindDivisor(45) returns 3 and FindDivisor(49) returns 7. The workhorse is the while loop starting on line 6, it starts with a number, let's say 45, find the divisor 3, saves it in factors and goes on with 15 (45 / 3), now it finds a divisor for 15, in this case 3 again, saves it in factors and goes on with the 5 (15 / 3), and so on and so on until all is left is a 1. Meanwhile factors has been accumulating 3, 5, etc.: the prime factors of the original number. An interesting detail is that factors is a set, so it doesn't have any duplicates.&lt;/P&gt;
&lt;P&gt;Finally let's check the CandidateFactors() definition:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IEnumerable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;ulong&lt;/SPAN&gt;&amp;gt; CandidateFactors(&lt;SPAN style="COLOR: blue"&gt;ulong&lt;/SPAN&gt; n)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;yield&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; 2UL;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;for&lt;/SPAN&gt; (&lt;SPAN style="COLOR: blue"&gt;ulong&lt;/SPAN&gt; candidate = 3UL; candidate &amp;lt;= n; candidate += 2UL)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5&lt;/SPAN&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; &lt;SPAN style="COLOR: blue"&gt;yield&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; candidate;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 6&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;As you can see, this function simply produces the sequence 2, 3, 5, 7, 9, ..., n which are the possible divisors of n. You can think of a more clever implementation, one that produces a less indiscriminate list of candidates, but let's leave that for another day.&lt;/P&gt;
&lt;P&gt;And in this way, this mixture of LINQ (DefaultIsEmpty(), Max(), First()), lambdas (FindDivisor), iterators (yield) and good old imperative programming, leads us to a compact and effective solution to an old problem. Do you happen to have a more elegant solution?&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6111883" width="1" height="1"&gt;</content><author><name>Edgar Sánchez</name><uri>http://weblogs.asp.net/members/Edgar-S_E100_nchez.aspx</uri></author><category term="Functional Programming" scheme="http://weblogs.asp.net/esanchez/archive/tags/Functional+Programming/default.aspx" /><category term="LINQ" scheme="http://weblogs.asp.net/esanchez/archive/tags/LINQ/default.aspx" /><category term="Math" scheme="http://weblogs.asp.net/esanchez/archive/tags/Math/default.aspx" /><category term="C# 3.0" scheme="http://weblogs.asp.net/esanchez/archive/tags/C_2300_+3.0/default.aspx" /></entry><entry><title>Project Euler and infinite sequences in C#</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/esanchez/archive/2008/04/17/project-euler-and-infinite-sequences-in-c.aspx" /><id>http://weblogs.asp.net/esanchez/archive/2008/04/17/project-euler-and-infinite-sequences-in-c.aspx</id><published>2008-04-17T08:10:25Z</published><updated>2008-04-17T08:10:25Z</updated><content type="html">&lt;p&gt;The &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=2" target="_blank"&gt;second problem at Project Euler&lt;/a&gt; proposes:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:&lt;/p&gt;    &lt;p&gt;1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...&lt;/p&gt;    &lt;p&gt;Find the sum of all the even-valued terms in the sequence which do not exceed four million.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;One way of solving this problem is by defining an endless sequence in C#. Something along these lines:&lt;/p&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; FibonacciSequence()&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;int&lt;/span&gt; n1 = 0;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;int&lt;/span&gt; n2 = 1;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;while&lt;/span&gt; (&lt;span style="color: blue"&gt;true&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;int&lt;/span&gt; n3 = n1 + n2;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;yield&lt;/span&gt; &lt;span style="color: blue"&gt;return&lt;/span&gt; n3;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; n1 = n2;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; n2 = n3;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;The FibonacciSequence() function returns the next value in the sequence everytime its execution hits the &lt;span style="color: blue"&gt;yield&lt;/span&gt; statement, and conveniently enough, n3 gets the values 1, 2, 3, 5, 8, ... in each next round of the loop. It's notable that the loop never finishes by itself, this responsability actually belongs to the code invoking FibonacciSequence(). So in fact we are seeing a way of representing an infinite set (all Fibonacci numbers) in C#. In our case we need all elements no bigger than 4'000.000, so we can write something like this:&lt;/p&gt;  &lt;blockquote&gt;   &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;     &lt;p style="margin: 0px"&gt;FibonacciSequence().TakeWhile(n =&amp;gt; n &amp;lt;= 4000000).Where(n =&amp;gt; n % 2 == 0).Sum()&lt;/p&gt;   &lt;/div&gt; &lt;/blockquote&gt;  &lt;p&gt;The TakeWhile() method stops asking for more terms as soon as an item bigger than four millions is produced, afterwards the Where() method leaves us with a sequence containing only the even terms, and these terms are finally added up by Sum().&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6105395" width="1" height="1"&gt;</content><author><name>Edgar Sánchez</name><uri>http://weblogs.asp.net/members/Edgar-S_E100_nchez.aspx</uri></author><category term="Functional Programming" scheme="http://weblogs.asp.net/esanchez/archive/tags/Functional+Programming/default.aspx" /><category term="LINQ" scheme="http://weblogs.asp.net/esanchez/archive/tags/LINQ/default.aspx" /><category term="C# 3.0" scheme="http://weblogs.asp.net/esanchez/archive/tags/C_2300_+3.0/default.aspx" /></entry><entry><title>Project Euler and functional C#</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/esanchez/archive/2008/04/16/project-euler-and-functional-c.aspx" /><id>http://weblogs.asp.net/esanchez/archive/2008/04/16/project-euler-and-functional-c.aspx</id><published>2008-04-16T06:59:19Z</published><updated>2008-04-16T06:59:19Z</updated><content type="html">&lt;p&gt;I think I already talked (a long time ago) about &lt;a href="http://projecteuler.net/" target="_blank"&gt;Project Euler&lt;/a&gt;: a set of problems, mainly math oriented, keen to be solved with a computer program. To be sure, it's not particularly deep math, but the problems posed go fairly quickly from really easy, to hmm, to challenging. In this sense, they are good programming calisthenics, and may also be a good way of learning a new language. For example, &lt;a href="http://blogs.msdn.com/chrsmith/archive/tags/Project+Euler/default.aspx" target="_blank"&gt;Chris Smith has solved some Project Euler problems using F#&lt;/a&gt;, I find the idea intriguing, so I'll try to do the same thing, only that I'll be using C#, but not your plain old C# mind you, as I'll do my best to use in as much as possible the functional facilities of C# 3.0. Let's see for example Problem 1:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Add all the natural numbers below one thousand that are multiples of 3 or 5.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Ok, ok, the problem is &lt;strike&gt;silly&lt;/strike&gt; trivial, but wait until you get to Problem 10 ;-). One C# 3.0 functional solution is:&lt;/p&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Enumerable&lt;/span&gt;.Range(1, 999).Where(n =&amp;gt; n % 3 == 0 || n % 5 == 0).Sum()&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Range() generates the sequence from 1 to 999, and then Where() filters it leaving just the numbers multiples of 3 or 5, finally Sum() adds up these numbers. Easy, isn't it? And without loops or ifs.&lt;/p&gt;  &lt;p&gt;Obviously, not all problems lend so naturally to be solved in a functional way, and we'll find out step by step how useful is the functional paradigm in Project Euler.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6103031" width="1" height="1"&gt;</content><author><name>Edgar Sánchez</name><uri>http://weblogs.asp.net/members/Edgar-S_E100_nchez.aspx</uri></author><category term="Functional Programming" scheme="http://weblogs.asp.net/esanchez/archive/tags/Functional+Programming/default.aspx" /><category term="F#" scheme="http://weblogs.asp.net/esanchez/archive/tags/F_2300_/default.aspx" /><category term="C# 3.0" scheme="http://weblogs.asp.net/esanchez/archive/tags/C_2300_+3.0/default.aspx" /><category term="WideOpen Web" scheme="http://weblogs.asp.net/esanchez/archive/tags/WideOpen+Web/default.aspx" /></entry><entry><title>Eight Silverlight training sessions</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/esanchez/archive/2008/01/07/eight-silverlight-training-sessions.aspx" /><id>http://weblogs.asp.net/esanchez/archive/2008/01/07/eight-silverlight-training-sessions.aspx</id><published>2008-01-07T05:20:21Z</published><updated>2008-01-07T05:20:21Z</updated><content type="html">&lt;p&gt;A little bit of &lt;a href="http://adamkinney.com/blog/291/default.aspx" target="_blank"&gt;cross-posting&lt;/a&gt; but I find this interesting enough as not to risk you miss it...&lt;/p&gt; &lt;p&gt;&lt;a title="Silverlight 1.0 FireStarter" href="http://visitmix.com/University/silverlight/firestarter/" target="_blank"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="480" alt="image" src="http://weblogs.asp.net/blogs/esanchez/WindowsLiveWriter/OchosesionesdeentrenamientodeSilverlight_14ED8/image_3.png" width="644" border="0"&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;&lt;a href="http://www.silverlight.net" target="_blank"&gt;Silverlight&lt;/a&gt; 1.0 FireStarter is a one day event, targeting developers and designers, that teaches the why's and how's of Silverlight 1.0, a total of eight very interesting sessions:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;An Introduction to Silverlight  &lt;li&gt;Getting Started Programming Silverlight &lt;li&gt;Workflow of Silverlight with &lt;a title="Microsoft Expression" href="http://www.microsoft.com/expression/" target="_blank"&gt;Expression&lt;/a&gt; and Visual Studio &lt;li&gt;Silverlight XAML  &lt;li&gt;A Development Story &lt;li&gt;Media, Markers and More &lt;li&gt;&lt;a href="http://www.popfly.ms/Home.aspx" target="_blank"&gt;Popfly&lt;/a&gt; and Silverlight  &lt;li&gt;Silverlight in the Future&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Now, the really nice thing is that&amp;nbsp; &lt;a href="http://visitmix.com/University/silverlight/firestarter/" target="_blank"&gt;you can see and download the videos&lt;/a&gt; of each presentation. Ah, you can even &lt;a href="http://adamkinney.com/downloads/firestartersite.zip" target="_blank"&gt;download the source code&lt;/a&gt;. Now, all I need is 8 free hours...&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5562296" width="1" height="1"&gt;</content><author><name>Edgar Sánchez</name><uri>http://weblogs.asp.net/members/Edgar-S_E100_nchez.aspx</uri></author><category term=".NET" scheme="http://weblogs.asp.net/esanchez/archive/tags/.NET/default.aspx" /><category term="Popfly" scheme="http://weblogs.asp.net/esanchez/archive/tags/Popfly/default.aspx" /><category term="Expression" scheme="http://weblogs.asp.net/esanchez/archive/tags/Expression/default.aspx" /><category term="Silverlight" scheme="http://weblogs.asp.net/esanchez/archive/tags/Silverlight/default.aspx" /></entry><entry><title>Tuples: an F# lightweight data structure</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/esanchez/archive/2007/12/05/tuples-an-f-lightweight-data-structure.aspx" /><id>http://weblogs.asp.net/esanchez/archive/2007/12/05/tuples-an-f-lightweight-data-structure.aspx</id><published>2007-12-05T05:24:01Z</published><updated>2007-12-05T05:24:01Z</updated><content type="html">&lt;p&gt;Maybe the best thing to do is to start by showing a few examples of tuples: &lt;/p&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;let&lt;/span&gt; amigo = (&lt;span style="color: #a31515"&gt;&amp;quot;Sebasti&amp;#225;n&amp;quot;&lt;/span&gt;, 7)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;let&lt;/span&gt; pair1 = (3, 2)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;let&lt;/span&gt; pair2 = (2, 5)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;let&lt;/span&gt; student = &lt;span style="color: #a31515"&gt;&amp;quot;Santiago&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Monterrey&amp;quot;&lt;/span&gt;, 2007, &lt;span style="color: blue"&gt;true&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;The first tuple (&amp;quot;Sebasti&amp;#225;n&amp;quot;, 7) groups two values: a string and an integer, allowing us to handle them as just one thing, this &amp;quot;thing&amp;quot; has the type string * int, that is, it belongs to the set of all the combinations of strings and integers, in this sense (&amp;quot;Sebasti&amp;#225;n&amp;quot;, 7) has the same type of (&amp;quot;Alejandra&amp;quot;, 19). By the way, note how we separate the different elements of the tuple with commas.&lt;/p&gt;  &lt;p&gt;Tuples pair1 and pair2 are of type (make a wild guess...) int * int. Strictly speaking, &amp;quot;&lt;span style="color: blue"&gt;let&lt;/span&gt; pair1 = (3, 2)&amp;quot; defines a function pair1 that always returns the same tuple (3, 2), that is, it's a constant function that takes 0 parameters.&lt;/p&gt;  &lt;p&gt;Finally, the student tuple is of type string * string * int * bool, the fact that I have omitted the parenthesis is not a mistake, tuples actually don't need them and many F# programmers don't put them (even though, I personally prefer to write them down, to remind myself that what I am seeing is something to be treated as a whole).&lt;/p&gt;  &lt;p&gt;With these tuples in our hands, we can define some functions: &lt;/p&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;let&lt;/span&gt; addPairs (x1,y1) (x2, y2) = x1 + x2, y1 + y2&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;let&lt;/span&gt; primero (p, q) = p&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;let&lt;/span&gt; getYear (name, city, year, status) = year&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;let&lt;/span&gt; getYearFunny (_, _, year, _) = year&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;addPairs is a function that receives two tuples and returns another (to be precise, its type is int * int -&amp;gt; int * int -&amp;gt; int * int). Just to get used to it, I omitted the parenthesis in the function body (but I insist: I don't think it's a good idea).&lt;/p&gt;  &lt;p&gt;The function primero is even more interesting: its type is... 'a * 'b -&amp;gt; 'a, which means that it receives a tuple containing some stuff and some other stuff and returns something of the same type of the first stuff. Maybe we expected the type to be something like int * int -&amp;gt; int, but that would have unnecessarily limited the usability of the function. For example, we can now say &amp;quot;primero amigo&amp;quot; and get the string &amp;quot;Santiago&amp;quot; or &amp;quot;primero pair1&amp;quot; and get the integer 3. If you think about it, the use of 'a and 'b is very similar to the use of generics in C# or VB.NET. It's utterly important to note that primero does *not* receive two parameters p and q but just one parameter, namely a two-value tuple, and then the function body tells that what must be returned is the first of those two values, this is a simple example of F# pattern matching. By the way, it's really not necessary to write the primero function, because F# already provides us with fst and snd.&lt;/p&gt;  &lt;p&gt;A somewhat more complex example of pattern matching is getYear, which gets a tuple of type 'a * 'b * 'c * 'd and returns a 'c. &amp;quot;getYear student&amp;quot; of course returns 2007 (what is returned by getYear (&amp;quot;William&amp;quot;, &amp;quot;Jones&amp;quot;, 1706,&amp;#160; 3.1416)? Is this even possible?). It's interesting to note that even though in getYear definition we name the four elements of the tuple, we use just one of them, which takes us to the definition of getYearFunny: the &amp;quot;_&amp;quot; symbol (underscore) basically means &amp;quot;an element whose value I don't care about&amp;quot;, and so getYearFunny ignores the first, second, and fourth values in the tuple. This is our first encounter with this &amp;quot;_&amp;quot; guy and we'll find out that it appears frequently and that it's very useful when doing pattern matching.&lt;/p&gt;  &lt;p&gt;Finally, let's mention a few more characteristics and uses of tuples:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;A tuple have a fixed number of elements: be them two, three or thirty, once set you can't add or remove elements from a tuple&lt;/li&gt;    &lt;li&gt;The elements in a tuple doesn't have a name, you access them by their position in the tuple with the help of pattern matching&lt;/li&gt;    &lt;li&gt;It's almost redundant to say, but a tuple may contain other tuples, e.g. what's the value of fst (snd (&amp;quot;Santiago&amp;quot; (9, 10))) ? Do we need all the parenthesis?&lt;/li&gt;    &lt;li&gt;A tuple is a handy way of making a function return multiple values (as the original getYear does) or to pass multiple values to a function that expects only one parameter&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Tuples are a basic and simple component of F#, built to represent a fixed-size sequential set of values.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5403759" width="1" height="1"&gt;</content><author><name>Edgar Sánchez</name><uri>http://weblogs.asp.net/members/Edgar-S_E100_nchez.aspx</uri></author><category term="Functional Programming" scheme="http://weblogs.asp.net/esanchez/archive/tags/Functional+Programming/default.aspx" /><category term=".NET" scheme="http://weblogs.asp.net/esanchez/archive/tags/.NET/default.aspx" /><category term="F#" scheme="http://weblogs.asp.net/esanchez/archive/tags/F_2300_/default.aspx" /><category term="Programming Languages" scheme="http://weblogs.asp.net/esanchez/archive/tags/Programming+Languages/default.aspx" /></entry><entry><title>F# doesn't need parenthesis in functions, or does it?</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/esanchez/archive/2007/12/02/f-doesn-t-need-parenthesis-in-functions-or-does-it.aspx" /><id>http://weblogs.asp.net/esanchez/archive/2007/12/02/f-doesn-t-need-parenthesis-in-functions-or-does-it.aspx</id><published>2007-12-03T02:09:18Z</published><updated>2007-12-03T02:09:18Z</updated><content type="html">&lt;p&gt;In a &lt;a title="F# basic function definition syntax" href="http://weblogs.asp.net/esanchez/archive/2007/11/28/FSharp-basic-function-definition-syntax.aspx" target="_blank"&gt;previous posting&lt;/a&gt; I mentioned that when you define a function in F# you don't need parenthesis surrounding the parameters, or commas separating them. So, this simple definitions are all valid:&lt;/p&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;let&lt;/span&gt; negate a = -a&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;let&lt;/span&gt; add a b = a + b&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;let&lt;/span&gt; multiply a b = a * b&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;When you compile them, you get this messages:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/esanchez/WindowsLiveWriter/Fnousaparntesisenlasfuncionesos_12986/image_4.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="56" alt="image" src="http://weblogs.asp.net/blogs/esanchez/WindowsLiveWriter/Fnousaparntesisenlasfuncionesos_12986/image_thumb_1.png" width="244" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;negate is a function that takes an integer and returns an integer, add takes two integers and return another, etc.&lt;/p&gt;  &lt;p&gt;Now we can type &amp;quot;add 3 4&amp;quot; and we get a 7, but things get interesting if we try to negate the &amp;quot;add 3 4&amp;quot;:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/esanchez/WindowsLiveWriter/Fnousaparntesisenlasfuncionesos_12986/image_8.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="155" alt="image" src="http://weblogs.asp.net/blogs/esanchez/WindowsLiveWriter/Fnousaparntesisenlasfuncionesos_12986/image_thumb_3.png" width="489" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Why? To solve this mystery we have to understand that function application (i.e. invocation or calling) is an operation as well as +, *, or -. As other operations, application has a precedence (which operation is called first when you've got a mixture) and associativity (if you have &amp;quot;a + b + c&amp;quot;, does this mean &amp;quot;(a + b) + c&amp;quot; or &amp;quot;a + (b + c)&amp;quot;). &lt;strong&gt;In F# function application has the highest precedence and it is left associative&lt;/strong&gt;, so that&lt;/p&gt;  &lt;p&gt;negate add 3 4&lt;/p&gt;  &lt;p&gt;is interpreted as &amp;quot;(negate add) 3 4&amp;quot; and not as &amp;quot;negate (add 3 4)&amp;quot;. It pops to the eye that &amp;quot;(negate add)&amp;quot; can't be right, but this is not because you can't pass a function as parameter in F# (which is not only possible but common and pretty useful) but because negate expects an integer as parameter and add is a function, not a number. Maybe the compiler message could be more explicit, but I hope the real source of the problem is more clear now. So, in order for F# to evaluate the expression in the expected order we should write:&lt;/p&gt;  &lt;p&gt;negate (add 3 4)&lt;/p&gt;  &lt;p&gt;And now, F# first adds 3 and 4, giving 7, and then negates the result giving -7. There are several other scenarios where for getting F# to do the evaluation in the adequate order we must use parenthesis, but remember you will be using them not because invocation requires them but because of precedence and associativity. After this discussion, I think it will be easier to understand the following results:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/esanchez/WindowsLiveWriter/Fnousaparntesisenlasfuncionesos_12986/image_10.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="472" alt="image" src="http://weblogs.asp.net/blogs/esanchez/WindowsLiveWriter/Fnousaparntesisenlasfuncionesos_12986/image_thumb_4.png" width="502" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Maybe the only unexpected expression is the last &amp;quot;(-1)&amp;quot;, once again the parenthesis are there because &amp;quot;-&amp;quot; is an operator and without them, the expression would be interpreted as &amp;quot;(multiply 3 -) 1&amp;quot;, and the compiler will shout because multiply expects two numbers and &amp;quot;-&amp;quot; is a function, not a number. This could be surprising at first (I bumped onto this very same issue with Haskell several years ago) but once you know why, it's not really a problem.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5395376" width="1" height="1"&gt;</content><author><name>Edgar Sánchez</name><uri>http://weblogs.asp.net/members/Edgar-S_E100_nchez.aspx</uri></author><category term="Functional Programming" scheme="http://weblogs.asp.net/esanchez/archive/tags/Functional+Programming/default.aspx" /><category term=".NET" scheme="http://weblogs.asp.net/esanchez/archive/tags/.NET/default.aspx" /><category term="F#" scheme="http://weblogs.asp.net/esanchez/archive/tags/F_2300_/default.aspx" /></entry><entry><title>Word 2007 and Microsoft Math</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/esanchez/archive/2007/12/02/word-2007-and-microsoft-math.aspx" /><id>http://weblogs.asp.net/esanchez/archive/2007/12/02/word-2007-and-microsoft-math.aspx</id><published>2007-12-02T19:22:00Z</published><updated>2007-12-02T19:22:00Z</updated><content type="html">&lt;P&gt;Reading&amp;nbsp;&lt;A class="" href="http://blogs.msdn.com/alfredth" target=_blank mce_href="http://blogs.msdn.com/alfredth"&gt;Alfred Thompson blog&lt;/A&gt;, who focus on computer science teaching at K-12 schools (what we call in &lt;A class="" href="http://en.wikipedia.org/wiki/Ecuador" target=_blank mce_href="http://en.wikipedia.org/wiki/Ecuador"&gt;Ecuador&lt;/A&gt; basic high school), I found this &lt;A class="" href="http://blogs.msdn.com/alfredth/archive/2007/11/30/math-add-in-for-microsoft-word.aspx" target=_blank mce_href="http://blogs.msdn.com/alfredth/archive/2007/11/30/math-add-in-for-microsoft-word.aspx"&gt;nice entry&lt;/A&gt;: Microsoft&amp;nbsp;released&amp;nbsp;a few&amp;nbsp;days ago the&amp;nbsp;&lt;A class="" href="http://www.microsoft.com/downloads/details.aspx?familyid=030fae9c-704f-48ca-971d-56241aefc764&amp;amp;displaylang=en&amp;amp;tm" target=_blank mce_href="http://www.microsoft.com/downloads/details.aspx?familyid=030fae9c-704f-48ca-971d-56241aefc764&amp;amp;displaylang=en&amp;amp;tm"&gt;Word 2007 Math Add-in&lt;/A&gt;. After installing and playing with it for just 2 minutes, I was able to create this document:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://ecuador.latindevelopers.net/photos/programadores_aprendiendo/picture1829.aspx" target=_blank mce_href="http://weblogs.asp.net/photos/programadores_aprendiendo/picture1829.aspx"&gt;&lt;IMG height=729 src="http://ecuador.latindevelopers.net/photos/programadores_aprendiendo/images/1829/original.aspx" width=868 border=0 mce_src="http://weblogs.asp.net/photos/programadores_aprendiendo/images/1829/original.aspx"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;As you can see, the add-in lets you put formulas inside the text (including fractions, exponentials, sigmas, integrals, etc.), create graphs from an equation (I just typed in y = ...), and even solve those equations. Ah, this is the kind of stuff I would've loved to have at high school or college.&lt;/P&gt;
&lt;TABLE class=""&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="" vAlign=top&gt;
&lt;P&gt;Speaking of which, the add-in seems to be based (I'm just especulating here) on &lt;A class="" href="http://www.microsoft.com/math/default.mspx" target=_blank mce_href="http://www.microsoft.com/math/default.mspx"&gt;Microsoft Math&lt;/A&gt;, an unexpensive&amp;nbsp;stand alone product I wasn't aware of,&amp;nbsp;that could be a nice present for a high school niece in love with physics or math.&lt;/P&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;IMG height=512 src="http://www.microsoft.com/Math/images/larger/ss_features_L.jpg" width=700 mce_src="http://www.microsoft.com/Math/images/larger/ss_features_L.jpg"&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5395090" width="1" height="1"&gt;</content><author><name>Edgar Sánchez</name><uri>http://weblogs.asp.net/members/Edgar-S_E100_nchez.aspx</uri></author><category term="Office" scheme="http://weblogs.asp.net/esanchez/archive/tags/Office/default.aspx" /><category term="Math" scheme="http://weblogs.asp.net/esanchez/archive/tags/Math/default.aspx" /></entry></feed>