<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
  <channel>
    <title>Did it with .NET</title>
    <link>http://diditwith.net/</link>
    <description>Dustin Campbell &amp;mdash; Musings &amp;bull; Ramblings &amp;bull; Whatever...</description>
    <language>en-us</language>
    <copyright>Dustin Campbell</copyright>
    <lastBuildDate>Sat, 24 Oct 2009 18:01:40 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>blog@diditwith.net</managingEditor>
    <webMaster>blog@diditwith.net</webMaster>
    <creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license><image><url>http://www.feedburner.com/fb/images/pub/fb_pwrd.gif</url></image><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/DidItWithDotNet" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
      <trackback:ping>http://diditwith.net/Trackback.aspx?guid=0e316b70-08ab-4be4-9cb5-e017a3985c33</trackback:ping>
      <pingback:server>http://diditwith.net/pingback.aspx</pingback:server>
      <pingback:target>http://diditwith.net/PermaLink,guid,0e316b70-08ab-4be4-9cb5-e017a3985c33.aspx</pingback:target>
      <dc:creator>Dustin Campbell</dc:creator>
      <wfw:comment>http://diditwith.net/CommentView,guid,0e316b70-08ab-4be4-9cb5-e017a3985c33.aspx</wfw:comment>
      <wfw:commentRss>http://diditwith.net/SyndicationService.asmx/GetEntryCommentsRss?guid=0e316b70-08ab-4be4-9cb5-e017a3985c33</wfw:commentRss>
      <slash:comments>4</slash:comments>
      
      <title>Naming Anonymous Types with Generate from Usage</title>
      <guid isPermaLink="false">http://diditwith.net/PermaLink,guid,0e316b70-08ab-4be4-9cb5-e017a3985c33.aspx</guid>
      <link>http://feedproxy.google.com/~r/DidItWithDotNet/~3/pNBS2HsnjWo/NamingAnonymousTypesWithGenerateFromUsage.aspx</link>
      <pubDate>Sat, 24 Oct 2009 18:01:40 GMT</pubDate>
      <description>&lt;body xmlns="http://www.w3.org/1999/xhtml"&gt;&#xD;
        &lt;p&gt;&#xD;
Now that &lt;a href="http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx"&gt;Visual Studio&#xD;
2010 Beta 2&lt;/a&gt; is finally out the door, I’ve had a bit more time to spend coding&#xD;
on some of my personal projects. Yesterday, I happened upon a cool trick while using&#xD;
the new &lt;a href="http://msdn.microsoft.com/en-us/library/dd409796(VS.100).aspx"&gt;Generate&#xD;
from Usage&lt;/a&gt; feature. It was so helpful to me that I thought others might benefit,&#xD;
so I’m sharing it here.&#xD;
&lt;/p&gt;&#xD;
        &lt;h3&gt;The Anonymous Type Problem&#xD;
&lt;/h3&gt;&#xD;
        &lt;p&gt;&#xD;
When you need to project some data from a LINQ expression, &lt;a href="http://msdn.microsoft.com/en-us/library/bb387028.aspx"&gt;anonymous&#xD;
types&lt;/a&gt; can be enormously convenient.&#xD;
&lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
          &lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" title="C# Query" alt="C# Query" src="http://diditwith.net/content_images/NamingAnonymousTypes/CSharpQuery.png"&gt;&lt;/img&gt;&#xD;
        &lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
Because anonymous types are… well… &lt;em&gt;anonymous&lt;/em&gt;, they don’t have names that&#xD;
can be expressed in code. This is problematic if you want to expose an anonymous type&#xD;
as the return type of a function. I have run into this problem many times. When refactoring&#xD;
code, it’s easy to get into a situation like the one below.&#xD;
&lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
          &lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" title="Broken C# Function" alt="Broken C# Function" src="http://diditwith.net/content_images/NamingAnonymousTypes/CSharpFromFunction.png"&gt;&lt;/img&gt;&#xD;
        &lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
So, how can you get around this problem? Well, there a few possibilities.&#xD;
&lt;/p&gt;&#xD;
        &lt;ol&gt;&#xD;
          &lt;li&gt;&#xD;
You could replace ??? with object and use reflection to get at the properties. (Yuck!) &#xD;
&lt;/li&gt;&#xD;
          &lt;li&gt;&#xD;
You could make the function generic and add a parameter to “mumble” the anonymous&#xD;
type.&lt;sup&gt;1&lt;/sup&gt; (Awkward!) &#xD;
&lt;/li&gt;&#xD;
          &lt;li&gt;&#xD;
Assuming C# 4.0, you could replace ??? with dynamic.&lt;sup&gt;2&lt;/sup&gt; (No compiler errors!)&lt;/li&gt;&#xD;
        &lt;/ol&gt;&#xD;
        &lt;p&gt;&#xD;
Because none of these solutions is particularly savory, most of us are forced to create&#xD;
a new named type to replace the anonymous type. Thankfully, there are some fantastic&#xD;
third-party refactoring tools out there that can automate this tedious process, but&#xD;
if you don’t use one of these tools you’re stuck writing the code by hand.&#xD;
&lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
Actually, no, that’s not quite true.&#xD;
&lt;/p&gt;&#xD;
        &lt;h3&gt;Generate from Usage to The Rescue!&#xD;
&lt;/h3&gt;&#xD;
        &lt;p&gt;&#xD;
In Visual Studio 2010, the new Generate from Usage feature makes the task of coding&#xD;
up new classes a snap! Just type a new name for the anonymous type in the editor,&#xD;
making the code look like a type constructor followed by an &lt;a href="http://msdn.microsoft.com/en-us/library/bb397680.aspx"&gt;object&#xD;
initializer&lt;/a&gt;. Then, press Ctrl+. to expand the smart tag that immediately appears&#xD;
and choose the first suggestion to generate a new class.&#xD;
&lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
          &lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" title="Generate Class" alt="Generate Class" src="http://diditwith.net/content_images/NamingAnonymousTypes/GenerateClass.png"&gt;&lt;/img&gt;&#xD;
        &lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
Next, expand each smart tag in the object initializer to generate each property.&#xD;
&lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
          &lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" title="Generate Property" alt="Generate Property" src="http://diditwith.net/content_images/NamingAnonymousTypes/GenerateProperty.png"&gt;&lt;/img&gt;&#xD;
        &lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
When you’re finished, you should have a brand new class containing each property,&#xD;
declared as &lt;a href="http://msdn.microsoft.com/en-us/library/bb384054.aspx"&gt;auto-implemented&#xD;
properties&lt;/a&gt;. Cool!&#xD;
&lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
          &lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" title="Generated Class" alt="Generated Class" src="http://diditwith.net/content_images/NamingAnonymousTypes/CSharpGeneratedClass.png"&gt;&lt;/img&gt;&#xD;
        &lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
For Visual Basic coders, Generate from Usage is even easier. Let’s start with the&#xD;
same LINQ expression in VB. (Notice the lack of the “_” line continuation characters.&#xD;
Hooray for VB10 &lt;a href="http://blogs.msdn.com/vbteam/archive/2009/03/27/implicit-line-continuation-in-vb-10-tyler-whitney.aspx"&gt;implicit&#xD;
line continuation&lt;/a&gt;!)&#xD;
&lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
          &lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" title="VB Query" alt="VB Query" src="http://diditwith.net/content_images/NamingAnonymousTypes/VBQuery.png"&gt;&lt;/img&gt;&#xD;
        &lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
Just like before, type the name of the new type that you wish to generate and press&#xD;
Ctrl+. to expand the smart tag. After choosing the first suggestion from the smart&#xD;
tag, you’re finished. The VB Generate Class feature will drill into the object initializer&#xD;
and generate all of the necessary properties at the same time that the class is generated.&#xD;
&lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
          &lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" title="VB Generate Class" alt="VB Generate Class" src="http://diditwith.net/content_images/NamingAnonymousTypes/VBGenerateClass.png"&gt;&lt;/img&gt;&#xD;
        &lt;/p&gt;&#xD;
        &lt;h3&gt;Wrapping Up&#xD;
&lt;/h3&gt;&#xD;
        &lt;p&gt;&#xD;
Of course, this technique is not without flaws.&#xD;
&lt;/p&gt;&#xD;
        &lt;ul&gt;&#xD;
          &lt;li&gt;&#xD;
The resulting type is not immutable like the anonymous type that you’re replacing.&#xD;
To address this, you can easily modify the generated properties to be read-only. &#xD;
&lt;/li&gt;&#xD;
          &lt;li&gt;&#xD;
The new type does not have the same structural equality semantics that anonymous types&#xD;
have. In practice, I’ve rarely run into an bug caused by anonymous type structural&#xD;
equality, but if this is a concern for you, use one of the &lt;a href="http://diditwith.net/2007/12/22/TwelveDaysOfRefactorXmasDayThreeNameAnonymousType.aspx"&gt;excellent&#xD;
third-party tools&lt;/a&gt; that account for these differences.&lt;/li&gt;&#xD;
        &lt;/ul&gt;&#xD;
        &lt;p&gt;&#xD;
 &#xD;
&lt;/p&gt;&#xD;
        &lt;p style="border-top: rgb(221,221,221) 1px solid"&gt;&#xD;
          &lt;sup&gt;1&lt;/sup&gt;See Wes Dyer's excellent &lt;a href="http://blogs.msdn.com/wesdyer/archive/2007/02/11/baby-names-nameless-keys-and-mumbling.aspx"&gt;article&lt;/a&gt; for&#xD;
an example of this clever trick.&lt;br&gt;&lt;sup&gt;2&lt;/sup&gt;Check out Bill Wagner's &lt;a href="http://srtsolutions.com/blogs/billwagner/archive/2008/11/11/duck-typing-in-c-4-0.aspx"&gt;post&lt;/a&gt; for&#xD;
details.&#xD;
&lt;/p&gt;&#xD;
        &lt;img width="0" height="0" src="http://diditwith.net/aggbug.ashx?id=0e316b70-08ab-4be4-9cb5-e017a3985c33"&gt;&lt;/img&gt;&#xD;
      &lt;/body&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=pNBS2HsnjWo:FGgPZYDEVjk:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=pNBS2HsnjWo:FGgPZYDEVjk:G79ilh31hkQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=G79ilh31hkQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=pNBS2HsnjWo:FGgPZYDEVjk:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=pNBS2HsnjWo:FGgPZYDEVjk:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?i=pNBS2HsnjWo:FGgPZYDEVjk:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=pNBS2HsnjWo:FGgPZYDEVjk:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?i=pNBS2HsnjWo:FGgPZYDEVjk:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=pNBS2HsnjWo:FGgPZYDEVjk:cGdyc7Q-1BI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=cGdyc7Q-1BI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=pNBS2HsnjWo:FGgPZYDEVjk:7qCK26UlVX8"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=7qCK26UlVX8" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=pNBS2HsnjWo:FGgPZYDEVjk:cTv1dNCI_Tc"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=cTv1dNCI_Tc" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/DidItWithDotNet/~4/pNBS2HsnjWo" height="1" width="1"/&gt;</description>
      <comments>http://diditwith.net/CommentView,guid,0e316b70-08ab-4be4-9cb5-e017a3985c33.aspx</comments>
      <category>C#</category>
      <category>Tips &amp; Tricks</category>
      <category>Visual Basic</category>
      <category>Visual Studio</category>
    <feedburner:origLink>http://diditwith.net/2009/10/24/NamingAnonymousTypesWithGenerateFromUsage.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://diditwith.net/Trackback.aspx?guid=eb6b7d27-1ede-4f32-9622-976a9f09d178</trackback:ping>
      <pingback:server>http://diditwith.net/pingback.aspx</pingback:server>
      <pingback:target>http://diditwith.net/PermaLink,guid,eb6b7d27-1ede-4f32-9622-976a9f09d178.aspx</pingback:target>
      <dc:creator>Dustin Campbell</dc:creator>
      <wfw:comment>http://diditwith.net/CommentView,guid,eb6b7d27-1ede-4f32-9622-976a9f09d178.aspx</wfw:comment>
      <wfw:commentRss>http://diditwith.net/SyndicationService.asmx/GetEntryCommentsRss?guid=eb6b7d27-1ede-4f32-9622-976a9f09d178</wfw:commentRss>
      <slash:comments>2</slash:comments>
      
      <title>A Lover of Monkeys</title>
      <guid isPermaLink="false">http://diditwith.net/PermaLink,guid,eb6b7d27-1ede-4f32-9622-976a9f09d178.aspx</guid>
      <link>http://feedproxy.google.com/~r/DidItWithDotNet/~3/EzuwryoSO70/ALoverOfMonkeys.aspx</link>
      <pubDate>Fri, 26 Jun 2009 04:07:05 GMT</pubDate>
      <description>&lt;body xmlns="http://www.w3.org/1999/xhtml"&gt;&#xD;
        &lt;p&gt;&#xD;
The other day, I caught a quick snapshot of &lt;a href="http://diditwith.net/2007/12/10/ItsAGirl.aspx"&gt;Bethany&lt;/a&gt; playing&#xD;
with her favorite new toy:&#xD;
&lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
          &lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" src="http://diditwith.net/content_images/Monkey.jpg"&gt;&lt;/img&gt;&#xD;
        &lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
 &#xD;
&lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
          &lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" src="http://diditwith.net/content_images/Monkey2.jpg"&gt;&lt;/img&gt;&#xD;
        &lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
A special thanks to my friend &lt;a href="http://beyondfocus.com/"&gt;Joseph Hill&lt;/a&gt; for&#xD;
providing her favorite monkey!&#xD;
&lt;/p&gt;&#xD;
        &lt;img width="0" height="0" src="http://diditwith.net/aggbug.ashx?id=eb6b7d27-1ede-4f32-9622-976a9f09d178"&gt;&lt;/img&gt;&#xD;
      &lt;/body&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=EzuwryoSO70:K-M2sXW_6yA:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=EzuwryoSO70:K-M2sXW_6yA:G79ilh31hkQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=G79ilh31hkQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=EzuwryoSO70:K-M2sXW_6yA:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=EzuwryoSO70:K-M2sXW_6yA:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?i=EzuwryoSO70:K-M2sXW_6yA:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=EzuwryoSO70:K-M2sXW_6yA:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?i=EzuwryoSO70:K-M2sXW_6yA:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=EzuwryoSO70:K-M2sXW_6yA:cGdyc7Q-1BI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=cGdyc7Q-1BI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=EzuwryoSO70:K-M2sXW_6yA:7qCK26UlVX8"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=7qCK26UlVX8" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=EzuwryoSO70:K-M2sXW_6yA:cTv1dNCI_Tc"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=cTv1dNCI_Tc" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/DidItWithDotNet/~4/EzuwryoSO70" height="1" width="1"/&gt;</description>
      <comments>http://diditwith.net/CommentView,guid,eb6b7d27-1ede-4f32-9622-976a9f09d178.aspx</comments>
      <category>Feeble Attempts At Humor</category>
      <category>It's all about me</category>
    <feedburner:origLink>http://diditwith.net/2009/06/26/ALoverOfMonkeys.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://diditwith.net/Trackback.aspx?guid=adf10d38-3d3e-4341-ad41-11618d5d622b</trackback:ping>
      <pingback:server>http://diditwith.net/pingback.aspx</pingback:server>
      <pingback:target>http://diditwith.net/PermaLink,guid,adf10d38-3d3e-4341-ad41-11618d5d622b.aspx</pingback:target>
      <dc:creator>Dustin Campbell</dc:creator>
      <wfw:comment>http://diditwith.net/CommentView,guid,adf10d38-3d3e-4341-ad41-11618d5d622b.aspx</wfw:comment>
      <wfw:commentRss>http://diditwith.net/SyndicationService.asmx/GetEntryCommentsRss?guid=adf10d38-3d3e-4341-ad41-11618d5d622b</wfw:commentRss>
      <slash:comments>1</slash:comments>
      
      <title>CodeRush Xpress for C# and VB is Live!</title>
      <guid isPermaLink="false">http://diditwith.net/PermaLink,guid,adf10d38-3d3e-4341-ad41-11618d5d622b.aspx</guid>
      <link>http://feedproxy.google.com/~r/DidItWithDotNet/~3/3fHZMff0D4w/CodeRushXpressForCAndVBIsLive.aspx</link>
      <pubDate>Fri, 15 May 2009 00:59:29 GMT</pubDate>
      <description>&lt;body xmlns="http://www.w3.org/1999/xhtml"&gt;&#xD;
        &lt;p&gt;&#xD;
Last October, &lt;a href="http://devexpress.com"&gt;DevExpress&lt;/a&gt; released a massively&#xD;
powerful &lt;strong&gt;&lt;font color="#008000"&gt;FREE&lt;/font&gt;&lt;/strong&gt; tool specifically for&#xD;
C# developers called CodeRush Xpress. Today, in partnership with Microsoft, DevExpress&#xD;
has outdone themselves with a brand new version of CodeRush Xpress containing full&#xD;
support for both C# and Visual Basic. This release merges CodeRush Xpress with Refactor!&#xD;
for Visual Basic, creating one giant über-tool with an amazing features:&#xD;
&lt;/p&gt;&#xD;
        &lt;ul&gt;&#xD;
          &lt;li&gt;&#xD;
Duplicate Line &#xD;
&lt;/li&gt;&#xD;
          &lt;li&gt;&#xD;
Highlight All References &#xD;
&lt;/li&gt;&#xD;
          &lt;li&gt;&#xD;
Increase or Reduce Selection &#xD;
&lt;/li&gt;&#xD;
          &lt;li&gt;&#xD;
Smart Clipboard Operations &#xD;
&lt;/li&gt;&#xD;
          &lt;li&gt;&#xD;
Generate from Using (TDD) &#xD;
&lt;/li&gt;&#xD;
          &lt;li&gt;&#xD;
Quick Navigation Window &#xD;
&lt;/li&gt;&#xD;
          &lt;li&gt;&#xD;
Quick File Navigation &#xD;
&lt;/li&gt;&#xD;
          &lt;li&gt;&#xD;
            &lt;strong&gt;60+&lt;/strong&gt; Refactorings!&lt;/li&gt;&#xD;
        &lt;/ul&gt;&#xD;
        &lt;p&gt;&#xD;
Get it while it’s hot! You can download your &lt;strong&gt;&lt;font color="#008000"&gt;FREE&lt;/font&gt;&lt;/strong&gt; copy&#xD;
of CodeRush Xpress for Visual Studio 2008 from &lt;a title="http://devexpress.com/crx" href="http://devexpress.com/crx"&gt;http://devexpress.com/crx&lt;/a&gt;.&#xD;
&lt;/p&gt;&#xD;
        &lt;img width="0" height="0" src="http://diditwith.net/aggbug.ashx?id=adf10d38-3d3e-4341-ad41-11618d5d622b"&gt;&lt;/img&gt;&#xD;
      &lt;/body&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=3fHZMff0D4w:DibI5k8q2gI:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=3fHZMff0D4w:DibI5k8q2gI:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=3fHZMff0D4w:DibI5k8q2gI:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?i=3fHZMff0D4w:DibI5k8q2gI:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=3fHZMff0D4w:DibI5k8q2gI:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?i=3fHZMff0D4w:DibI5k8q2gI:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=3fHZMff0D4w:DibI5k8q2gI:cGdyc7Q-1BI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=cGdyc7Q-1BI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=3fHZMff0D4w:DibI5k8q2gI:7qCK26UlVX8"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=7qCK26UlVX8" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=3fHZMff0D4w:DibI5k8q2gI:cTv1dNCI_Tc"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=cTv1dNCI_Tc" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/DidItWithDotNet/~4/3fHZMff0D4w" height="1" width="1"/&gt;</description>
      <comments>http://diditwith.net/CommentView,guid,adf10d38-3d3e-4341-ad41-11618d5d622b.aspx</comments>
    <feedburner:origLink>http://diditwith.net/2009/05/15/CodeRushXpressForCAndVBIsLive.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://diditwith.net/Trackback.aspx?guid=aaca9fbb-5908-4b66-a6f7-6c7b99856c50</trackback:ping>
      <pingback:server>http://diditwith.net/pingback.aspx</pingback:server>
      <pingback:target>http://diditwith.net/PermaLink,guid,aaca9fbb-5908-4b66-a6f7-6c7b99856c50.aspx</pingback:target>
      <dc:creator>Dustin Campbell</dc:creator>
      <wfw:comment>http://diditwith.net/CommentView,guid,aaca9fbb-5908-4b66-a6f7-6c7b99856c50.aspx</wfw:comment>
      <wfw:commentRss>http://diditwith.net/SyndicationService.asmx/GetEntryCommentsRss?guid=aaca9fbb-5908-4b66-a6f7-6c7b99856c50</wfw:commentRss>
      <slash:comments>5</slash:comments>
      
      <title>YAPES: Problem Ten</title>
      <guid isPermaLink="false">http://diditwith.net/PermaLink,guid,aaca9fbb-5908-4b66-a6f7-6c7b99856c50.aspx</guid>
      <link>http://feedproxy.google.com/~r/DidItWithDotNet/~3/VXxiXJlAFjY/YAPESProblemTen.aspx</link>
      <pubDate>Tue, 17 Mar 2009 10:13:48 GMT</pubDate>
      <description>&lt;body xmlns="http://www.w3.org/1999/xhtml"&gt;&#xD;
        &lt;p&gt;&#xD;
I’m back again with another post in my &lt;a href="http://diditwith.net/CategoryView,category,Project%2BEuler.aspx"&gt;Yet&#xD;
Another Project Euler Series&lt;/a&gt;. As a reminder, my approach to these problems is&#xD;
to try to find the most beautiful solution that I can using F#. While performance&#xD;
is an important, I’m looking for the most elegant solutions that I can find.&#xD;
&lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
          &lt;a href="http://projecteuler.net/"&gt;Project Euler&lt;/a&gt;&#xD;
          &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=10"&gt;problem&#xD;
ten&lt;/a&gt; is another dealing prime numbers.&#xD;
&lt;/p&gt;&#xD;
        &lt;blockquote&gt;&#xD;
          &lt;div&gt;The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.&#xD;
&lt;/div&gt;&#xD;
          &lt;br&gt;&#xD;
          &lt;div&gt;Find the sum of all the primes below two million.&#xD;
&lt;/div&gt;&#xD;
        &lt;/blockquote&gt;&#xD;
        &lt;p&gt;&#xD;
As you can guess, there’s not too much to this problem. In fact, we’ll declare just&#xD;
one simple supporting function.&#xD;
&lt;/p&gt;&#xD;
        &lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;&#xD;
          &lt;span style="color: blue"&gt;let&lt;/span&gt; lessThan&#xD;
y x = x &amp;lt; y &#xD;
&lt;/div&gt;&#xD;
        &lt;p&gt;&#xD;
Using the above function and our existing prime number generator, our solution is&#xD;
short and simple.&#xD;
&lt;/p&gt;&#xD;
        &lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;primes&lt;br&gt;&#xD;
  |&amp;gt; Seq.take_while (lessThan 2000000L)&lt;br&gt;&#xD;
  |&amp;gt; Seq.sum&#xD;
&lt;/div&gt;&#xD;
        &lt;p&gt;&#xD;
I feel a little guilty that this post is so short, but that’s the benefit of spending &lt;a href="http://diditwith.net/2008/09/17/YAPESProblemSevenPart1.aspx"&gt;two&lt;/a&gt;&lt;a href="http://diditwith.net/2009/01/20/YAPESProblemSevenPart2.aspx"&gt;whole&lt;/a&gt; blog&#xD;
posts working out a prime number generator for &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=7"&gt;problem&#xD;
seven&lt;/a&gt;.&#xD;
&lt;/p&gt;&#xD;
        &lt;img width="0" height="0" src="http://diditwith.net/aggbug.ashx?id=aaca9fbb-5908-4b66-a6f7-6c7b99856c50"&gt;&lt;/img&gt;&#xD;
      &lt;/body&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=VXxiXJlAFjY:UE1a5xhHEWQ:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=VXxiXJlAFjY:UE1a5xhHEWQ:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=VXxiXJlAFjY:UE1a5xhHEWQ:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?i=VXxiXJlAFjY:UE1a5xhHEWQ:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=VXxiXJlAFjY:UE1a5xhHEWQ:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?i=VXxiXJlAFjY:UE1a5xhHEWQ:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=VXxiXJlAFjY:UE1a5xhHEWQ:cGdyc7Q-1BI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=cGdyc7Q-1BI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=VXxiXJlAFjY:UE1a5xhHEWQ:7qCK26UlVX8"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=7qCK26UlVX8" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=VXxiXJlAFjY:UE1a5xhHEWQ:cTv1dNCI_Tc"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=cTv1dNCI_Tc" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/DidItWithDotNet/~4/VXxiXJlAFjY" height="1" width="1"/&gt;</description>
      <comments>http://diditwith.net/CommentView,guid,aaca9fbb-5908-4b66-a6f7-6c7b99856c50.aspx</comments>
      <category>F#</category>
      <category>Functional Programming</category>
      <category>Project Euler</category>
    <feedburner:origLink>http://diditwith.net/2009/03/17/YAPESProblemTen.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://diditwith.net/Trackback.aspx?guid=47699f05-612a-4feb-8cf6-296c788fca4e</trackback:ping>
      <pingback:server>http://diditwith.net/pingback.aspx</pingback:server>
      <pingback:target>http://diditwith.net/PermaLink,guid,47699f05-612a-4feb-8cf6-296c788fca4e.aspx</pingback:target>
      <dc:creator>Dustin Campbell</dc:creator>
      <wfw:comment>http://diditwith.net/CommentView,guid,47699f05-612a-4feb-8cf6-296c788fca4e.aspx</wfw:comment>
      <wfw:commentRss>http://diditwith.net/SyndicationService.asmx/GetEntryCommentsRss?guid=47699f05-612a-4feb-8cf6-296c788fca4e</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <title>Lesser-Known VB: Customizing the Completion List</title>
      <guid isPermaLink="false">http://diditwith.net/PermaLink,guid,47699f05-612a-4feb-8cf6-296c788fca4e.aspx</guid>
      <link>http://feedproxy.google.com/~r/DidItWithDotNet/~3/QealjHhUIMk/LesserKnownVBCustomizingTheCompletionList.aspx</link>
      <pubDate>Sat, 14 Mar 2009 20:23:10 GMT</pubDate>
      <description>&lt;p&gt;
In &lt;a href="http://msdn.microsoft.com/vbasic"&gt;Visual Basic .NET&lt;/a&gt;, there are several
cases in which the statement completion list will present the user with a list of
values rather than the standard completion set. Most often, this occurs when assigning
to a variable of one of the common System.Drawing types, Color, Brush or Pen.
&lt;/p&gt;
&lt;p&gt;
&lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" title="Value Completion List" border="0" alt="Value Completion List" src="http://diditwith.net/content/binary/WindowsLiveWriter/CustomizingEnumIntelliSense_86CC/Example1_3.png" width="400" height="256"&gt;
&lt;/p&gt;
&lt;p&gt;
At first glance, the screenshot above might seem as if the Visual Basic IDE has hard-coded
a set of values into IntelliSense, but that’s not the case. In fact, this is caused
by a seldom-used feature of &lt;a href="http://msdn.microsoft.com/en-us/library/ms172653.aspx"&gt;XML
Documentation&lt;/a&gt; that is supported by &lt;a href="http://msdn.microsoft.com/vbasic"&gt;Visual
Basic .NET&lt;/a&gt;, but isn’t currently supported by &lt;a href="http://msdn.microsoft.com/vcsharp"&gt;C#&lt;/a&gt;&lt;sup&gt;1&lt;/sup&gt;.
By cracking open the the XML Documentation file for System.Drawing.dll (located at
C:\Windows\Microsoft.NET\Framework\v2.0.50727\en\System.Drawing.xml on my machine),
we’ll see a curious XML tag on the System.Drawing.Color definition.
&lt;/p&gt;
&lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;member &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;T:System.Drawing.Color&lt;/span&gt;"&lt;span style="color: blue"&gt;&amp;gt;&lt;br&gt;
&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;summary&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;Represents
an ARGB (alpha, red, green, blue) color.&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;summary&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br&gt;
&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;filterpriority&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;1&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;filterpriority&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;
&lt;br&gt;
&amp;nbsp; &lt;span style="background-color: #ffff00; color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;&lt;span style="background-color: #ffff00"&gt;completionlist &lt;/span&gt;&lt;/span&gt;&lt;span style="color: red"&gt;&lt;span style="background-color: #ffff00"&gt;cref&lt;/span&gt;&lt;/span&gt;&lt;span style="background-color: #ffff00; color: blue"&gt;=&lt;/span&gt;&lt;span style="background-color: #ffff00"&gt;"&lt;/span&gt;&lt;span style="background-color: #ffff00; color: blue"&gt;T:System.Drawing.Color&lt;/span&gt;&lt;span style="background-color: #ffff00"&gt;" &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span style="background-color: #ffff00"&gt;/&amp;gt;&lt;/span&gt;
&lt;br&gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;member&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt; 
&lt;/div&gt;
&lt;p&gt;
The highlighted &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;completionlist&lt;/span&gt; tag
above is used by Visual Basic to populate the completion list with the public shared&lt;sup&gt;2&lt;/sup&gt; fields
and properties from the specified class or module. In this particular case, the XML
documentation causes Visual Basic to populate the completion list with the public
shared properties of &lt;a href="http://msdn.microsoft.com/en-us/library/system.drawing.color.aspx"&gt;System.Drawing.Color&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Don’t believe me? Just comment out the System.Drawing.Color &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;completionlist&lt;/span&gt; tag
above, save and restart Visual Studio to see how this influences the statement completion
list.
&lt;/p&gt;
&lt;p&gt;
&lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" title="Standard Completion List" border="0" alt="Standard Completion List" src="http://diditwith.net/content/binary/WindowsLiveWriter/CustomizingEnumIntelliSense_86CC/Example2_3.png" width="400" height="209" ?&gt;
&lt;/p&gt;
&lt;p&gt;
Many of you are probably thinking, “so, is this just a nifty implementation detail,
or is something I can actually use?” The answer is, yes, this something you can use
today to customize Visual Basic’s statement completion. The code below demonstrates
how the &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;completionlist&lt;/span&gt; tag
can be used.
&lt;/p&gt;
&lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;&lt;span style="color: green"&gt;''' &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;completionlist
cref="CommonOperations"/&amp;gt;&lt;br&gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;Public Class &lt;/span&gt;Operation&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;Private ReadOnly &lt;/span&gt;_execute &lt;span style="color: blue"&gt;As &lt;/span&gt;Func(&lt;span style="color: blue"&gt;Of
Integer&lt;/span&gt;, &lt;span style="color: blue"&gt;Integer&lt;/span&gt;, &lt;span style="color: blue"&gt;Integer&lt;/span&gt;)&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;Public Sub New&lt;/span&gt;(&lt;span style="color: blue"&gt;ByVal &lt;/span&gt;execute &lt;span style="color: blue"&gt;As &lt;/span&gt;Func(&lt;span style="color: blue"&gt;Of
Integer&lt;/span&gt;, &lt;span style="color: blue"&gt;Integer&lt;/span&gt;, &lt;span style="color: blue"&gt;Integer&lt;/span&gt;))&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _execute = execute&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;End Sub&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Public Function &lt;/span&gt;Execute(&lt;span style="color: blue"&gt;ByVal &lt;/span&gt;arg1 &lt;span style="color: blue"&gt;As
Integer&lt;/span&gt;, &lt;span style="color: blue"&gt;ByVal &lt;/span&gt;arg2 &lt;span style="color: blue"&gt;As
Integer&lt;/span&gt;) &lt;span style="color: blue"&gt;As Integer&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Return &lt;/span&gt;_execute(arg1, arg2)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;End Function&lt;br&gt;
End Class&lt;br&gt;
&lt;br&gt;
Public NotInheritable Class &lt;/span&gt;CommonOperations&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;Public Shared ReadOnly &lt;/span&gt;Add
= &lt;span style="color: blue"&gt;New &lt;/span&gt;Operation(&lt;span style="color: blue"&gt;Function&lt;/span&gt;(x,
y) x + y)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;Public Shared ReadOnly &lt;/span&gt;Subtract
= &lt;span style="color: blue"&gt;New &lt;/span&gt;Operation(&lt;span style="color: blue"&gt;Function&lt;/span&gt;(x,
y) x - y)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;Public Shared ReadOnly &lt;/span&gt;Multiply
= &lt;span style="color: blue"&gt;New &lt;/span&gt;Operation(&lt;span style="color: blue"&gt;Function&lt;/span&gt;(x,
y) x * y)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;Public Shared ReadOnly &lt;/span&gt;Divide
= &lt;span style="color: blue"&gt;New &lt;/span&gt;Operation(&lt;span style="color: blue"&gt;Function&lt;/span&gt;(x,
y) x / y)&lt;br&gt;
&lt;span style="color: blue"&gt;End Class&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;
Visual Basic will automatically pick up the &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;completionlist&lt;/span&gt; tag
in the code above and use it to populate the completion list like so.
&lt;/p&gt;
&lt;p&gt;
&lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" title="Custom Value Completion List" border="0" alt="Custom Value Completion List" src="http://diditwith.net/content/binary/WindowsLiveWriter/CustomizingEnumIntelliSense_86CC/Example3_3.png" width="400" height="149"&gt; 
&lt;/p&gt;
&lt;p&gt;
While a bit limited, it’s pretty easy to customize the statement completion list experience
for Visual Basic to make certain types of APIs more discoverable. It’s as simple as
a single XML tag.
&lt;/p&gt;
&lt;p style="border-top: rgb(221,221,221) 1px solid"&gt;
&lt;sup&gt;1&lt;/sup&gt;&lt;a href="http://blogs.msdn.com/kevinpilchbisson/default.aspx"&gt;Kevin Pilch-Bisson&lt;/a&gt; (C#
IDE Developer Lead and &lt;a href="http://twitter.com/Pilchie"&gt;swell guy&lt;/a&gt;) has a clever
idea for supporting the &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;completionlist&lt;/span&gt; tag
in the C# statement completion list while staying true to the C# IntelliSense model.&lt;br&gt;
&lt;sup&gt;2&lt;/sup&gt;The VB "Shared" keyword = static in C#.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://diditwith.net/aggbug.ashx?id=47699f05-612a-4feb-8cf6-296c788fca4e" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=QealjHhUIMk:HGiTvKXZwmY:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=QealjHhUIMk:HGiTvKXZwmY:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=QealjHhUIMk:HGiTvKXZwmY:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?i=QealjHhUIMk:HGiTvKXZwmY:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=QealjHhUIMk:HGiTvKXZwmY:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?i=QealjHhUIMk:HGiTvKXZwmY:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=QealjHhUIMk:HGiTvKXZwmY:cGdyc7Q-1BI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=cGdyc7Q-1BI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=QealjHhUIMk:HGiTvKXZwmY:7qCK26UlVX8"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=7qCK26UlVX8" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=QealjHhUIMk:HGiTvKXZwmY:cTv1dNCI_Tc"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=cTv1dNCI_Tc" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/DidItWithDotNet/~4/QealjHhUIMk" height="1" width="1"/&gt;</description>
      <comments>http://diditwith.net/CommentView,guid,47699f05-612a-4feb-8cf6-296c788fca4e.aspx</comments>
      <category>Extending Visual Studio</category>
      <category>IntelliSense</category>
      <category>Tips &amp; Tricks</category>
      <category>Visual Basic</category>
      <category>Visual Studio</category>
    <feedburner:origLink>http://diditwith.net/2009/03/14/LesserKnownVBCustomizingTheCompletionList.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://diditwith.net/Trackback.aspx?guid=a7d69759-4152-4264-a97e-2b7aa2ec3672</trackback:ping>
      <pingback:server>http://diditwith.net/pingback.aspx</pingback:server>
      <pingback:target>http://diditwith.net/PermaLink,guid,a7d69759-4152-4264-a97e-2b7aa2ec3672.aspx</pingback:target>
      <dc:creator>Dustin Campbell</dc:creator>
      <wfw:comment>http://diditwith.net/CommentView,guid,a7d69759-4152-4264-a97e-2b7aa2ec3672.aspx</wfw:comment>
      <wfw:commentRss>http://diditwith.net/SyndicationService.asmx/GetEntryCommentsRss?guid=a7d69759-4152-4264-a97e-2b7aa2ec3672</wfw:commentRss>
      <slash:comments>1</slash:comments>
      
      <title>YAPES: Problem Nine</title>
      <guid isPermaLink="false">http://diditwith.net/PermaLink,guid,a7d69759-4152-4264-a97e-2b7aa2ec3672.aspx</guid>
      <link>http://feedproxy.google.com/~r/DidItWithDotNet/~3/otHJMc10ryw/YAPESProblemNine.aspx</link>
      <pubDate>Mon, 09 Mar 2009 15:05:47 GMT</pubDate>
      <description>&lt;body xmlns="http://www.w3.org/1999/xhtml"&gt;&#xD;
        &lt;p&gt;&#xD;
Welcome back F# junkies! In this installment of YAPES&lt;sup&gt;1&lt;/sup&gt;, we’re tackling &lt;a href="http://projecteuler.net"&gt;Project&#xD;
Euler&lt;/a&gt;&lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=9"&gt;problem&#xD;
nine&lt;/a&gt;.&#xD;
&lt;/p&gt;&#xD;
        &lt;blockquote&gt;&#xD;
          &lt;div&gt;A Pythagorean triplet is a set of three natural numbers, &lt;i&gt;a&lt;/i&gt; &amp;lt; &lt;i&gt;b&lt;/i&gt; &amp;lt; &lt;i&gt;c&lt;/i&gt;,&#xD;
for which,&#xD;
&lt;/div&gt;&#xD;
          &lt;br&gt;&#xD;
          &lt;div style="text-align: center"&gt;&#xD;
            &lt;i&gt;a&lt;/i&gt;&#xD;
            &lt;sup&gt;2&lt;/sup&gt; + &lt;i&gt;b&lt;/i&gt;&lt;sup&gt;2&lt;/sup&gt; = &lt;i&gt;c&lt;/i&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/div&gt;&#xD;
          &lt;br&gt;&#xD;
          &lt;div&gt;For example, 3&lt;sup&gt;2&lt;/sup&gt; + 4&lt;sup&gt;2&lt;/sup&gt; = 9 + 16 = 25 = 5&lt;sup&gt;2&lt;/sup&gt;.&#xD;
&lt;/div&gt;&#xD;
          &lt;br&gt;&#xD;
          &lt;div&gt;There exists exactly one Pythagorean triplet for which &lt;i&gt;a&lt;/i&gt; + &lt;i&gt;b&lt;/i&gt; + &lt;i&gt;c&lt;/i&gt; =&#xD;
1000. Find the product &lt;em&gt;abc&lt;/em&gt;.&#xD;
&lt;/div&gt;&#xD;
        &lt;/blockquote&gt;&#xD;
        &lt;p&gt;&#xD;
Like we’ve done before, we’ll break this problem down into steps:&#xD;
&lt;/p&gt;&#xD;
        &lt;ol&gt;&#xD;
          &lt;li&gt;&#xD;
Generate a stream of &lt;a href="http://en.wikipedia.org/wiki/Pythagorean_triple"&gt;Pythagorean&#xD;
triples&lt;/a&gt;. &#xD;
&lt;/li&gt;&#xD;
          &lt;li&gt;&#xD;
Find the Pythagorean triple whose sum is equal to 1000. &#xD;
&lt;/li&gt;&#xD;
          &lt;li&gt;&#xD;
Calculate the product of that triple.&lt;/li&gt;&#xD;
        &lt;/ol&gt;&#xD;
        &lt;p&gt;&#xD;
Sound good?&#xD;
&lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
The first order of business is to determine how to generate &lt;a href="http://en.wikipedia.org/wiki/Pythagorean_triple"&gt;Pythagorean&#xD;
triples&lt;/a&gt;. It turns out that there are &lt;a href="http://en.wikipedia.org/wiki/Formulas_for_generating_Pythagorean_triples"&gt;many&#xD;
ways&lt;/a&gt; to do this. However, to keep it simple, we will use &lt;a href="http://en.wikipedia.org/wiki/Euclid"&gt;Euclid’s&lt;/a&gt; classic&#xD;
formula:&#xD;
&lt;/p&gt;&#xD;
        &lt;blockquote&gt;&#xD;
          &lt;div&gt;Given a pair of positive integers &lt;em&gt;m&lt;/em&gt; and &lt;em&gt;n&lt;/em&gt; with &lt;em&gt;m&lt;/em&gt; &amp;gt; &lt;em&gt;n&lt;/em&gt;&lt;/div&gt;&#xD;
          &lt;br&gt;&#xD;
          &lt;div style="text-align: center"&gt;&#xD;
            &lt;em&gt;a&lt;/em&gt; = 2&lt;em&gt;mn&lt;/em&gt;, &lt;em&gt;b&lt;/em&gt; = &lt;em&gt;m&lt;/em&gt;&lt;sup&gt;2&lt;/sup&gt; – &lt;em&gt;n&lt;/em&gt;&lt;sup&gt;2&lt;/sup&gt;, &lt;em&gt;c&lt;/em&gt; = &lt;em&gt;m&lt;/em&gt;&lt;sup&gt;2&lt;/sup&gt; + &lt;em&gt;n&lt;/em&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/div&gt;&#xD;
        &lt;/blockquote&gt;&#xD;
        &lt;p&gt;&#xD;
Truthfully, this formula doesn’t actually produce every possible Pythagorean triple.&#xD;
However, since the solution is within the set that it generates, that really isn’t&#xD;
a problem.&#xD;
&lt;/p&gt;&#xD;
        &lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 8px; margin-top: 30px; padding-left: 8px; padding-right: 8px; margin-bottom: 30px; background: white; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 8px"&gt;&#xD;
          &lt;div style="font-size: medium"&gt;&#xD;
            &lt;strong&gt;&#xD;
              &lt;span style="color: rgb(0,102,255)"&gt;N&lt;/span&gt;&#xD;
              &lt;span style="color: rgb(255,80,80)"&gt;e&lt;/span&gt;&#xD;
              &lt;span style="color: rgb(0,204,0)"&gt;R&lt;/span&gt;&#xD;
              &lt;span style="color: rgb(204,51,255)"&gt;d&lt;/span&gt; Note&lt;/strong&gt;&#xD;
          &lt;/div&gt;&#xD;
          &lt;div style="margin-top: 10px; margin-bottom: 10px"&gt;To produce &lt;em&gt;every&lt;/em&gt; Pythagorean&#xD;
triple, we would have to introduce an additional parameter, &lt;em&gt;k&lt;/em&gt;, into the formula.&#xD;
&lt;/div&gt;&#xD;
          &lt;blockquote&gt;&#xD;
            &lt;div style="text-align: center"&gt;&#xD;
              &lt;em&gt;a&lt;/em&gt; = 2&lt;em&gt;kmn&lt;/em&gt;, &lt;em&gt;b&lt;/em&gt; = &lt;em&gt;k&lt;/em&gt;(&lt;em&gt;m&lt;/em&gt;&lt;sup&gt;2&lt;/sup&gt; – &lt;em&gt;n&lt;/em&gt;&lt;sup&gt;2&lt;/sup&gt;), &lt;em&gt;c&lt;/em&gt; = &lt;em&gt;k&lt;/em&gt;(&lt;em&gt;m&lt;/em&gt;&lt;sup&gt;2&lt;/sup&gt; + &lt;em&gt;n&lt;/em&gt;&lt;sup&gt;2&lt;/sup&gt;)&#xD;
&lt;/div&gt;&#xD;
          &lt;/blockquote&gt;&#xD;
          &lt;div style="margin-top: 10px"&gt;This is left as an exercise for you, dear reader!&#xD;
&lt;/div&gt;&#xD;
        &lt;/div&gt;&#xD;
        &lt;p&gt;&#xD;
First, we’ll declare an &lt;a href="http://msdn.microsoft.com/fsharp"&gt;F#&lt;/a&gt; function&#xD;
that calculates a Pythagorean triple using Euclid’s formula.&#xD;
&lt;/p&gt;&#xD;
        &lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;&#xD;
          &lt;span style="color: blue"&gt;let&lt;/span&gt; getTriple&#xD;
m n =&lt;br&gt;&#xD;
  &lt;span style="color: blue"&gt;let&lt;/span&gt; a = 2*m*n&lt;br&gt;&#xD;
  &lt;span style="color: blue"&gt;let&lt;/span&gt; b = m*m - n*n&lt;br&gt;&#xD;
  &lt;span style="color: blue"&gt;let&lt;/span&gt; c = m*m + n*n&lt;br&gt;&#xD;
  a,b,c &#xD;
&lt;/div&gt;&#xD;
        &lt;p&gt;&#xD;
Using the above function, it’s easy to produce a stream of Pythagorean triples. Our&#xD;
strategy will be to start with the pair, &lt;em&gt;m&lt;/em&gt; = 2 and &lt;em&gt;n&lt;/em&gt; = 1. To generate&#xD;
the next pair, we’ll first check if &lt;em&gt;n&lt;/em&gt; + 1 is less than &lt;em&gt;m&lt;/em&gt;. If it&#xD;
is, we’ll use &lt;em&gt;m&lt;/em&gt; and &lt;em&gt;n&lt;/em&gt; + 1 as the next pair, otherwise we’ll use &lt;em&gt;m&lt;/em&gt; +&#xD;
1 and 1. Using &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;Seq.unfold&lt;/span&gt;,&#xD;
we can use this algorithm to produce an infinite stream of Pythagorean triples:&#xD;
&lt;/p&gt;&#xD;
        &lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;&#xD;
          &lt;span style="color: blue"&gt;let&lt;/span&gt; triples&#xD;
=&lt;br&gt;&#xD;
  Seq.unfold (&lt;span style="color: blue"&gt;fun&lt;/span&gt; (m,n) &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt;&lt;br&gt;&#xD;
                &lt;span style="color: blue"&gt;let&lt;/span&gt; res&#xD;
= getTriple m n&lt;br&gt;&#xD;
                &lt;span style="color: blue"&gt;let&lt;/span&gt; next&#xD;
= &lt;span style="color: blue"&gt;if&lt;/span&gt; n+1 &amp;lt; m &lt;span style="color: blue"&gt;then&lt;/span&gt; m,n+1 &lt;span style="color: blue"&gt;else&lt;/span&gt; m+1,1&lt;br&gt;&#xD;
               &#xD;
Some(res, next))&lt;br&gt;&#xD;
             (2,1) &#xD;
&lt;/div&gt;&#xD;
        &lt;p&gt;&#xD;
With our generator in place, we can easily solve the problem.&#xD;
&lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
        &lt;/p&gt;&#xD;
        &lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;triples&lt;br&gt;&#xD;
  |&amp;gt; Seq.find (&lt;font color="#0000ff"&gt;fun&lt;/font&gt; (a,b,c) &lt;font color="#0000ff"&gt;–&amp;gt;&lt;/font&gt; a+b+c&#xD;
= 1000)&lt;br&gt;&#xD;
  |&amp;gt; (&lt;font color="#0000ff"&gt;fun&lt;/font&gt; (a,b,c) &lt;font color="#0000ff"&gt;–&amp;gt;&lt;/font&gt; a*b*c)&#xD;
&lt;/div&gt;&#xD;
        &lt;p&gt;&#xD;
While the above solution works well, we can abstract further to produce a more elegant&#xD;
solution. First, we’ll declare three functions to handle the three operations that&#xD;
we’re performing explicitly, &lt;em&gt;sum&lt;/em&gt;, &lt;em&gt;product&lt;/em&gt; and &lt;em&gt;equals&lt;/em&gt;.&#xD;
&lt;/p&gt;&#xD;
        &lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;&#xD;
          &lt;span style="color: blue"&gt;let&lt;/span&gt; sum&#xD;
(a,b,c) = a+b+c&lt;br&gt;&lt;span style="color: blue"&gt;let&lt;/span&gt; product (a,b,c) = a*b*c&lt;br&gt;&lt;span style="color: blue"&gt;let&lt;/span&gt; equals x y = x = y &#xD;
&lt;/div&gt;&#xD;
        &lt;p&gt;&#xD;
Now, we can restate our solution using these functions with a bit of function composition&#xD;
to produce a truly beautiful solution.&#xD;
&lt;/p&gt;&#xD;
        &lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;triples&lt;br&gt;&#xD;
  |&amp;gt; Seq.find (sum &amp;gt;&amp;gt; equals 1000)&lt;br&gt;&#xD;
  |&amp;gt; product &#xD;
&lt;/div&gt;&#xD;
        &lt;p&gt;&#xD;
As we’ve seen &lt;a href="http://diditwith.net/2008/05/06/YAPESProblemSix.aspx"&gt;once&#xD;
before&lt;/a&gt;, function composition is beautiful.&#xD;
&lt;/p&gt;&#xD;
        &lt;p style="border-top: rgb(221,221,221) 1px solid"&gt;&#xD;
          &lt;sup&gt;1&lt;/sup&gt;Yet Another Project Euler Series&#xD;
&lt;/p&gt;&#xD;
        &lt;img width="0" height="0" src="http://diditwith.net/aggbug.ashx?id=a7d69759-4152-4264-a97e-2b7aa2ec3672"&gt;&lt;/img&gt;&#xD;
      &lt;/body&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=otHJMc10ryw:uKWN3K95H9Q:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=otHJMc10ryw:uKWN3K95H9Q:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=otHJMc10ryw:uKWN3K95H9Q:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?i=otHJMc10ryw:uKWN3K95H9Q:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=otHJMc10ryw:uKWN3K95H9Q:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?i=otHJMc10ryw:uKWN3K95H9Q:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=otHJMc10ryw:uKWN3K95H9Q:cGdyc7Q-1BI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=cGdyc7Q-1BI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=otHJMc10ryw:uKWN3K95H9Q:7qCK26UlVX8"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=7qCK26UlVX8" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=otHJMc10ryw:uKWN3K95H9Q:cTv1dNCI_Tc"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=cTv1dNCI_Tc" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/DidItWithDotNet/~4/otHJMc10ryw" height="1" width="1"/&gt;</description>
      <comments>http://diditwith.net/CommentView,guid,a7d69759-4152-4264-a97e-2b7aa2ec3672.aspx</comments>
      <category>F#</category>
      <category>Functional Programming</category>
      <category>Project Euler</category>
    <feedburner:origLink>http://diditwith.net/2009/03/09/YAPESProblemNine.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://diditwith.net/Trackback.aspx?guid=2e8c82a9-37f5-474a-9dfc-f5ed98a215e9</trackback:ping>
      <pingback:server>http://diditwith.net/pingback.aspx</pingback:server>
      <pingback:target>http://diditwith.net/PermaLink,guid,2e8c82a9-37f5-474a-9dfc-f5ed98a215e9.aspx</pingback:target>
      <dc:creator>Dustin Campbell</dc:creator>
      <wfw:comment>http://diditwith.net/CommentView,guid,2e8c82a9-37f5-474a-9dfc-f5ed98a215e9.aspx</wfw:comment>
      <wfw:commentRss>http://diditwith.net/SyndicationService.asmx/GetEntryCommentsRss?guid=2e8c82a9-37f5-474a-9dfc-f5ed98a215e9</wfw:commentRss>
      <slash:comments>2</slash:comments>
      
      <title>YAPES: Problem Eight</title>
      <guid isPermaLink="false">http://diditwith.net/PermaLink,guid,2e8c82a9-37f5-474a-9dfc-f5ed98a215e9.aspx</guid>
      <link>http://feedproxy.google.com/~r/DidItWithDotNet/~3/CVwRSaI9AdU/YAPESProblemEight.aspx</link>
      <pubDate>Sat, 07 Mar 2009 21:09:11 GMT</pubDate>
      <description>&lt;body xmlns="http://www.w3.org/1999/xhtml"&gt;&#xD;
        &lt;p&gt;&#xD;
Welcome coding ninjas!&lt;sup&gt;1&lt;/sup&gt; Continuing with &lt;a href="http://diditwith.net/CategoryView,category,Project%2BEuler.aspx"&gt;Yet&#xD;
Another Project Euler Series (YAPES™)&lt;/a&gt;, we come upon &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=8"&gt;problem&#xD;
eight&lt;/a&gt;.&#xD;
&lt;/p&gt;&#xD;
        &lt;blockquote&gt;&#xD;
          &lt;div&gt;Find the greatest product of five consecutive digits in the 1000-digit number.&#xD;
&lt;/div&gt;&#xD;
          &lt;br&gt;&#xD;
          &lt;div style="text-align: center; font-family: consolas,'Courier New',courier,monospace"&gt;73167176531330624919225119674426574742355349194934&lt;br&gt;&#xD;
96983520312774506326239578318016984801869478851843&lt;br&gt;&#xD;
85861560789112949495459501737958331952853208805511&lt;br&gt;&#xD;
12540698747158523863050715693290963295227443043557&lt;br&gt;&#xD;
66896648950445244523161731856403098711121722383113&lt;br&gt;&#xD;
62229893423380308135336276614282806444486645238749&lt;br&gt;&#xD;
30358907296290491560440772390713810515859307960866&lt;br&gt;&#xD;
70172427121883998797908792274921901699720888093776&lt;br&gt;&#xD;
65727333001053367881220235421809751254540594752243&lt;br&gt;&#xD;
52584907711670556013604839586446706324415722155397&lt;br&gt;&#xD;
53697817977846174064955149290862569321978468622482&lt;br&gt;&#xD;
83972241375657056057490261407972968652414535100474&lt;br&gt;&#xD;
82166370484403199890008895243450658541227588666881&lt;br&gt;&#xD;
16427171479924442928230863465674813919123162824586&lt;br&gt;&#xD;
17866458359124566529476545682848912883142607690042&lt;br&gt;&#xD;
24219022671055626321111109370544217506941658960408&lt;br&gt;&#xD;
07198403850962455444362981230987879927244284909188&lt;br&gt;&#xD;
84580156166097919133875499200524063689912560717606&lt;br&gt;&#xD;
05886116467109405077541002256983155200055935729725&lt;br&gt;&#xD;
71636269561882670428252483600823257530420752963450&#xD;
&lt;/div&gt;&#xD;
        &lt;/blockquote&gt;&#xD;
        &lt;p&gt;&#xD;
Being one of the earlier &lt;a href="http://projecteuler.net/"&gt;Project Euler&lt;/a&gt; problems,&#xD;
this is pretty trivial to solve. We’ll approach the problem by first breaking it into&#xD;
a set of simple steps:&#xD;
&lt;/p&gt;&#xD;
        &lt;ol&gt;&#xD;
          &lt;li&gt;&#xD;
Transform the 1000-digit number into a list of digits. &#xD;
&lt;/li&gt;&#xD;
          &lt;li&gt;&#xD;
Transform the list of digits into a list of quintuplets—that is, a list containing&#xD;
tuples of each five consecutive elements. &#xD;
&lt;/li&gt;&#xD;
          &lt;li&gt;&#xD;
Transform the list of quintuplets into a list containing the product of each. &#xD;
&lt;/li&gt;&#xD;
          &lt;li&gt;&#xD;
Find the maximum element in the list.&lt;/li&gt;&#xD;
        &lt;/ol&gt;&#xD;
        &lt;p&gt;&#xD;
Some or all of the steps above could be combined to produce a more optimal solution.&#xD;
However, the beauty of &lt;a href="http://msdn.microsoft.com/fsharp"&gt;F#&lt;/a&gt;—and functional&#xD;
programming in general—is the ability to easily break a problem like this into a series&#xD;
of data transformations that run quickly enough for most situations. When a problem&#xD;
is broken down into simple operations, it is easier to optimize in other ways (e.g. &lt;a href="http://en.wikipedia.org/wiki/Parallelization"&gt;parallelization&lt;/a&gt;).&#xD;
&lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
The first step is to transform the 1000-digit number into a list of digits. To facilitate&#xD;
working with such a large number in code, we’ll represent it as a multi-line string.&#xD;
Like before, we’ll break the problem of converting a string into a list of digits&#xD;
into a set of simple steps:&#xD;
&lt;/p&gt;&#xD;
        &lt;ol&gt;&#xD;
          &lt;li&gt;&#xD;
Transform the string into a list of chars. &#xD;
&lt;/li&gt;&#xD;
          &lt;li&gt;&#xD;
Because this was a multi-line string, some of the chars will be line breaks and whitespace.&#xD;
So, we’ll filter the list to include only the chars that represent digits. &#xD;
&lt;/li&gt;&#xD;
          &lt;li&gt;&#xD;
Transform the list of digit chars into a list containing the numerical value of each.&lt;/li&gt;&#xD;
        &lt;/ol&gt;&#xD;
        &lt;p&gt;&#xD;
Let’s define a few library functions to to help with each step above.&#xD;
&lt;/p&gt;&#xD;
        &lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;&#xD;
          &lt;span style="color: blue"&gt;module&lt;/span&gt; String&#xD;
=&lt;br&gt;&#xD;
  &lt;span style="color: green"&gt;/// Takes a string and produces a list of chars.&lt;/span&gt;&lt;br&gt;&#xD;
  &lt;span style="color: blue"&gt;let&lt;/span&gt; toChars (s : string) =&lt;br&gt;&#xD;
    s.ToCharArray() |&amp;gt; Array.to_list&lt;br&gt;&lt;br&gt;&lt;span style="color: blue"&gt;module&lt;/span&gt; Char =&lt;br&gt;&#xD;
  &lt;span style="color: green"&gt;/// Determines whether a char represents a digit.&lt;/span&gt;&lt;br&gt;&#xD;
  &lt;span style="color: blue"&gt;let&lt;/span&gt; isDigit (c : char) =&lt;br&gt;&#xD;
    System.Char.IsDigit(c)&lt;br&gt;&lt;br&gt;&#xD;
  &lt;span style="color: green"&gt;/// Converts a char representing a digit into its&#xD;
numerical value.&lt;/span&gt;&lt;br&gt;&#xD;
  &lt;span style="color: blue"&gt;let&lt;/span&gt; toNumber (c : char) =&lt;br&gt;&#xD;
    int c - int '0'&#xD;
&lt;/div&gt;&#xD;
        &lt;p&gt;&#xD;
Armed with these, we can perform the 3 steps above in a declarative fashion.&#xD;
&lt;/p&gt;&#xD;
        &lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;&#xD;
          &lt;span style="color: blue"&gt;let&lt;/span&gt; number&#xD;
=&lt;br&gt;&#xD;
  &lt;span style="color: maroon"&gt;"73167176531330624919225119674426574742355349194934&lt;/span&gt;&lt;br&gt;&#xD;
   &lt;span style="color: maroon"&gt;96983520312774506326239578318016984801869478851843&lt;/span&gt;&lt;br&gt;&#xD;
   &lt;span style="color: maroon"&gt;85861560789112949495459501737958331952853208805511&lt;/span&gt;&lt;br&gt;&#xD;
   &lt;span style="color: maroon"&gt;12540698747158523863050715693290963295227443043557&lt;/span&gt;&lt;br&gt;&#xD;
   &lt;span style="color: maroon"&gt;66896648950445244523161731856403098711121722383113&lt;/span&gt;&lt;br&gt;&#xD;
   &lt;span style="color: maroon"&gt;62229893423380308135336276614282806444486645238749&lt;/span&gt;&lt;br&gt;&#xD;
   &lt;span style="color: maroon"&gt;30358907296290491560440772390713810515859307960866&lt;/span&gt;&lt;br&gt;&#xD;
   &lt;span style="color: maroon"&gt;70172427121883998797908792274921901699720888093776&lt;/span&gt;&lt;br&gt;&#xD;
   &lt;span style="color: maroon"&gt;65727333001053367881220235421809751254540594752243&lt;/span&gt;&lt;br&gt;&#xD;
   &lt;span style="color: maroon"&gt;52584907711670556013604839586446706324415722155397&lt;/span&gt;&lt;br&gt;&#xD;
   &lt;span style="color: maroon"&gt;53697817977846174064955149290862569321978468622482&lt;/span&gt;&lt;br&gt;&#xD;
   &lt;span style="color: maroon"&gt;83972241375657056057490261407972968652414535100474&lt;/span&gt;&lt;br&gt;&#xD;
   &lt;span style="color: maroon"&gt;82166370484403199890008895243450658541227588666881&lt;/span&gt;&lt;br&gt;&#xD;
   &lt;span style="color: maroon"&gt;16427171479924442928230863465674813919123162824586&lt;/span&gt;&lt;br&gt;&#xD;
   &lt;span style="color: maroon"&gt;17866458359124566529476545682848912883142607690042&lt;/span&gt;&lt;br&gt;&#xD;
   &lt;span style="color: maroon"&gt;24219022671055626321111109370544217506941658960408&lt;/span&gt;&lt;br&gt;&#xD;
   &lt;span style="color: maroon"&gt;07198403850962455444362981230987879927244284909188&lt;/span&gt;&lt;br&gt;&#xD;
   &lt;span style="color: maroon"&gt;84580156166097919133875499200524063689912560717606&lt;/span&gt;&lt;br&gt;&#xD;
   &lt;span style="color: maroon"&gt;05886116467109405077541002256983155200055935729725&lt;/span&gt;&lt;br&gt;&#xD;
   &lt;span style="color: maroon"&gt;71636269561882670428252483600823257530420752963450"&lt;/span&gt;&lt;br&gt;&#xD;
   |&amp;gt; String.toChars&lt;br&gt;&#xD;
   |&amp;gt; List.filter Char.isDigit&lt;br&gt;&#xD;
   |&amp;gt; List.map Char.toNumber&#xD;
&lt;/div&gt;&#xD;
        &lt;p&gt;&#xD;
The next step is to transform the list of digits into a list of quintuplets. To achieve&#xD;
this, we’ll write a simple recursive function.&#xD;
&lt;/p&gt;&#xD;
        &lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;&#xD;
          &lt;span style="color: blue"&gt;let&lt;/span&gt; &lt;span style="color: blue"&gt;rec&lt;/span&gt; toQuintuplets&#xD;
l =&lt;br&gt;&#xD;
  &lt;span style="color: blue"&gt;match&lt;/span&gt; l &lt;span style="color: blue"&gt;with&lt;/span&gt;&lt;br&gt;&#xD;
  | x1::(x2::x3::x4::x5::_ &lt;span style="color: blue"&gt;as&lt;/span&gt; t) &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; (x1,x2,x3,x4,x5)::(toQuintuplets&#xD;
t)&lt;br&gt;&#xD;
  | _ &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; [] &#xD;
&lt;/div&gt;&#xD;
        &lt;p&gt;&#xD;
Notice that the first pattern match above is slightly more complicated as we bind&#xD;
the list starting with the next element in the list to &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;t&lt;/span&gt;,&#xD;
to make the recursion cleaner.&#xD;
&lt;/p&gt;&#xD;
        &lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 8px; margin-top: 30px; padding-left: 8px; padding-right: 8px; margin-bottom: 30px; background: white; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 8px"&gt;&#xD;
          &lt;div style="font-size: medium"&gt;&#xD;
            &lt;strong&gt;&#xD;
              &lt;span style="color: rgb(0,102,255)"&gt;N&lt;/span&gt;&#xD;
              &lt;span style="color: rgb(255,80,80)"&gt;e&lt;/span&gt;&#xD;
              &lt;span style="color: rgb(0,204,0)"&gt;R&lt;/span&gt;&#xD;
              &lt;span style="color: rgb(204,51,255)"&gt;d&lt;/span&gt; Note&lt;/strong&gt;&#xD;
          &lt;/div&gt;&#xD;
          &lt;div style="margin-top: 10px; margin-bottom: 10px"&gt;The &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;toQuintuplets&lt;/span&gt; function&#xD;
defined above will work well for this particular problem but will stack overflow if&#xD;
passed too large of a list because it is head-recursive. There are a couple of ways&#xD;
to make it tail-recursive and avoid blowing the stack, but the most elegant is to&#xD;
employ a &lt;a href="http://en.wikipedia.org/wiki/Continuation"&gt;continuation&lt;/a&gt;.&#xD;
&lt;/div&gt;&#xD;
          &lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;&#xD;
            &lt;span style="color: blue"&gt;let&lt;/span&gt; toQuintuplets&#xD;
l =&lt;br&gt;&#xD;
  &lt;span style="color: blue"&gt;let&lt;/span&gt; &lt;span style="color: blue"&gt;rec&lt;/span&gt; loop&#xD;
l cont =&lt;br&gt;&#xD;
    &lt;span style="color: blue"&gt;match&lt;/span&gt; l &lt;span style="color: blue"&gt;with&lt;/span&gt;&lt;br&gt;&#xD;
    | x1::(x2::x3::x4::x5::_ &lt;span style="color: blue"&gt;as&lt;/span&gt; t) &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; loop&#xD;
t (&lt;span style="color: blue"&gt;fun&lt;/span&gt; l –&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt; cont&#xD;
((x1,x2,x3,x4,x5)::l))&lt;br&gt;&#xD;
    | _ &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; cont []&lt;br&gt;&lt;br&gt;&#xD;
  loop l (&lt;span style="color: blue"&gt;fun&lt;/span&gt; x &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; x)&lt;br&gt;&lt;/div&gt;&#xD;
          &lt;div style="margin-top: 10px"&gt;Of course, that unnecessarily complicates this solution&#xD;
by turning the logic inside out!&#xD;
&lt;/div&gt;&#xD;
        &lt;/div&gt;&#xD;
        &lt;p&gt;&#xD;
The last function we’ll define is a small helper to produce the product of all of&#xD;
the values in a quintuplet.&#xD;
&lt;/p&gt;&#xD;
        &lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;&#xD;
          &lt;span style="color: blue"&gt;let&lt;/span&gt; product&#xD;
(x1,x2,x3,x4,x5) = x1*x2*x3*x4*x5&#xD;
&lt;/div&gt;&#xD;
        &lt;p&gt;&#xD;
All of the pieces are now in place, and the steps can be combined to solve &lt;a href="http://projecteuler.net/"&gt;Project&#xD;
Euler&lt;/a&gt;&lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=8"&gt;problem&#xD;
eight&lt;/a&gt;.&#xD;
&lt;/p&gt;&#xD;
        &lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;number&lt;br&gt;&#xD;
  |&amp;gt; toQuintuplets&lt;br&gt;&#xD;
  |&amp;gt; List.map product&lt;br&gt;&#xD;
  |&amp;gt; List.max &#xD;
&lt;/div&gt;&#xD;
        &lt;p&gt;&#xD;
It’s that simple!&#xD;
&lt;/p&gt;&#xD;
        &lt;p style="border-top: rgb(221,221,221) 1px solid"&gt;&#xD;
          &lt;sup&gt;1&lt;/sup&gt;Or pirates.&#xD;
&lt;/p&gt;&#xD;
        &lt;img width="0" height="0" src="http://diditwith.net/aggbug.ashx?id=2e8c82a9-37f5-474a-9dfc-f5ed98a215e9"&gt;&lt;/img&gt;&#xD;
      &lt;/body&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=CVwRSaI9AdU:NBDsyPbZOjc:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=CVwRSaI9AdU:NBDsyPbZOjc:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=CVwRSaI9AdU:NBDsyPbZOjc:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?i=CVwRSaI9AdU:NBDsyPbZOjc:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=CVwRSaI9AdU:NBDsyPbZOjc:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?i=CVwRSaI9AdU:NBDsyPbZOjc:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=CVwRSaI9AdU:NBDsyPbZOjc:cGdyc7Q-1BI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=cGdyc7Q-1BI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=CVwRSaI9AdU:NBDsyPbZOjc:7qCK26UlVX8"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=7qCK26UlVX8" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/DidItWithDotNet?a=CVwRSaI9AdU:NBDsyPbZOjc:cTv1dNCI_Tc"&gt;&lt;img src="http://feeds.feedburner.com/~ff/DidItWithDotNet?d=cTv1dNCI_Tc" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/DidItWithDotNet/~4/CVwRSaI9AdU" height="1" width="1"/&gt;</description>
      <comments>http://diditwith.net/CommentView,guid,2e8c82a9-37f5-474a-9dfc-f5ed98a215e9.aspx</comments>
      <category>F#</category>
      <category>Functional Programming</category>
      <category>Project Euler</category>
    <feedburner:origLink>http://diditwith.net/2009/03/07/YAPESProblemEight.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://diditwith.net/Trackback.aspx?guid=defeb0a8-4ce8-46c5-86be-e3df29794ff9</trackback:ping>
      <pingback:server>http://diditwith.net/pingback.aspx</pingback:server>
      <pingback:target>http://diditwith.net/PermaLink,guid,defeb0a8-4ce8-46c5-86be-e3df29794ff9.aspx</pingback:target>
      <dc:creator>Dustin Campbell</dc:creator>
      <wfw:comment>http://diditwith.net/CommentView,guid,defeb0a8-4ce8-46c5-86be-e3df29794ff9.aspx</wfw:comment>
      <wfw:commentRss>http://diditwith.net/SyndicationService.asmx/GetEntryCommentsRss?guid=defeb0a8-4ce8-46c5-86be-e3df29794ff9</wfw:commentRss>
      <title>Modifying VB XML Literals at Debug-Time</title>
      <guid isPermaLink="false">http://diditwith.net/PermaLink,guid,defeb0a8-4ce8-46c5-86be-e3df29794ff9.aspx</guid>
      <link>http://feedproxy.google.com/~r/DidItWithDotNet/~3/QMGMdWgJpi0/ModifyingVBXMLLiteralsAtDebugTime.aspx</link>
      <pubDate>Sat, 14 Feb 2009 20:07:24 GMT</pubDate>
      <description>&lt;p&gt;
As a Program Manager at Microsoft, I wear several different hats. First and foremost,
I am the Visual Basic IDE Program Manager, whose job is to keep the powerful engine
that is the &lt;a href="http://msdn.microsoft.com/en-us/vbasic/bb735849.aspx"&gt;VB IDE
Team&lt;/a&gt; running smoothly. However, another hat I wear is that of the Debugger Experience
PM, whose job is to ensure that C#, Visual Basic and, yes, F# programmers have a great
debug-time experience. From the language perspective, this often comes down to making
the right tweaks in the debugger expression evaluators.
&lt;/p&gt;
&lt;p&gt;
So, what are these debugger expression evaluators? The expression evaluators (affectionately
known as the EEs) are magic machinery that deal with language-specific details on
behalf of the core debugging engine that lives inside Visual Studio. In general, the
EEs are responsible for the following:&lt;sup&gt;1&lt;/sup&gt;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Displaying values in &lt;a href="http://msdn.microsoft.com/en-us/library/ea46xwzd.aspx"&gt;datatips&lt;/a&gt;,
and the locals, autos and watch windows (i.e. the &lt;a href="http://msdn.microsoft.com/en-us/library/bhawk8xd.aspx"&gt;variable
windows&lt;/a&gt;) 
&lt;li&gt;
Executing code that has been typed into a &lt;a href="http://msdn.microsoft.com/en-us/library/0taedcee.aspx"&gt;watch&lt;/a&gt;,
the &lt;a href="http://msdn.microsoft.com/en-us/library/f177hahy.aspx"&gt;Immediate Window&lt;/a&gt;,
a &lt;a href="http://msdn.microsoft.com/en-us/library/7sye83ce.aspx"&gt;breakpoint condition&lt;/a&gt;,
etc. and passing the result back to the debugger 
&lt;li&gt;
Processing &lt;a href="http://msdn.microsoft.com/en-us/library/aa6fy2x5.aspx"&gt;edited
values&lt;/a&gt; at debug-time&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Notice that all three of the items above must be tailored to the language that the
user is developing in. For example, it doesn’t make sense to display the C#-style
hexadecimal value, 0x2a, to Visual Basic users. VB users expect to see the value rendered
in the Visual Basic style for hexadecimal numbers, &amp;amp;H2A. Thus, to keep the experiences
consistent, C# and VB both provide their own debugger expression evaluators.&lt;sup&gt;2&lt;/sup&gt;
&lt;/p&gt;
&lt;p&gt;
Recently, I was playing with a bit of Visual Basic code using an XML literal.
&lt;/p&gt;
&lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;&lt;span style="color: blue"&gt;Module&lt;/span&gt; Module1&lt;br&gt;
&lt;br&gt;
&amp;nbsp; &lt;span style="color: blue"&gt;Sub&lt;/span&gt; Main()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;Dim&lt;/span&gt; contacts = &lt;span style="color: #6464b9"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #844646"&gt;contacts&lt;/span&gt;&lt;span style="color: #6464b9"&gt;&amp;gt;&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #6464b9"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #844646"&gt;contact&lt;/span&gt; &lt;span style="color: #b96464"&gt;name&lt;/span&gt;&lt;span style="color: #6464b9"&gt;=&lt;/span&gt;&lt;span style="color: #555555"&gt;"&lt;/span&gt;&lt;span style="color: #6464b9"&gt;Bob&lt;/span&gt;&lt;span style="color: #555555"&gt;"&lt;/span&gt;&lt;span style="color: #6464b9"&gt;/&amp;gt;&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #6464b9"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #844646"&gt;contact&lt;/span&gt; &lt;span style="color: #b96464"&gt;name&lt;/span&gt;&lt;span style="color: #6464b9"&gt;=&lt;/span&gt;&lt;span style="color: #555555"&gt;"&lt;/span&gt;&lt;span style="color: #6464b9"&gt;Sally&lt;/span&gt;&lt;span style="color: #555555"&gt;"&lt;/span&gt;&lt;span style="color: #6464b9"&gt;/&amp;gt;&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #6464b9"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #844646"&gt;contacts&lt;/span&gt;&lt;span style="color: #6464b9"&gt;&amp;gt;&lt;/span&gt;
&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(contacts)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;End&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue"&gt;Sub&lt;/span&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="color: blue"&gt;End&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue"&gt;Module&lt;/span&gt; 
&lt;/div&gt;
&lt;p&gt;
If you’ve never seen a VB XML literal before, know that they are beautiful and powerful
language constructs.&lt;sup&gt;3&lt;/sup&gt; Essentially, the code above is equivalent to the
following C# code.
&lt;/p&gt;
&lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; contacts
= &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af"&gt;XElement&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"contacts"&lt;/span&gt;,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af"&gt;XElement&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"contact"&lt;/span&gt;, &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af"&gt;XAttribute&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"name"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"Bob"&lt;/span&gt;)),&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af"&gt;XElement&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"contact"&lt;/span&gt;, &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af"&gt;XAttribute&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"name"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"Sally"&lt;/span&gt;)));&lt;br&gt;
&lt;br&gt;
&lt;span  ?color:#2B91AF;?&gt;Console&lt;/span&gt;.WriteLine(contacts); 
&lt;/div&gt;
&lt;p&gt;
If I set a breakpoint in the VB code above on &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;Console.WriteLine(contacts)&lt;/span&gt; and
press F5 to start debugging, I can see in the Locals window that my XML literal has
effectively produced the same tree of &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;XElement&lt;/span&gt; and &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;XAttribute&lt;/span&gt; objects
that the C# code explicitly declares above.
&lt;/p&gt;
&lt;p&gt;
&lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" title="image" border="0" alt="Locals" src="http://diditwith.net/content/binary/WindowsLiveWriter/VBTrickEditingXMLLiteralsatDebugTime_F437/image_6.png" width="500" height="367"&gt;
&lt;/p&gt;
&lt;p&gt;
Now, suppose that I’ve made a mistake when declaring the XML literal (e.g. “Bob” should
be “Harry”), but I don’t want to stop debugging, fix the code, and F5 to start again.
I have a couple of options available to me for modifying values at debug-time.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
I can use &lt;a href="http://msdn.microsoft.com/en-us/library/bcew296c.aspx"&gt;Edit and
Continue&lt;/a&gt;. This feature allows me to change my code at debug-time and move the
instruction pointer in order to execute the modified code. However, this might not
always be convenient. 
&lt;li&gt;
I can edit the value directly in one of the variable windows, the datatip, or the
Immediate Window.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
Choosing option two above, I’ll fix my mistake by editing the XML value directly in
the Locals Window.
&lt;/p&gt;
&lt;p&gt;
&lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" title="image" border="0" alt="EditingWhenNotRemovingQuotes" src="http://diditwith.net/content/binary/WindowsLiveWriter/VBTrickEditingXMLLiteralsatDebugTime_F437/EditingWhenNotRemovingQuotes_3.png" width="500" height="78"&gt;
&lt;/p&gt;
&lt;p&gt;
Upon pressing ENTER, I’m presented with the following error dialog.
&lt;/p&gt;
&lt;p&gt;
&lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" title="image" border="0" alt="EndOfStatementExpected" src="http://diditwith.net/content/binary/WindowsLiveWriter/VBTrickEditingXMLLiteralsatDebugTime_F437/EndOfStatementExpected_3.png" width="263" height="171"&gt;
&lt;/p&gt;
&lt;p&gt;
What the heck does that mean? Well, this is the message that would be displayed by
the Visual Basic compiler for a string value with inner quotes that are not escaped.&lt;sup&gt;4&lt;/sup&gt;
&lt;/p&gt;
&lt;p&gt;
Attempt number two: I’ll edit the value again and be careful to escape the quotes.
&lt;/p&gt;
&lt;p&gt;
&lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" alt="EditingAndEscapingQuotes" src="http://diditwith.net/content/binary/WindowsLiveWriter/VBTrickEditingXMLLiteralsatDebugTime_F437/EditingAndEscapingQuotes_3.png" width="500" height="61"&gt;
&lt;/p&gt;
&lt;p&gt;
This time, when I press ENTER, I get yet another error message.
&lt;/p&gt;
&lt;p&gt;
&lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" title="ValueOfTypeStringCannotBeConvertedToXElement" border="0" alt="ValueOfTypeStringCannotBeConvertedToXElement" src="http://diditwith.net/content/binary/WindowsLiveWriter/VBTrickEditingXMLLiteralsatDebugTime_F437/ValueOfTypeStringCannotBeConvertedToXElement_3.png" width="417" height="171"&gt; 
&lt;/p&gt;
&lt;p&gt;
Grrr. Now I’m getting irritated!
&lt;/p&gt;
&lt;p&gt;
Fortunately, the error message above gives me the clue I need to make this work. The
problem is that I’m producing a string value, and I should be producing an XML value.
What do I need to do differently? I need to remove the quotes that are enclosing the
whole XML value.
&lt;/p&gt;
&lt;p&gt;
Last try.
&lt;/p&gt;
&lt;p&gt;
&lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" title="image" border="0" alt="Editing XML in Locals" src="http://diditwith.net/content/binary/WindowsLiveWriter/VBTrickEditingXMLLiteralsatDebugTime_F437/image_4.png" width="500" height="73"&gt;
&lt;/p&gt;
&lt;p&gt;
Success!
&lt;/p&gt;
&lt;p&gt;
This time, I’m rewarded with a red display color for the value, indicating that the
value has changed. If I expand some of the inner nodes, I can see that the tree of &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;XElement&lt;/span&gt; and &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;XAttribute&lt;/span&gt; objects
has indeed been rebuilt.
&lt;/p&gt;
&lt;p&gt;
&lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" title="image" border="0" alt="Modified XML in Locals" src="http://diditwith.net/content/binary/WindowsLiveWriter/VBTrickEditingXMLLiteralsatDebugTime_F437/image_8.png" width="500" height="282"&gt;
&lt;/p&gt;
&lt;p&gt;
That’s a pretty nifty trick that you can use &lt;strong&gt;right now&lt;/strong&gt; in Visual
Studio 2008. However, when I put on my Debugger PM hat, I consider how to improve
this experience for &lt;a href="http://www.microsoft.com/visualstudio/en-us/products/2010/default.mspx"&gt;Visual
Studio 2010&lt;/a&gt;. Easy! &lt;em&gt;We’ve removed the enclosing double-quotes from XElement
values in the Visual Basic Expression Evaluator.&lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
Sometimes achieving the best experience is just a simple tweak away.
&lt;/p&gt;
&lt;p style="border-top: rgb(221,221,221) 1px solid"&gt;
&lt;sup&gt;1&lt;/sup&gt;This is not an exhaustive list.&lt;br&gt;
&lt;sup&gt;2&lt;/sup&gt;F# currently uses the C# EE, but eventually F# will provide its own. Feel
free to ping &lt;a href="http://blogs.msdn.com/lukeh/default.aspx"&gt;Luke&lt;/a&gt; and &lt;a href="http://blogs.msdn.com/timng/default.aspx"&gt;Tim&lt;/a&gt; to
help make this happen.&lt;br&gt;
&lt;sup&gt;3&lt;/sup&gt;For a good example of how powerful XML literals are, see Dmitry Robsman's &lt;a href="http://blogs.msdn.com/dmitryr/archive/2008/12/29/asp-net-mvc-view-engine-using-vb-net-xml-literals.aspx"&gt;ASP
.NET MVC Engine Using VB.NET XML Literals&lt;/a&gt;.&lt;br&gt;
&lt;sup&gt;4&lt;/sup&gt;Displaying this string without escaping the quotes strikes me as a bug.
:-)
&lt;/p&gt;
&lt;img width="0" height="0" src="http://diditwith.net/aggbug.ashx?id=defeb0a8-4ce8-46c5-86be-e3df29794ff9" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=iLx0giJd"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?d=41" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=YQcVA3P4"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?d=43" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=4hvLxjtF"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?i=4hvLxjtF" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=0P8Dmjz7"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?i=0P8Dmjz7" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=OFKD6yoh"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?d=131" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=5cn0Wpi4"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?d=1617" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=lZgNKAJG"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?d=300" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/DidItWithDotNet/~4/QMGMdWgJpi0" height="1" width="1"/&gt;</description>
      <comments>http://diditwith.net/CommentView,guid,defeb0a8-4ce8-46c5-86be-e3df29794ff9.aspx</comments>
      <category>Debugging</category>
      <category>Tips &amp; Tricks</category>
      <category>Visual Basic</category>
      <category>XML Literals</category>
    <feedburner:origLink>http://diditwith.net/2009/02/14/ModifyingVBXMLLiteralsAtDebugTime.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://diditwith.net/Trackback.aspx?guid=72a12a68-a264-45aa-8217-c8ea994cc866</trackback:ping>
      <pingback:server>http://diditwith.net/pingback.aspx</pingback:server>
      <pingback:target>http://diditwith.net/PermaLink,guid,72a12a68-a264-45aa-8217-c8ea994cc866.aspx</pingback:target>
      <dc:creator>Dustin Campbell</dc:creator>
      <wfw:comment>http://diditwith.net/CommentView,guid,72a12a68-a264-45aa-8217-c8ea994cc866.aspx</wfw:comment>
      <wfw:commentRss>http://diditwith.net/SyndicationService.asmx/GetEntryCommentsRss?guid=72a12a68-a264-45aa-8217-c8ea994cc866</wfw:commentRss>
      
      <title>Dustin and Chris, Deep Fried</title>
      <guid isPermaLink="false">http://diditwith.net/PermaLink,guid,72a12a68-a264-45aa-8217-c8ea994cc866.aspx</guid>
      <link>http://feedproxy.google.com/~r/DidItWithDotNet/~3/VqlARPU5xxM/DustinAndChrisDeepFried.aspx</link>
      <pubDate>Tue, 20 Jan 2009 08:01:22 GMT</pubDate>
      <description>&lt;body xmlns="http://www.w3.org/1999/xhtml"&gt;&#xD;
        &lt;p&gt;&#xD;
A few months ago, I was honored to record an episode of &lt;a href="http://deepfriedbytes.com/"&gt;Deep&#xD;
Fried Bytes&lt;/a&gt; my good friend (and partner-in-crime) &lt;a href="http://blogs.msdn.com/chrsmith/"&gt;Chris&#xD;
Smith&lt;/a&gt;. We blabbed on about F#, functional programming, pink vodka. The usual stuff.&#xD;
&lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
Check it out!&#xD;
&lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
          &lt;a href="http://deepfriedbytes.com/podcast/episode-24-chatting-about-f-with-chris-smith-and-dustin-campbell/"&gt;Episode&#xD;
24: Chatting about F# with Chris Smith and Dustin Campbell&lt;/a&gt;&#xD;
        &lt;/p&gt;&#xD;
        &lt;img width="0" height="0" src="http://diditwith.net/aggbug.ashx?id=72a12a68-a264-45aa-8217-c8ea994cc866"&gt;&lt;/img&gt;&#xD;
      &lt;/body&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=onrPVcLL"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?d=41" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=ApN2FR1V"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?d=43" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=5KMTcyfw"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?i=5KMTcyfw" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=UORSJbtU"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?i=UORSJbtU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=UL7ecECN"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?d=131" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=OcUIctOd"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?d=1617" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=TRCrTKty"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?d=300" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/DidItWithDotNet/~4/VqlARPU5xxM" height="1" width="1"/&gt;</description>
      <comments>http://diditwith.net/CommentView,guid,72a12a68-a264-45aa-8217-c8ea994cc866.aspx</comments>
      <category>Community</category>
      <category>F#</category>
      <category>It's all about me</category>
      <category>Speaking</category>
    <feedburner:origLink>http://diditwith.net/2009/01/20/DustinAndChrisDeepFried.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://diditwith.net/Trackback.aspx?guid=82535152-f7e2-4051-afd6-2330aea0bdf9</trackback:ping>
      <pingback:server>http://diditwith.net/pingback.aspx</pingback:server>
      <pingback:target>http://diditwith.net/PermaLink,guid,82535152-f7e2-4051-afd6-2330aea0bdf9.aspx</pingback:target>
      <dc:creator>Dustin Campbell</dc:creator>
      <wfw:comment>http://diditwith.net/CommentView,guid,82535152-f7e2-4051-afd6-2330aea0bdf9.aspx</wfw:comment>
      <wfw:commentRss>http://diditwith.net/SyndicationService.asmx/GetEntryCommentsRss?guid=82535152-f7e2-4051-afd6-2330aea0bdf9</wfw:commentRss>
      <slash:comments>6</slash:comments>
      <title>YAPES: Problem Seven, Part 2</title>
      <guid isPermaLink="false">http://diditwith.net/PermaLink,guid,82535152-f7e2-4051-afd6-2330aea0bdf9.aspx</guid>
      <link>http://feedproxy.google.com/~r/DidItWithDotNet/~3/rUqfnAuOfQQ/YAPESProblemSevenPart2.aspx</link>
      <pubDate>Tue, 20 Jan 2009 07:44:09 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://diditwith.net/2008/09/17/YAPESProblemSevenPart1.aspx"&gt;Last time&lt;/a&gt;,
we took a brute force approach to solving &lt;a href="http://projecteuler.net"&gt;Project
Euler&lt;/a&gt; &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=7"&gt;problem
seven&lt;/a&gt;. Unfortunately, the resulting prime number generator turned out to be fairly
ugly and not really efficient enough to handle the needs of future YAPES&lt;sup&gt;1&lt;/sup&gt; problems.
This time, we’ll approach the problem using a tried-and-true algorithm that should
give us the desired performance &lt;em&gt;and&lt;/em&gt; produce a beautiful solution. I can already
hear some of you rolling your eyes, because we’ll be looking at...
&lt;/p&gt;
&lt;h3&gt;The Sieve of Eratosthenes
&lt;/h3&gt;
&lt;p&gt;
Amazingly, the &lt;a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes"&gt;Sieve
of Eratosthenes&lt;/a&gt; is perhaps the oldest known algorithm for generating prime numbers,
and yet it remains reasonably efficient by today’s standards. It’s true that there
are &lt;a href="http://en.wikipedia.org/wiki/Sieve_of_Atkin"&gt;faster methods&lt;/a&gt; available,
but this should be fast enough. The basic algorithm is nothing earth-shattering—it’s
first semester computer science stuff. However, we’ll add a twist to keep things interesting.
&lt;/p&gt;
&lt;p&gt;
The Sieve of Eratosthenes works by marking &lt;a href="http://en.wikipedia.org/wiki/Composite_numbers"&gt;composite
numbers&lt;/a&gt;:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Start with the natural numbers from 2 up to some maximum limit. 
&lt;li&gt;
Take the first unmarked number (2) and mark all multiples of that number. Note that
2 remains unmarked. Only its multiples (e.g. 4,6,8,…) are marked. 
&lt;li&gt;
Take the next unmarked number and mark all of its multiples. 
&lt;li&gt;
Repeat step 3 until there are no more numbers left to test. 
&lt;li&gt;
When finished, the unmarked numbers are the primes.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
As described, this algorithm doesn’t quite work for our purposes. It is certainly
easy to implement, but because of the starting requirement (a big list of numbers),
it doesn’t lend itself well to generating a, potentially, infinite stream of primes.
However, with a small modification, we can make it work.
&lt;/p&gt;
&lt;p&gt;
The adaptation we’ll implement comes from from the excellent paper, &lt;a href="http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf"&gt;The
Genuine Sieve of Eratosthenes&lt;/a&gt;&lt;sup&gt;2&lt;/sup&gt;, by Melissa E. O’Neill. The idea is
to turn the algorithm inside out. Instead of keeping track of which numbers are prime,
we’ll maintain a table of composite numbers along with their known factors. As we
iterate the natural numbers, the table will be updated with new information about
which numbers are known to be composite and what factors have been found.
&lt;/p&gt;
&lt;p&gt;
Confused? Let’s walk through the algorithm, step by step.
&lt;/p&gt;
&lt;h3&gt;Tracking Composites
&lt;/h3&gt;
&lt;p&gt;
We start with 2 as the first natural number and check to see if it exists in the composite
table. Since the table is empty, 2 isn’t present, so we add a new list containing
2 to the table with the square of 2 (4) as the key. Finally, since 2 was not found
in the composite table, we know it is prime.
&lt;/p&gt;
&lt;table border="0" cellspacing="0" cellpadding="0" width="300"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td width="300" align="right"&gt;
&lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" alt="Sieve Diagram 1 (after 2)" src="http://diditwith.net/content_images/Sieve1After2.png"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
We take the next natural number, 3. Like before, 3 is not found in the composite table,
so we add it to the table using its square (9) as the key and return 3 as prime.
&lt;/p&gt;
&lt;table border="0" cellspacing="0" cellpadding="0" width="300"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td width="300" align="right"&gt;
&lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" alt="Sieve Diagram 2 (after 3)" src="http://diditwith.net/content_images/Sieve2After3.png"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
We take the next natural number, 4. This time is different—4 is found in the composite
table, so we know it’s not prime. In this case, we take the list found in the table
under 4 (the known factors), and for each factor, we add 4 and insert the result into
the table. Thus, we insert 6 into the table along with its known factor, 2. (Interestingly,
this algorithm doesn’t tell us that 3 is a factor of 6). Finally, we remove 4 from
the table. 
&lt;/p&gt;
&lt;table border="0" cellspacing="0" cellpadding="0" width="300"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td width="300" align="right"&gt;
&lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" alt="Sieve Diagram 3 (after 4)" src="http://diditwith.net/content_images/Sieve3After4.png"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
Let's skip ahead to some more interesting cases.
&lt;/p&gt;
&lt;p&gt;
After 9 has been processed, the table looks like so:
&lt;/p&gt;
&lt;table border="0" cellspacing="0" cellpadding="0" width="300"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td width="300" align="right"&gt;
&lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" alt="Sieve Diagram 4 (after 9)" src="http://diditwith.net/content_images/Sieve4After9.png"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
The next natural number, 10, is found in the table, so we know that it’s composite
and not prime. Again, we take the list of known factors, and for each factor, we add
10 and insert the result into the table. This time, the value (12) already exists
in the table, so we add it to the list of known factors for that value. Lastly, we
remove 10 from the table.
&lt;/p&gt;
&lt;table border="0" cellspacing="0" cellpadding="0" width="300"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td width="300" align="right"&gt;
&lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" alt="Sieve Diagram 5 (after 10)" src="http://diditwith.net/content_images/Sieve5After10.png"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
The next natural number, 11, is not found in the table, so we insert its square and
return it as prime.
&lt;/p&gt;
&lt;table border="0" cellspacing="0" cellpadding="0" width="300"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td width="300" align="right"&gt;
&lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" alt="Sieve Diagram 6 (after 11)" src="http://diditwith.net/content_images/Sieve6After11.png"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
We find the next natural number, 12, in the table. However, because there is more
than one known factor in the table, we add two new composites, 12+2=14 and 12+3=15,
before finally removing 12.
&lt;/p&gt;
&lt;table border="0" cellspacing="0" cellpadding="0" width="300"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td width="300" align="right"&gt;
&lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" alt="Sieve Diagram 7 (after 12)" src="http://diditwith.net/content_images/Sieve7After12.png"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
As you can see, this take on the classic algorithm allows us to determine which numbers
are prime and composite by using a minimal amount of memory. Obviously, the table
can grow large as we find larger primes, but the memory usage is quite reasonable
relative to the traditional sieve where a large list of natural numbers would have
to be generated before even the first prime is returned.
&lt;/p&gt;
&lt;h3&gt;Show Me Some Code!
&lt;/h3&gt;
&lt;p&gt;
The F# code nearly writes itself.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;&lt;span style="color: blue"&gt;let&lt;/span&gt; reinsert
x table prime = 
&lt;br&gt;
&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;let&lt;/span&gt; comp = x+prime 
&lt;br&gt;
&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;match&lt;/span&gt; Map.tryfind comp table &lt;span style="color: blue"&gt;with&lt;/span&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp; | None&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; table
|&amp;gt; Map.add comp [prime] 
&lt;br&gt;
&amp;nbsp;&amp;nbsp; | Some(facts) &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; table |&amp;gt; Map.add
comp (prime::facts) 
&lt;br&gt;
&lt;br&gt;
&lt;span style="color: blue"&gt;let&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue"&gt;rec&lt;/span&gt; sieve
x table = 
&lt;br&gt;
&amp;nbsp; seq { 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;match&lt;/span&gt; Map.tryfind x table &lt;span style="color: blue"&gt;with&lt;/span&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; | None &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;yield&lt;/span&gt; x 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;yield!&lt;/span&gt; sieve
(x+1L) (table |&amp;gt; Map.add (x*x) [x]) 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; | Some(factors) &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;yield!&lt;/span&gt; sieve
(x+1L) (factors |&amp;gt; List.fold_left (reinsert x) (table |&amp;gt; Map.remove x)) } 
&lt;br&gt;
&lt;br&gt;
&lt;span style="color: blue"&gt;let&lt;/span&gt; primes = 
&lt;br&gt;
&amp;nbsp; sieve 2L Map.empty 
&lt;/div&gt;
&lt;p&gt;
The meat of the algorithm is in the &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;sieve&lt;/span&gt; function.
Here, we try to locate &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;x&lt;/span&gt; in
the table. If it is not found, we yield &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;x&lt;/span&gt; (because
it’s prime) and recursively call &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;sieve&lt;/span&gt; passing &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;x+1&lt;/span&gt; and
a table with the square of &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;x&lt;/span&gt; added
to it. (Note that &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;Map&lt;/span&gt; is
an immutable data structure, so “mutating” functions like &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;Map.add&lt;/span&gt; actually
return a brand new &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;Map&lt;/span&gt;.)
If &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;x&lt;/span&gt; is
found in the table, we recursively call &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;sieve&lt;/span&gt; passing &lt;span style="font-family: consolas,'Courier New',courier,monospace" new?,courier,monospace;? font-family: consolas,?Courier&gt;x+1&lt;/span&gt; and
a table from which &lt;span style="font-family: consolas,'Courier New',courier,monospace" new?,courier,monospace;? font-family: consolas,?Courier&gt;x&lt;/span&gt; has
been removed and each known factor has been reinserted. The &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;reinsert&lt;/span&gt; function
performs the trivial work of reinserting a known factor into the table.
&lt;/p&gt;
&lt;p&gt;
A couple of other interesting details about this code are worth pointing out:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;sieve&lt;/span&gt; returns
a sequence expression, denoted by the &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;seq&lt;/span&gt; keyword
and the curly braces. Sequence expressions are lazily evaluated, meaning that the
next prime won't be calculated until requested. In addition, the sequence expression
is recursive due to the use of the &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;yield!&lt;/span&gt; keyword,
which can be used to return another sequence expression as part of a sequence. 
&lt;li&gt;
The call to &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;List.fold_left&lt;/span&gt; might
look a bit exotic because we are folding over the list of known factors to produce
a new table with each factor reinserted.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Mutation Isn't All Bad
&lt;/h3&gt;
&lt;p&gt;
Now that we have a working prime number generator, let’s take a moment to optimize.
Until now, we’ve been sticking with the immutable F# &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;Map&lt;/span&gt; type,
but we &lt;em&gt;could&lt;/em&gt; employ a mutable data structure to improve performance. To replace
the data structure in a way that preserves the beauty of the algorithm, we’ll define
a set of APIs for &lt;a href="http://msdn.microsoft.com/en-us/library/xfhwa508.aspx"&gt;&lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;Dictionary&amp;lt;TKey,TValue&amp;gt;&lt;/span&gt;&lt;/a&gt; that
mimic the &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;Map&lt;/span&gt; APIs:
&lt;/p&gt;
&lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;&lt;span style="color: blue"&gt;module&lt;/span&gt; Dict
= 
&lt;br&gt;
&amp;nbsp; &lt;span style="color: blue"&gt;open&lt;/span&gt; System.Collections.Generic 
&lt;br&gt;
&lt;br&gt;
&amp;nbsp; &lt;span style="color: blue"&gt;let&lt;/span&gt; empty() = &lt;span style="color: blue"&gt;new&lt;/span&gt; Dictionary&amp;lt;_,_&amp;gt;() 
&lt;br&gt;
&lt;br&gt;
&amp;nbsp; &lt;span style="color: blue"&gt;let&lt;/span&gt; add k v (d : #IDictionary&amp;lt;'a,'b&amp;gt;)
= 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; d.[k] &amp;lt;- v; d 
&lt;br&gt;
&lt;br&gt;
&amp;nbsp; &lt;span style="color: blue"&gt;let&lt;/span&gt; remove (k : 'a) (d : #IDictionary&amp;lt;'a,'b&amp;gt;)
= 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; d.Remove(k) |&amp;gt; ignore; d 
&lt;br&gt;
&lt;br&gt;
&amp;nbsp; &lt;span style="color: blue"&gt;let&lt;/span&gt; tryfind k (d : #IDictionary&amp;lt;'a,'b&amp;gt;)
= 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;match&lt;/span&gt; d.TryGetValue(k) &lt;span style="color: blue"&gt;with&lt;/span&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; | true, v&amp;nbsp; &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; Some(v) 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; | false, _ &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; None 
&lt;/div&gt;
&lt;p&gt;
Now, it’s a simple matter of replacing references to &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;Map&lt;/span&gt; with &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;Dict&lt;/span&gt;,
and our algorithm immediately benefits from the performance boost of using a mutable
table.
&lt;/p&gt;
&lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;&lt;span style="color: blue"&gt;let&lt;/span&gt; reinsert
x table prime = 
&lt;br&gt;
&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;let&lt;/span&gt; comp = x+prime 
&lt;br&gt;
&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;match&lt;/span&gt; Dict.tryfind comp table &lt;span style="color: blue"&gt;with&lt;/span&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp; | None&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; table
|&amp;gt; Dict.add comp [prime] 
&lt;br&gt;
&amp;nbsp;&amp;nbsp; | Some(facts) &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; table |&amp;gt; Dict.add
comp (prime::facts) 
&lt;br&gt;
&lt;br&gt;
&lt;span style="color: blue"&gt;let&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue"&gt;rec&lt;/span&gt; sieve
x table = 
&lt;br&gt;
&amp;nbsp; seq { 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;match&lt;/span&gt; Dict.tryfind x table &lt;span style="color: blue"&gt;with&lt;/span&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; | None &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;yield&lt;/span&gt; x 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;yield!&lt;/span&gt; sieve
(x+1L) (table |&amp;gt; Dict.add (x*x) [x]) 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; | Some(factors) &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;yield!&lt;/span&gt; sieve
(x+1L) (factors |&amp;gt; List.fold_left (reinsert x) (table |&amp;gt; Dict.remove x)) } 
&lt;br&gt;
&lt;br&gt;
&lt;span style="color: blue"&gt;let&lt;/span&gt; primes = 
&lt;br&gt;
&amp;nbsp; sieve 2L (Dict.empty())
&lt;/div&gt;
&lt;p&gt;
With our prime number generator in hand, solving Project Euler problem seven is trivial:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;primes
|&amp;gt; Seq.nth 10000
&lt;/div&gt;
&lt;p&gt;
At last, we’ve produced a prime number generator with a good balance of performance
and beauty. If we’ve learned anything from this article or its &lt;a href="http://diditwith.net/2008/09/17/YAPESProblemSevenPart1.aspx"&gt;predecessor&lt;/a&gt;,
I hope it’s that finding an optimal solution often requires finding an appropriate
algorithm.
&lt;/p&gt;
&lt;p&gt;
Take that &lt;a href="http://blogs.msdn.com/chrsmith/"&gt;Chris&lt;/a&gt;!
&lt;/p&gt;
&lt;p style="border-top: rgb(221,221,221) 1px solid"&gt;
&lt;sup&gt;1&lt;/sup&gt;Yet Another Project Euler Series&lt;br&gt;
&lt;sup&gt;2&lt;/sup&gt;O'Neill's paper appears in The Journal of Functional Programming, Volume
19, Issue 01.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://diditwith.net/aggbug.ashx?id=82535152-f7e2-4051-afd6-2330aea0bdf9" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=SHu04fnt"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?d=41" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=6IEOok9t"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?d=43" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=0pwpgu7r"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?i=0pwpgu7r" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=xW2lwjHy"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?i=xW2lwjHy" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=u0MWPxb1"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?d=131" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=mMY9vLSy"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?d=1617" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=KHoxBWbh"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?d=300" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/DidItWithDotNet/~4/rUqfnAuOfQQ" height="1" width="1"/&gt;</description>
      <comments>http://diditwith.net/CommentView,guid,82535152-f7e2-4051-afd6-2330aea0bdf9.aspx</comments>
      <category>F#</category>
      <category>Functional Programming</category>
      <category>Project Euler</category>
      <category>Quality Code</category>
    <feedburner:origLink>http://diditwith.net/2009/01/20/YAPESProblemSevenPart2.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://diditwith.net/Trackback.aspx?guid=befb5f67-34d7-4ab2-a510-eb94cb4f6666</trackback:ping>
      <pingback:server>http://diditwith.net/pingback.aspx</pingback:server>
      <pingback:target>http://diditwith.net/PermaLink,guid,befb5f67-34d7-4ab2-a510-eb94cb4f6666.aspx</pingback:target>
      <dc:creator>Dustin Campbell</dc:creator>
      <wfw:comment>http://diditwith.net/CommentView,guid,befb5f67-34d7-4ab2-a510-eb94cb4f6666.aspx</wfw:comment>
      <wfw:commentRss>http://diditwith.net/SyndicationService.asmx/GetEntryCommentsRss?guid=befb5f67-34d7-4ab2-a510-eb94cb4f6666</wfw:commentRss>
      <slash:comments>2</slash:comments>
      
      <title>Printf and Formatting Debug Output in F#</title>
      <guid isPermaLink="false">http://diditwith.net/PermaLink,guid,befb5f67-34d7-4ab2-a510-eb94cb4f6666.aspx</guid>
      <link>http://feedproxy.google.com/~r/DidItWithDotNet/~3/t0Xr7M2_ztg/PrintfAndFormattingDebugOutputInF.aspx</link>
      <pubDate>Mon, 29 Dec 2008 19:35:14 GMT</pubDate>
      <description>&lt;body xmlns="http://www.w3.org/1999/xhtml"&gt;&#xD;
        &lt;p&gt;&#xD;
The F# library provides a variety of functions (based on the &lt;a href="http://en.wikipedia.org/wiki/Printf"&gt;printf&lt;/a&gt; functions&#xD;
found in &lt;a href="http://en.wikipedia.org/wiki/Ocaml"&gt;OCaml&lt;/a&gt;) that produce formatted&#xD;
text.&#xD;
&lt;/p&gt;&#xD;
        &lt;table border="0" cellspacing="0" cellpadding="2" width="500"&gt;&#xD;
          &lt;tbody&gt;&#xD;
            &lt;tr&gt;&#xD;
              &lt;td style="border-bottom: rgb(221,221,221) 1px solid; border-left: rgb(221,221,221) 1px solid; padding-bottom: 4px; background-color: rgb(243,243,243); padding-left: 4px; padding-right: 4px; color: rgb(108,108,108); border-top: rgb(221,221,221) 1px solid; padding-top: 4px" width="100"&gt;&#xD;
                &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;printf&lt;/span&gt;&#xD;
              &lt;/td&gt;&#xD;
              &lt;td style="border-bottom: rgb(221,221,221) 1px solid; padding-bottom: 4px; background-color: rgb(243,243,243); padding-left: 4px; padding-right: 4px; color: rgb(108,108,108); font-size: small; border-top: rgb(221,221,221) 1px solid; border-right: rgb(221,221,221) 1px solid; padding-top: 4px" width="400"&gt;&#xD;
outputs formatted text to the console using the stdout stream.&lt;/td&gt;&#xD;
            &lt;/tr&gt;&#xD;
            &lt;tr&gt;&#xD;
              &lt;td style="border-bottom: rgb(221,221,221) 1px solid; border-left: rgb(221,221,221) 1px solid; padding-bottom: 4px; padding-left: 4px; padding-right: 4px; color: rgb(108,108,108); padding-top: 4px" width="100"&gt;&#xD;
                &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;printfn&lt;/span&gt;&#xD;
              &lt;/td&gt;&#xD;
              &lt;td style="border-bottom: rgb(221,221,221) 1px solid; padding-bottom: 4px; padding-left: 4px; padding-right: 4px; color: rgb(108,108,108); font-size: small; border-right: rgb(221,221,221) 1px solid; padding-top: 4px" width="400"&gt;&#xD;
outputs formatted text suffixed with a new-line character to the console using the&#xD;
stdout stream.&lt;/td&gt;&#xD;
            &lt;/tr&gt;&#xD;
            &lt;tr&gt;&#xD;
              &lt;td style="border-bottom: rgb(221,221,221) 1px solid; border-left: rgb(221,221,221) 1px solid; padding-bottom: 4px; background-color: rgb(243,243,243); padding-left: 4px; padding-right: 4px; color: rgb(108,108,108); padding-top: 4px" width="100"&gt;&#xD;
                &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;eprintf&lt;/span&gt;&#xD;
              &lt;/td&gt;&#xD;
              &lt;td style="border-bottom: rgb(221,221,221) 1px solid; padding-bottom: 4px; background-color: rgb(243,243,243); padding-left: 4px; padding-right: 4px; color: rgb(108,108,108); font-size: small; border-right: rgb(221,221,221) 1px solid; padding-top: 4px" width="400"&gt;&#xD;
outputs formatted text to the console using the stderr stream.&lt;/td&gt;&#xD;
            &lt;/tr&gt;&#xD;
            &lt;tr&gt;&#xD;
              &lt;td style="border-bottom: rgb(221,221,221) 1px solid; border-left: rgb(221,221,221) 1px solid; padding-bottom: 4px; padding-left: 4px; padding-right: 4px; color: rgb(108,108,108); padding-top: 4px" width="100"&gt;&#xD;
                &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;eprintfn&lt;/span&gt;&#xD;
              &lt;/td&gt;&#xD;
              &lt;td style="border-bottom: rgb(221,221,221) 1px solid; padding-bottom: 4px; padding-left: 4px; padding-right: 4px; color: rgb(108,108,108); font-size: small; border-right: rgb(221,221,221) 1px solid; padding-top: 4px" width="400"&gt;&#xD;
outputs formatted text suffixed with a new-line character to the console using the&#xD;
stderr stream.&lt;/td&gt;&#xD;
            &lt;/tr&gt;&#xD;
            &lt;tr&gt;&#xD;
              &lt;td style="border-bottom: rgb(221,221,221) 1px solid; border-left: rgb(221,221,221) 1px solid; padding-bottom: 4px; background-color: rgb(243,243,243); padding-left: 4px; padding-right: 4px; color: rgb(108,108,108); padding-top: 4px" width="100"&gt;&#xD;
                &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;sprintf&lt;/span&gt;&#xD;
              &lt;/td&gt;&#xD;
              &lt;td style="border-bottom: rgb(221,221,221) 1px solid; padding-bottom: 4px; background-color: rgb(243,243,243); padding-left: 4px; padding-right: 4px; color: rgb(108,108,108); font-size: small; border-right: rgb(221,221,221) 1px solid; padding-top: 4px" width="400"&gt;&#xD;
returns formatted text as a string.&lt;/td&gt;&#xD;
            &lt;/tr&gt;&#xD;
            &lt;tr&gt;&#xD;
              &lt;td style="border-bottom: rgb(221,221,221) 1px solid; border-left: rgb(221,221,221) 1px solid; padding-bottom: 4px; padding-left: 4px; padding-right: 4px; color: rgb(108,108,108); padding-top: 4px" width="100"&gt;&#xD;
                &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;bprintf&lt;/span&gt;&#xD;
              &lt;/td&gt;&#xD;
              &lt;td style="border-bottom: rgb(221,221,221) 1px solid; padding-bottom: 4px; padding-left: 4px; padding-right: 4px; color: rgb(108,108,108); font-size: small; border-right: rgb(221,221,221) 1px solid; padding-top: 4px" width="400"&gt;&#xD;
appends formatted text to a System.Text.StringBuilder.&lt;/td&gt;&#xD;
            &lt;/tr&gt;&#xD;
            &lt;tr&gt;&#xD;
              &lt;td style="border-bottom: rgb(221,221,221) 1px solid; border-left: rgb(221,221,221) 1px solid; padding-bottom: 4px; background-color: rgb(243,243,243); padding-left: 4px; padding-right: 4px; color: rgb(108,108,108); padding-top: 4px" width="100"&gt;&#xD;
                &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;fprintf&lt;/span&gt;&#xD;
              &lt;/td&gt;&#xD;
              &lt;td style="border-bottom: rgb(221,221,221) 1px solid; padding-bottom: 4px; background-color: rgb(243,243,243); padding-left: 4px; padding-right: 4px; color: rgb(108,108,108); font-size: small; border-right: rgb(221,221,221) 1px solid; padding-top: 4px" width="400"&gt;&#xD;
writes formatted text to a System.IO.TextWriter.&lt;/td&gt;&#xD;
            &lt;/tr&gt;&#xD;
            &lt;tr&gt;&#xD;
              &lt;td style="border-bottom: rgb(221,221,221) 1px solid; border-left: rgb(221,221,221) 1px solid; padding-bottom: 4px; padding-left: 4px; padding-right: 4px; color: rgb(108,108,108); padding-top: 4px" width="100"&gt;&#xD;
                &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;fprintfn&lt;/span&gt;&#xD;
              &lt;/td&gt;&#xD;
              &lt;td style="border-bottom: rgb(221,221,221) 1px solid; padding-bottom: 4px; padding-left: 4px; padding-right: 4px; color: rgb(108,108,108); font-size: small; border-right: rgb(221,221,221) 1px solid; padding-top: 4px" width="400"&gt;&#xD;
writes formatted text suffixed with a new-line character to a System.IO.TextWriter.&lt;/td&gt;&#xD;
            &lt;/tr&gt;&#xD;
            &lt;tr&gt;&#xD;
              &lt;td style="border-bottom: rgb(221,221,221) 1px solid; border-left: rgb(221,221,221) 1px solid; padding-bottom: 4px; background-color: rgb(243,243,243); padding-left: 4px; padding-right: 4px; color: rgb(108,108,108); padding-top: 4px" width="100"&gt;&#xD;
                &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;twprintf&lt;/span&gt;&#xD;
              &lt;/td&gt;&#xD;
              &lt;td style="border-bottom: rgb(221,221,221) 1px solid; padding-bottom: 4px; background-color: rgb(243,243,243); padding-left: 4px; padding-right: 4px; color: rgb(108,108,108); font-size: small; border-right: rgb(221,221,221) 1px solid; padding-top: 4px" width="400"&gt;&#xD;
writes formatted text to a System.IO.TextWriter.&lt;/td&gt;&#xD;
            &lt;/tr&gt;&#xD;
            &lt;tr&gt;&#xD;
              &lt;td style="border-bottom: rgb(221,221,221) 1px solid; border-left: rgb(221,221,221) 1px solid; padding-bottom: 4px; padding-left: 4px; padding-right: 4px; color: rgb(108,108,108); padding-top: 4px" width="100"&gt;&#xD;
                &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;twprintfn&lt;/span&gt;&#xD;
              &lt;/td&gt;&#xD;
              &lt;td style="border-bottom: rgb(221,221,221) 1px solid; padding-bottom: 4px; padding-left: 4px; padding-right: 4px; color: rgb(108,108,108); font-size: small; border-right: rgb(221,221,221) 1px solid; padding-top: 4px" width="400"&gt;&#xD;
writes formatted text suffixed with a new-line character to a System.IO.TextWriter.&lt;/td&gt;&#xD;
            &lt;/tr&gt;&#xD;
          &lt;/tbody&gt;&#xD;
        &lt;/table&gt;&#xD;
        &lt;p&gt;&#xD;
The functions above are especially powerful because the F# compiler will &lt;a href="http://diditwith.net/2008/01/16/WhyILoveFTypesafeFormatStrings.aspx"&gt;type-check&#xD;
the format arguments&lt;/a&gt; and give design-time errors.&#xD;
&lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
          &lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" alt="Type-safe Format String Error with Tooltip" src="http://diditwith.net/content_images/typesafeformatstring2.png"&gt;&lt;/img&gt;&#xD;
        &lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
While this set of functions covers most of the cases where formatted text is needed,&#xD;
there is one glaring omission: debug output. If we want to pass formatted text to &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;System.Diagnostics.Debug.Write&lt;/span&gt;,&#xD;
we have to do it using &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;sprintf&lt;/span&gt; like&#xD;
so:&#xD;
&lt;/p&gt;&#xD;
        &lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;System.Diagnostics.Debug.Write(sprintf &lt;span style="color: maroon"&gt;"The&#xD;
answer = %d"&lt;/span&gt; 42) &#xD;
&lt;/div&gt;&#xD;
        &lt;p&gt;&#xD;
Obviously, that's not ideal. What we would &lt;em&gt;really&lt;/em&gt; like is a function that&#xD;
behaves exactly like the other &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;printf&lt;/span&gt; functions&#xD;
but writes debug output. Fortunately, extensibility has been built into the F# library,&#xD;
making it possible to create additional formatting functions that benefit from the&#xD;
same sweet type-checking. So, how do we go about doing that? The trick is to wrap&#xD;
our functions around a special F# library function, &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;ksprintf&lt;/span&gt;.&#xD;
&lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
          &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;ksprintf&lt;/span&gt; is&#xD;
similar to &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;sprintf&lt;/span&gt; in&#xD;
that it formats text as a string. However, instead of returning that string to the&#xD;
caller, it passes the string into a &lt;em&gt;continuation&lt;/em&gt;.&#xD;
&lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
I haven't covered the broad topic of &lt;a href="http://en.wikipedia.org/wiki/Continuations"&gt;continuations&lt;/a&gt; yet&#xD;
because a) they can be pretty eye-crossing when presented plainly, and b) there are&#xD;
already &lt;a href="http://blogs.msdn.com/wesdyer/archive/2007/12/22/continuation-passing-style.aspx"&gt;fantastic&#xD;
treatments&lt;/a&gt; out there. The basic idea is to pass a lambda&lt;sup&gt;1&lt;/sup&gt; (the so-called&#xD;
continuation) into a computation that will be executed when the computation is finished.&#xD;
Really. That's it. This is a simple idea, but it can become complicated quickly. However,&#xD;
in the case of &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;ksprintf&lt;/span&gt; it&#xD;
remains simple. We'll pass a lambda that calls &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;Debug.Write&lt;/span&gt; with&#xD;
the final string after it has been formatted.&#xD;
&lt;/p&gt;&#xD;
        &lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;&#xD;
          &lt;span style="color: blue"&gt;module&lt;/span&gt; Debug&#xD;
=&lt;br&gt;&#xD;
  &lt;span style="color: blue"&gt;open&lt;/span&gt; System.Diagnostics&lt;br&gt;&lt;br&gt;&#xD;
  &lt;span style="color: blue"&gt;let&lt;/span&gt; writef fmt = Printf.ksprintf (&lt;span style="color: blue"&gt;fun&lt;/span&gt; s &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; Debug.Write(s))&#xD;
fmt&lt;br&gt;&lt;br&gt;&#xD;
  &lt;span style="color: blue"&gt;let&lt;/span&gt; writefn fmt = Printf.ksprintf (&lt;span style="color: blue"&gt;fun&lt;/span&gt; s &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; Debug.WriteLine(s))&#xD;
fmt&#xD;
&lt;/div&gt;&#xD;
        &lt;p&gt;&#xD;
In fact, the wrapper lambdas are redundant because &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;Debug.Write&lt;/span&gt; and &lt;span style="font-family: consolas,'Courier New',courier,monospace"&gt;Debug.WriteLine&lt;/span&gt; have&#xD;
overloads that match the expected signature. We can simply pass the functions themselves&#xD;
and remove the wrappers:&#xD;
&lt;/p&gt;&#xD;
        &lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;&#xD;
          &lt;span style="color: blue"&gt;module&lt;/span&gt; Debug&#xD;
=&lt;br&gt;&#xD;
  &lt;span style="color: blue"&gt;open&lt;/span&gt; System.Diagnostics&lt;br&gt;&lt;br&gt;&#xD;
  &lt;span style="color: blue"&gt;let&lt;/span&gt; writef fmt = Printf.ksprintf Debug.Write&#xD;
fmt&lt;br&gt;&lt;br&gt;&#xD;
  &lt;span style="color: blue"&gt;let&lt;/span&gt; writefn fmt = Printf.ksprintf Debug.WriteLine&#xD;
fmt&#xD;
&lt;/div&gt;&#xD;
        &lt;p&gt;&#xD;
Just drop that code into an F# project, and you'll be writing debug output in a beautiful,&#xD;
concise F# style.&#xD;
&lt;/p&gt;&#xD;
        &lt;div style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; padding-bottom: 4px; background-color: rgb(255,255,255); margin: 4px; padding-left: 4px; padding-right: 4px; font-family: consolas,'Courier New',courier,monospace; color: rgb(0,0,0); font-size: small; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted; padding-top: 4px"&gt;Debug.writef &lt;span style="color: maroon"&gt;"The&#xD;
answer = %d"&lt;/span&gt; 42&#xD;
&lt;/div&gt;&#xD;
        &lt;p&gt;&#xD;
You'll even get helpful design-time type errors:&#xD;
&lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
          &lt;img style="border-bottom: rgb(221,221,221) 1px dotted; border-left: rgb(221,221,221) 1px dotted; border-top: rgb(221,221,221) 1px dotted; border-right: rgb(221,221,221) 1px dotted" alt="Debug.writef Type-safe Format String Error with Tooltip" src="http://diditwith.net/content_images/DebugWriteFTypeSafe.png"&gt;&lt;/img&gt;&#xD;
        &lt;/p&gt;&#xD;
        &lt;p&gt;&#xD;
Now it's just a matter of convincing the &lt;a href="http://msdn.microsoft.com/fsharp"&gt;F#&#xD;
team&lt;/a&gt; to add it to the libraries. ;-)&#xD;
&lt;/p&gt;&#xD;
        &lt;p style="border-top: rgb(221,221,221) 1px solid"&gt;&#xD;
          &lt;sup&gt;1&lt;/sup&gt;Did It With .NET readers probably already know that "lambda" = "anonymous&#xD;
function".&#xD;
&lt;/p&gt;&#xD;
        &lt;img width="0" height="0" src="http://diditwith.net/aggbug.ashx?id=befb5f67-34d7-4ab2-a510-eb94cb4f6666"&gt;&lt;/img&gt;&#xD;
      &lt;/body&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=OLbOpdgX"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?d=41" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=aKVRpHAi"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?d=43" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=8lmIdKqU"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?i=8lmIdKqU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=QjwqPY7u"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?i=QjwqPY7u" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=6nvyOfCc"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?d=131" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=ZL2F1OJh"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?d=1617" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/DidItWithDotNet?a=cP9HxTR5"&gt;&lt;img src="http://feeds.feedburner.com/~f/DidItWithDotNet?d=300" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/DidItWithDotNet/~4/t0Xr7M2_ztg" height="1" width="1"/&gt;</description>
      <comments>http://diditwith.net/CommentView,guid,befb5f67-34d7-4ab2-a510-eb94cb4f6666.aspx</comments>
      <category>F#</category>
    <feedburner:origLink>http://diditwith.net/2008/12/29/PrintfAndFormattingDebugOutputInF.aspx</feedburner:origLink></item>
  </channel>
</rss>
