<?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>2008-04-17T04:10:25Z</updated><entry><title>Distance between adjacent points in F#</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/esanchez/archive/2009/03/12/distance-between-adjacent-points-in-f.aspx" /><id>http://weblogs.asp.net/esanchez/archive/2009/03/12/distance-between-adjacent-points-in-f.aspx</id><published>2009-03-13T04:57:18Z</published><updated>2009-03-13T04:57:18Z</updated><content type="html">&lt;p&gt;Let’s say you are given a list of data points:&lt;/p&gt;  &lt;p&gt;[7;5;12;8;5]&lt;/p&gt;  &lt;p&gt;And you are asked to find the distance between every adjacent pair, that is:&lt;/p&gt;  &lt;p&gt;[(7-5);(5-12);(12-8);(8-5)] = [2;-7;4;3]&lt;/p&gt;  &lt;p&gt;It turns out that there is an elegant solution to this problem:&lt;/p&gt;  &lt;p&gt;let rec pairs = function   &lt;br /&gt;&amp;#160; | h1 :: (h2 :: _ as tail) -&amp;gt; (h1, h2) :: pairs tail    &lt;br /&gt;&amp;#160; | _ –&amp;gt; []&lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;let distances dataPoints =    &lt;br /&gt;&amp;#160; dataPoints |&amp;gt; pairs |&amp;gt; List.map (fun (a, b) -&amp;gt; a - b)&lt;/p&gt;  &lt;p&gt;The magic happens in the pairs function, this function takes [7;5;12;8;5] and turns it into [(7,5);(5,12);(12,8);(8,5)], that is, it creates a tuple with every member of the list and its right neighbor. The trick is that &lt;strong&gt;tail&lt;/strong&gt; gets bounded&amp;#160; to ( &lt;strong&gt;h2 :: _&lt;/strong&gt; ), so that the recursive call processes the list starting with h2. This is called a named subpattern, something I discovered in section 1.4.2.2 of &lt;a href="http://www.amazon.com/F-Scientists-Jon-Harrop/dp/0470242116/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1236919987&amp;amp;sr=8-1" target="_blank"&gt;F# for Scientists&lt;/a&gt;. You learn at least a nice thing every day!&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6957039" 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" /></entry><entry><title>Entity Framework, LINQ to SQL and Oracle</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/esanchez/archive/2008/11/17/entity-framework-linq-to-sql-and-oracle.aspx" /><id>http://weblogs.asp.net/esanchez/archive/2008/11/17/entity-framework-linq-to-sql-and-oracle.aspx</id><published>2008-11-18T05:24:23Z</published><updated>2008-11-18T05:24:23Z</updated><content type="html">&lt;p&gt;Amid the debate about which is better and have more future (two things that not necessarily go together) between LINQ to SQL and Entity Framework, one thing they have in common is the fact that Oracle is in “no comment” mode about both of them. It’s like Oracle would be expecting that the lack of its “official” provider for Entity Framework, let alone LINQ to SQL, would somehow move people to develop in Java instead of .NET Framework. IMHO, Visual Studio 2008 is so productive that people may first consider moving from Oracle to SQL Server before moving from VS 2008 to JDeveloper.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;a href="http://weblogs.asp.net/blogs/esanchez/image_0E20416F.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="68" alt="image" src="http://weblogs.asp.net/blogs/esanchez/image_thumb_554571B6.png" width="244" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Luckily, .NET Framework has a big ecosystem of developers and ISV’s: enter &lt;a href="http://www.devart.com/" target="_blank"&gt;Devart&lt;/a&gt;, a software house in Russia or Ukraine –I’m not sure. They’ve been offering for a while now an &lt;strong&gt;Entity Framework provider for Oracle&lt;/strong&gt;, I have had the chance to use it with Oracle 10g with good success. The good news is that a few days ago they &lt;a href="http://www.devart.com/news/2008/dotconnects500.html" target="_blank"&gt;released new versions of all of its providers&lt;/a&gt; (changing their names while at it), including &lt;a href="http://www.devart.com/dotconnect/oracle/" target="_blank"&gt;dotConnect for Oracle 5.00&lt;/a&gt;. Even more intriguing, &lt;strong&gt;this new version includes a LINQ to SQL provider for Oracle&lt;/strong&gt;, something supposedly so complex to do that it would have taken a long time before it even existed. To be fair, I haven’t already used this last provider, but the very fact that it’s available is exciting. Now Oracle friendly Visual Studio 2008 developers (no, that’s not an oxymoron at all) has two good paths to follow. Let the debate begin!&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6744911" 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="Visual Studio 2008" scheme="http://weblogs.asp.net/esanchez/archive/tags/Visual+Studio+2008/default.aspx" /><category term="LINQ" scheme="http://weblogs.asp.net/esanchez/archive/tags/LINQ/default.aspx" /><category term="Entity Framework" scheme="http://weblogs.asp.net/esanchez/archive/tags/Entity+Framework/default.aspx" /></entry><entry><title>Free F# libraries (well, almost)</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/esanchez/archive/2008/11/10/free-f-libraries-well-almost.aspx" /><id>http://weblogs.asp.net/esanchez/archive/2008/11/10/free-f-libraries-well-almost.aspx</id><published>2008-11-11T05:36:08Z</published><updated>2008-11-11T05:36:08Z</updated><content type="html">&lt;p&gt;In what was one of the very last &lt;a target="_blank" href="http://microsoftpdc.com/"&gt;PDC2008&lt;/a&gt; sessions, &lt;a target="_blank" href="http://blogs.msdn.com/lucabol/"&gt;Luca Bolognese&lt;/a&gt; did an encore presentation of F#, instead of trying to tell you what it was all about I invite you to &lt;a title="Introduction to F#" target="_blank" href="http://channel9.msdn.com/pdc2008/TL11/"&gt;watch the video&lt;/a&gt; (Luca is engaging and funny, and the session is so packed with information that one our will pass in no time). What I wanted to do is to talk about a couple of very interesting libraries, all written in F#, that Luca used in his demos:&lt;/p&gt;  &lt;p&gt;&lt;img style="margin: 0px 0px 0px 10px; display: inline" align="right" src="http://www.ffconsultancy.com/products/images/ffn.gif" /&gt; &lt;a target="_blank" href="http://www.ffconsultancy.com/products/fsharp_for_numerics/index.html"&gt;F# for Numerics&lt;/a&gt; offers a bunch of numerical analysis functions, things like matrix operations, integration and differentiation, statistical methods, maximization and minimization, Fourier transforms, you know, the stuff we all love about maths [:P].&lt;/p&gt;  &lt;p&gt;&lt;img style="margin: 0px 10px 0px 0px; display: inline" align="left" src="http://www.ffconsultancy.com/products/images/ffv.gif" /&gt; &lt;a target="_blank" href="http://www.ffconsultancy.com/products/fsharp_for_visualization/index.html"&gt;F# for Visualization&lt;/a&gt; allows us to visualize functions in 2D as well as 3D, including animations and PNG export. Believe me, the graphs really look good.&lt;/p&gt;  &lt;p&gt;If you are hesitant about this, &lt;a title="Jon Harrop&amp;#39;s F# news blog" target="_blank" href="http://www.ffconsultancy.com/products/fsharp_for_scientists/index.html"&gt;Jon Harrop&lt;/a&gt;, the man behind the libraries is offering free licenses of both libraries, well, with the usual banners and watermarks reminding you that you should really buy the real thing. Not that they are expensive either: you can get both by around US$ 100.&lt;/p&gt;  &lt;p&gt;Personally, I feel a really sweet smell from the very fact that these libraries exist: a great symptom of a language or technology readiness for the market is that libraries from third parties start to appear (as, for example, has happened in the last months with WPF, but that’s the matter for another blog entry…)&lt;/p&gt;  &lt;p&gt;While we are on the subject, and for those of you who are really intrigued by scientic applications, I strongly suggest you to take a look to &lt;a target="_blank" href="http://www.ffconsultancy.com/products/fsharp_for_scientists/index.html"&gt;F# for Scientists&lt;/a&gt;, the book where Jon tells us how to use F# in this field.&lt;/p&gt;  &lt;p&gt;&lt;img style="display: block; float: none; margin-left: auto; margin-right: auto" src="http://www.ffconsultancy.com/products/images/ffs.gif" /&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6729040" 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" /></entry><entry><title>Point distance, imperative vs. functional style</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/esanchez/archive/2008/09/18/point-distance-imperative-vs-functional-style.aspx" /><id>http://weblogs.asp.net/esanchez/archive/2008/09/18/point-distance-imperative-vs-functional-style.aspx</id><published>2008-09-19T03:58:34Z</published><updated>2008-09-19T03:58:34Z</updated><content type="html">&lt;p&gt;Let’s consider a &lt;strike&gt;silly&lt;/strike&gt; simple algebra problem: given a specific point and a set of several other points, find the closest point in the set to the given point. One C# solution is:&lt;/p&gt;  &lt;div style="font-size: 11pt; 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;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #8ac5ff"&gt;static&lt;/span&gt; &lt;span style="color: #8ac5ff"&gt;class&lt;/span&gt; &lt;span style="color: #64b1ff"&gt;PointMath&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; 2&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; 3&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;static&lt;/span&gt; &lt;span style="color: #8ac5ff"&gt;double&lt;/span&gt; Distance(&lt;span style="color: #64b1ff"&gt;Point&lt;/span&gt; p1, &lt;span style="color: #64b1ff"&gt;Point&lt;/span&gt; p2)&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;/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;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #8ac5ff"&gt;double&lt;/span&gt; xDist &lt;span style="color: silver"&gt;=&lt;/span&gt; p1&lt;span style="color: silver"&gt;.&lt;/span&gt;X &lt;span style="color: silver"&gt;-&lt;/span&gt; p2&lt;span style="color: silver"&gt;.&lt;/span&gt;X;&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;double&lt;/span&gt; yDist &lt;span style="color: silver"&gt;=&lt;/span&gt; p1&lt;span style="color: silver"&gt;.&lt;/span&gt;Y &lt;span style="color: silver"&gt;-&lt;/span&gt; p2&lt;span style="color: silver"&gt;.&lt;/span&gt;Y;&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; &lt;span style="color: #8ac5ff"&gt;return&lt;/span&gt; &lt;span style="color: #64b1ff"&gt;Math&lt;/span&gt;&lt;span style="color: silver"&gt;.&lt;/span&gt;Sqrt(xDist &lt;span style="color: silver"&gt;*&lt;/span&gt; xDist &lt;span style="color: silver"&gt;+&lt;/span&gt; yDist &lt;span style="color: silver"&gt;*&lt;/span&gt; yDist);&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; }&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;&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;span style="color: #8ac5ff"&gt;public&lt;/span&gt; &lt;span style="color: #8ac5ff"&gt;static&lt;/span&gt; &lt;span style="color: #64b1ff"&gt;Point&lt;/span&gt; ClosestPoint(&lt;span style="color: #64b1ff"&gt;Point&lt;/span&gt; p, &lt;span style="color: #ff6fb7"&gt;IList&lt;/span&gt;&lt;span style="color: silver"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #64b1ff"&gt;Point&lt;/span&gt;&lt;span style="color: silver"&gt;&amp;gt;&lt;/span&gt; points)&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;&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;&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;double&lt;/span&gt; shortestDistance &lt;span style="color: silver"&gt;=&lt;/span&gt; &lt;span style="color: #ffffaa"&gt;Double&lt;/span&gt;&lt;span style="color: silver"&gt;.&lt;/span&gt;PositiveInfinity;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&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;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #64b1ff"&gt;Point&lt;/span&gt; closestPoint &lt;span style="color: silver"&gt;=&lt;/span&gt; &lt;span style="color: #8ac5ff"&gt;null&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160; 14&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; 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; &lt;span style="color: #8ac5ff"&gt;foreach&lt;/span&gt; (&lt;span style="color: #8ac5ff"&gt;var&lt;/span&gt; point &lt;span style="color: #8ac5ff"&gt;in&lt;/span&gt; points)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&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;&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; 17&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;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #8ac5ff"&gt;double&lt;/span&gt; distance &lt;span style="color: silver"&gt;=&lt;/span&gt; Distance(p, point);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160; 18&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;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #8ac5ff"&gt;if&lt;/span&gt; (distance &lt;span style="color: silver"&gt;&amp;lt;&lt;/span&gt; shortestDistance)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160; 19&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;&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; 20&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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; shortestDistance &lt;span style="color: silver"&gt;=&lt;/span&gt; distance;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160; 21&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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; closestPoint &lt;span style="color: silver"&gt;=&lt;/span&gt; point;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160; 22&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;&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; 23&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;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160; 24&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; 25&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; closestPoint;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="background: #181818; color: #ffaa55"&gt;&amp;#160;&amp;#160; 26&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; 27&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;The Distance() function finds the distance between two points with good ol’ Pythagoras, the ClosestPoint() function does the classic loop: traverse the points list and calculate every distance, if you find a smaller one, keep it and the also keep the current point, at the end return the last point you kept. Easy, but with declarations, curly braces and whatnot, the solution takes 27 lines, OK, 14 lines if we ignore the blank lines and the curly-braces-only lines. And we didn’t even show the Point class definition… Can we do any better?&lt;/p&gt;  &lt;p&gt;What about this F# solution:&lt;/p&gt;  &lt;div style="font-size: 11pt; 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;span style="color: #8ac5ff"&gt;let&lt;/span&gt; distance (x1, y1) (x2, y2) : double =&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; &lt;span style="color: #8ac5ff"&gt;let&lt;/span&gt; xDistance = x1 - x2&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; &lt;span style="color: #8ac5ff"&gt;let&lt;/span&gt; yDistance = y1 - y2&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; sqrt (xDistance * xDistance + yDistance * yDistance)&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;&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;&lt;span style="color: #8ac5ff"&gt;let&lt;/span&gt; closestPoint toPoint fromPoints = List.min_by (distance toPoint) fromPoints&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The distance function is almost a clone of its C# cousin, the sexy one is the closestPoint function: just one line! Let’s try to dissect it a little bit:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;The List.min_by function expects two parameters: the last one is the list from where the minimum will be picked, the first one is the function that will be used to compare the items&lt;/li&gt;    &lt;li&gt;As I said, distance expects two parameters, but we are providing just one (toPoint), what are we accomplishing with this? Well, to begin with, the type of the distance function is (my apologies for the relaxed syntax) Point x Point –&amp;gt; double (i.e. distance takes two points and returns one double). If we fix one of those parameters (for example saying distance (0.5, 0.5) ), &lt;strong&gt;what we are actually doing is to define a new function&lt;/strong&gt; of type Point –&amp;gt; double, this new function only knows how to calculate distances to the point (0.5, 0.5). In our case, in line 6 we have created a function that knows how to calculate the distances to toPoint&lt;/li&gt;    &lt;li&gt;So, List_min_by finds the point in fromPoint closest to toPoint, calculate every distance to toPoint and keeping the fromPoints member with the shortest distance&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;I can hear some of you complaining about the fact that closestPoint may have taken just one line of code, but it took seven lines of explanation, but this is mainly because we are not used to the language, once you have familiarity with F#, the meaning of line 6 comes quite naturally. Any small imperative problem that you would like to see solved in a functional style?&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6633452" 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" /></entry><entry><title>The first Visual F# CTP is here!</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/esanchez/archive/2008/08/31/the-first-visual-f-ctp-is-here.aspx" /><id>http://weblogs.asp.net/esanchez/archive/2008/08/31/the-first-visual-f-ctp-is-here.aspx</id><published>2008-09-01T04:35:20Z</published><updated>2008-09-01T04:35:20Z</updated><content type="html">&lt;p&gt;You leave on vacation for one short week and a lot happens... for example, &lt;a href="http://blogs.msdn.com/dsyme/archive/2008/08/29/the-f-september-2008-ctp-is-now-available.aspx" target="_blank"&gt;Don Syme&lt;/a&gt; &amp;amp; co. have released &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=61ad6924-93ad-48dc-8c67-60f7e7803d3c&amp;amp;displaylang=en" target="_blank"&gt;the first F# CTP&lt;/a&gt;, well on the way (hopefully before this year's end) to put F# on the same level as C#, C++ or VB.NET. As far as I know, this will be an historical event: for the first time a mainstream platform (commercial or otherwise) wholly adopts a functional language. Allow me to seize the occasion to reiterate that there are several reasons for the functional programming paradigm to be considered &lt;strike&gt;interesting&lt;/strike&gt; important, IMHO the most relevant are:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;The usage of high level functions and lazy evaluation allows us to reach higher levels of abstraction and modularization, which eases the programming of the ever more complex problems that we face.&lt;/li&gt;    &lt;li&gt;The extensive use of immutable values and structures greatly paves the way to code execution parallelization, which is especially relevant in today's multi-core CPU world.&lt;/li&gt;    &lt;li&gt;Functional thinking adapts particularly well to the solution of math problems (be them symbolic or numeric). Well, this last point may not have as broad reach as the former, but it is especially fascinating and useful for mathematicians and scientists (which in more than a few cases have had to stick to FORTRAN or use very specialized products like Matlab or Mathematica).&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;For reasons like these, functional programming has steadily invaded the programming scenario, for example:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;C# 3.0 has lambda and higher level functions (or a subset, at least, sort of...) and lazy evaluation (LINQ anyone?)&lt;/li&gt;    &lt;li&gt;In the Java world, people is starting to consider &lt;a href="http://www.scala-lang.org/" target="_blank"&gt;Scala&lt;/a&gt;, a functional language for the JVM&lt;/li&gt;    &lt;li&gt;There's renewed interest in specialty languages like &lt;a href="http://erlang.org/about.html" target="_blank"&gt;Erlang&lt;/a&gt; (used at Ericsson to forge extremely scalable and reliable systems)&lt;/li&gt;    &lt;li&gt;&lt;a title="In my mind, F# is another first-class programming language on the CLR" href="http://blogs.msdn.com/somasegar/archive/2007/10/17/f-a-functional-programming-language.aspx" target="_blank"&gt;F# will be a first class citizen in the .NET Framework world&lt;/a&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;An example of the F# September CTP running on my Visual Studio 2008 SP1:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/esanchez/WindowsLiveWriter/ThefirstVisualFCTPishere_14837/FSharpSeptemberCTP_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="300" alt="FSharpSeptemberCTP" src="http://weblogs.asp.net/blogs/esanchez/WindowsLiveWriter/ThefirstVisualFCTPishere_14837/FSharpSeptemberCTP_thumb.png" width="768" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;If anybody is wondering what this code does, fibonacciSequence generates the Fibonacci numbers up to a given maximum, and the second function adds the even terms of the sequence up to a given limit. It's a succinct solution for &lt;a title="Hallar la suma de los elementos pares de la secuencia de Fibonacci que no excedan los cuatro millones" href="http://projecteuler.net/index.php?section=problems&amp;amp;id=2" target="_blank"&gt;Problem 2 @ Project Euler&lt;/a&gt; (and it would be interesting for you to try and solve it in your favorite language). It's all &lt;a href="http://en.wikipedia.org/wiki/Quechua" target="_blank"&gt;quechua&lt;/a&gt; to you? Well, that's just a matter of getting to know the language :-), IMHO the best way to learn F# is following the &lt;a href="http://www.amazon.com/gp/product/1590598504/ref=s9subs_c2_14_at2-rfc_g1?pf_rd_m=ATVPDKIKX0DER&amp;amp;pf_rd_s=center-1&amp;amp;pf_rd_r=15FC62DGY36Y70CGKX72&amp;amp;pf_rd_t=101&amp;amp;pf_rd_p=278240701&amp;amp;pf_rd_i=507846" target="_blank"&gt;Expert F#&lt;/a&gt; book, co-authored by the language's father himself. Furthermore, Microsoft has put online the language official site, the &lt;a href="http://msdn.microsoft.com/en-us/fsharp/default.aspx" target="_blank"&gt;F# Developer Center&lt;/a&gt;, where you will find several other resources.&lt;/p&gt;  &lt;p&gt;By the way, it seems like the official name of the release will be Visual F# 1.0 (which actually corresponds to Version 2, counting from its inception at &lt;a href="http://research.microsoft.com/fsharp/fsharp.aspx" target="_blank"&gt;Microsoft Research&lt;/a&gt;). Finally, Visual F# requires only .NET 2.0, and an intriguing consequence of this is that you will be able to run F# code on Linux, thanks to the &lt;a href="http://www.mono-project.com/Main_Page" target="_blank"&gt;Mono&lt;/a&gt;&amp;#160; project, that is, you will have an open source functional programming platform, courtesy from Microsoft (and Novell).&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6586217" 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="Visual Studio 2008" scheme="http://weblogs.asp.net/esanchez/archive/tags/Visual+Studio+2008/default.aspx" /><category term="Open source" scheme="http://weblogs.asp.net/esanchez/archive/tags/Open+source/default.aspx" /></entry><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></feed>