<?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:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>valerio.net</title>
	
	<link>http://thevalerios.net/matt</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Wed, 18 Aug 2010 04:26:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/ValerioDotNet" /><feedburner:info uri="valeriodotnet" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Installing WP LaTeX</title>
		<link>http://feedproxy.google.com/~r/ValerioDotNet/~3/QN6h8gVhDS8/</link>
		<comments>http://thevalerios.net/matt/2010/08/installing-wp-latex/#comments</comments>
		<pubDate>Wed, 18 Aug 2010 03:36:27 +0000</pubDate>
		<dc:creator>matt</dc:creator>
				<category><![CDATA[Server]]></category>
		<category><![CDATA[equations]]></category>
		<category><![CDATA[latex]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://thevalerios.net/matt/?p=351</guid>
		<description><![CDATA[When I first started blogging I was really interested in having LaTeX equation support. After all, software development hasn’t always been my vocation – I did go to school for electrical engineering and have had my fair share of math classes 2 years ago getting LaTeX working within WordPress was incredibly difficult. It required installing [...]]]></description>
			<content:encoded><![CDATA[<p>When I first started blogging I was really interested in having LaTeX equation support. After all, software development hasn’t always been my vocation – I did go to school for electrical engineering and have had my fair share of math classes <img src='http://thevalerios.net/matt/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>2 years ago getting <a href="http://www.latex-project.org/">LaTeX</a> working within <a href="http://wordpress.org/">WordPress</a> was incredibly difficult. It required installing <a href="http://www.imagemagick.org/script/index.php">ImageMagick</a>, dvips, and a myriad of other tools on your hosting server. I asked Hostgator support if this was possible, only to be told an emphatic ‘no’ – the Linux VMs used to host my website were significantly sandboxed and this was not possible, so I gave up.</p>
<p>Fast forward to today – not only has WordPress made significant advances in update functionality – it is now trivial to add new plugins. What used to take hours on an SSH commandline now is possible with only a few clicks.</p>
<p>Here are the steps to follow:</p>
<ol>
<li>Log into the admin console and click on the Plugins tab.</li>
<li>Click on Add New<br />
<a href="http://thevalerios.net/matt/wp-content/uploads/2010/08/image.png"><img style="display: inline; border: 0px;" title="image" src="http://thevalerios.net/matt/wp-content/uploads/2010/08/image_thumb.png" border="0" alt="image" width="244" height="82" /></a></li>
<li>Search for ‘latex’</li>
<li>Find “WP Latex” in the list and click on ‘Install Now’:<br />
<a href="http://thevalerios.net/matt/wp-content/uploads/2010/08/image1.png"><img style="display: inline; border: 0px;" title="image" src="http://thevalerios.net/matt/wp-content/uploads/2010/08/image_thumb1.png" border="0" alt="image" width="193" height="52" /></a></li>
<li>Go to the Plugins tab, find ‘WP LaTeX’, and click ‘Active’</li>
</ol>
<p>Now it’s just a matter of composing a blog post with equations embedded between <a href="http://thevalerios.net/matt/wp-content/uploads/2010/08/image2.png"><img style="display: inline; border: 0px;" title="image" src="http://thevalerios.net/matt/wp-content/uploads/2010/08/image_thumb2.png" border="0" alt="image" width="83" height="14" /></a> tags.</p>
<p>I like using <a href="http://explore.live.com/windows-live-writer">Windows Live Writer</a> for composing blog post, and this works perfectly. All that I need to do is create a new paragraph, enter the equation text in LaTeX, and then center align it (screenshot of WLW):</p>
<p><a href="http://thevalerios.net/matt/wp-content/uploads/2010/08/image3.png"><img style="display: inline; border: 0px;" title="image" src="http://thevalerios.net/matt/wp-content/uploads/2010/08/image_thumb3.png" border="0" alt="image" width="610" height="161" /></a></p>
<p>Here’s the rendered equation:</p>
<p style="text-align: center;"><img src='http://s.wordpress.com/latex.php?latex=e%5E%7Bi%5Cpi%7D%2B1%3D0&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='e^{i\pi}+1=0' title='e^{i\pi}+1=0' class='latex' /></p>
<p>Very cool. <img src='http://thevalerios.net/matt/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/ValerioDotNet/~4/QN6h8gVhDS8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://thevalerios.net/matt/2010/08/installing-wp-latex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://thevalerios.net/matt/2010/08/installing-wp-latex/</feedburner:origLink></item>
		<item>
		<title>On “Preinstalled Software” and “One Button Restore”</title>
		<link>http://feedproxy.google.com/~r/ValerioDotNet/~3/GYkr4LNEvBo/</link>
		<comments>http://thevalerios.net/matt/2010/08/on-preinstalled-software-and-one-button-restore/#comments</comments>
		<pubDate>Sat, 14 Aug 2010 23:33:49 +0000</pubDate>
		<dc:creator>matt</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[diskpart]]></category>
		<category><![CDATA[laptop]]></category>
		<category><![CDATA[lenovo]]></category>
		<category><![CDATA[partition]]></category>
		<category><![CDATA[rant]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[windows 7]]></category>

		<guid isPermaLink="false">http://thevalerios.net/matt/2010/08/on-preinstalled-software-and-one-button-restore/</guid>
		<description><![CDATA[Most laptop computers these days come with a number of “features” that drive me completely insane. Bear with me – I’ll try to stay calm In particular – “preinstalled software” and “one button restore” are particularly egregious. My new Lenovo G555 is no exception. The amount of preinstalled software was staggering. No, I don’t want [...]]]></description>
			<content:encoded><![CDATA[<p>Most laptop computers these days come with a number of “features” that drive me completely insane. Bear with me – I’ll try to stay calm <img src='http://thevalerios.net/matt/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>In particular – “preinstalled software” and “one button restore” are particularly egregious. My new Lenovo G555 is no exception.</p>
<p>The amount of preinstalled software was staggering. No, I don’t want to have facial recognition software protecting my computer from unauthorized logins. No, I don’t want identity theft protection management utilities from some software company I’ve never heard of. No, I don’t want silly wireless network control software that looks like it was written for a target audience of 6 year olds. No, I don’t want McAfee virus protection that only works for 30 days before it mercilessly hounds me with unending popups to pay for a subscription.</p>
<p>If the preinstalled pieces of software are not totally useless, they’re predatory. What’s the value in this from the consumer’s point of view? As a purchaser of a computer, I feel intense frustration with Lenovo for lessening the value of my purchase by installing a lot of things I don’t want, forcing me to spend my time cleaning things up to make my computer tolerable again. As for the predatory software, I feel “sold out” by Lenovo – they were probably able to “subsidize” the cost of my laptop hardware by letting 3rd-parties pay to install their programs on my machine. This is ridiculous!</p>
<p>As if this wasn’t bad enough, there is literally no way to remove all of the crapware to return to the original unmarred operating system. Sure, I could try to remove everything in the “Add/Remove Programs” dialog section of the Control Panel, but how could I be sure that the uninstallers <em>actually</em> removed everything? What if there are files and registry keys left – I don’t trust any of these software vendors (many of whom I’ve never even heard of) to do what I requested and not just leave backdoors behind.</p>
<p>After being foiled in that attempt, I tried to reinstall Windows 7. Oh wait, I can’t. I did not receive a Windows 7 DVD. My only option is to use the “one key recovery” button. What does this do? Oh, it just reverts the computer back to its original state, invasive crapware and all. <em>No thanks</em>.</p>
<p>My solution? Without any other options, the only workable solution is to go out and buy a brand new copy of Windows 7. This <em>really</em> irks me, seeing that <em>I just paid for a perfectly good copy of Windows 7, but I can’t use it because Lenovo’s crappy “solutions” are getting in the way.</em> Honestly? This is criminal, especially since there is no way to get a refund for the preinstalled copy of Windows 7.</p>
<p>So, after purchasing a new copy of Windows 7 (which wasn’t a total waste since I really want Ultimate instead of Home Premium), is it easy to install? No, of course not. The hard drive has special partitions to hold the one-key backup (in lieu of shipping the installation media). Really? Nothing on the website ever said “500GB* hard drive, * where you can’t use 25GB+ of the space because we’re too lazy to ship you installation media for the software you just purchased”. When did I give Lenovo permission to take some of the hard drive space that I just paid for to use for their misguided attempts in software distribution?</p>
<p>After blowing away these partitions (Shift+F10 during installation and diskpart overrides to the rescue) I was able to pave the machine and successfully install Windows 7 Ultimate.</p>
<p>What happened to the days where customers could buy hardware without all of this extra crap? Extra things (e.g. O/S, software, etc) should cost extra, and consumers should have a choice to purchase as little or as much as they need.</p>
<p>Sadly, this approach is not limited to Lenovo – I’ve had a similar experience with Dell and have heard horror stories from other consumers regarding other laptop vendors.</p>
<p>Has anyone else had a similarly frustrating experience?</p>
<img src="http://feeds.feedburner.com/~r/ValerioDotNet/~4/GYkr4LNEvBo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://thevalerios.net/matt/2010/08/on-preinstalled-software-and-one-button-restore/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://thevalerios.net/matt/2010/08/on-preinstalled-software-and-one-button-restore/</feedburner:origLink></item>
		<item>
		<title>The Blog is Back</title>
		<link>http://feedproxy.google.com/~r/ValerioDotNet/~3/irwrQkW1XEQ/</link>
		<comments>http://thevalerios.net/matt/2010/08/the-blog-is-back/#comments</comments>
		<pubDate>Sat, 14 Aug 2010 21:39:21 +0000</pubDate>
		<dc:creator>matt</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[blogging]]></category>
		<category><![CDATA[meta]]></category>

		<guid isPermaLink="false">http://thevalerios.net/matt/2010/08/the-blog-is-back/</guid>
		<description><![CDATA[Well, it’s been awhile, but (hopefully), the blog is back. Why has the blog been dark for over a year? I thought about it for awhile, trying to get to the real reason. The “no time” excuse got dismissed pretty quickly, since I had various small pockets of time that I could have used.&#160; Similarly, [...]]]></description>
			<content:encoded><![CDATA[<p>Well, it’s been awhile, but (hopefully), the blog is back. Why has the blog been dark for over a year? </p>
<p>I thought about it for awhile, trying to get to the <em>real</em> reason. The “no time” excuse got dismissed pretty quickly, since I had various small pockets of time that I could have used.&#160; Similarly, the “nothing to say” argument fades away quickly since I’ve continued learning and exploring the new advanced in technology that have happened over the last year.</p>
<p>What’s the real reason? It might sound lame, but…the barrier to writing a post wasn’t low anymore. Why? The general pace of technology advancement simply outstripped my current computing capacity.&#160; More specifically, new advances with Windows 7 and Visual Studio 2010 raised the hardware requirement bar so high that my 3-year-old Windows XP dual-core laptop couldn’t keep up. </p>
<p>Trust me, I tried. I waited 16+ hours for the installation(s) to finish, but the end result was a way-too-sluggish machine. 2GB of RAM just doesn’t cut it, and virtual memory off of a 5400rpm hard drive is doesn’t help much.</p>
<p>I guess this is to be expected – advances in software typically push the limits of available hardware (or is it the other way around?). This has always been evident in the high-end graphics card market.&#160; I just found that the hardware requirement bar got pushed significantly faster these last few years due to Vista/Win7 and Visual Studio 2010.</p>
<p>So, the blog is back basically because I just got a new laptop <img src='http://thevalerios.net/matt/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/ValerioDotNet/~4/irwrQkW1XEQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://thevalerios.net/matt/2010/08/the-blog-is-back/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://thevalerios.net/matt/2010/08/the-blog-is-back/</feedburner:origLink></item>
		<item>
		<title>Great IoC and DI Articles</title>
		<link>http://feedproxy.google.com/~r/ValerioDotNet/~3/Jllm5ZIDfqk/</link>
		<comments>http://thevalerios.net/matt/2009/12/great-ioc-and-di-articles/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 04:55:34 +0000</pubDate>
		<dc:creator>matt</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[castle]]></category>
		<category><![CDATA[DI]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[IoC]]></category>
		<category><![CDATA[windsor]]></category>

		<guid isPermaLink="false">http://thevalerios.net/matt/2009/12/great-ioc-and-di-articles/</guid>
		<description><![CDATA[I’ve been interested in learning more about Inversion of Control (IoC) and Dependency Injection (DI) containers for awhile now, so I decided to take a look. My interest was piqued by an article by Mark Seemann from his upcoming book “DI in .NET”. This was a good introduction, but not very in-depth &#8211; I’m certainly [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve been interested in learning more about Inversion of Control (IoC) and Dependency Injection (DI) containers for awhile now, so I decided to take a look.</p>
<p>My interest was piqued by an <a href="http://dotnetslackers.com/articles/designpatterns/DI-Patterns-Constructor-Injection.aspx">article</a> by <a href="http://twitter.com/ploeh">Mark</a> <a href="http://blog.ploeh.dk/">Seemann</a> from his <a href="http://www.manning.com/seemann/">upcoming book “DI in .NET”</a>. This was a good introduction, but not very in-depth &#8211; I’m certainly looking forward to his book now. <img src='http://thevalerios.net/matt/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>After digging in more, I discovered a few more good articles (also on .NET Slackers) that explain IoC and DI using the Castle Project &#8211; “Inversion of Control and Dependency Injection with Castle Windsor Container” – check them out:</p>
<ul>
<li><a href="http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart1.aspx">Part I</a></li>
<li><a href="http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart2.aspx">Part II</a></li>
<li><a href="http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart3.aspx">Part III</a></li>
<li><a href="http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart4.aspx">Part IV</a></li>
</ul>
<p>Very cool stuff.</p>
<img src="http://feeds.feedburner.com/~r/ValerioDotNet/~4/Jllm5ZIDfqk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://thevalerios.net/matt/2009/12/great-ioc-and-di-articles/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://thevalerios.net/matt/2009/12/great-ioc-and-di-articles/</feedburner:origLink></item>
		<item>
		<title>F# Jobs on the Rise</title>
		<link>http://feedproxy.google.com/~r/ValerioDotNet/~3/SwWyrQyfNXs/</link>
		<comments>http://thevalerios.net/matt/2009/06/f-jobs-on-the-rise/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 15:12:10 +0000</pubDate>
		<dc:creator>matt</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[jobs]]></category>

		<guid isPermaLink="false">http://thevalerios.net/matt/2009/06/f-jobs-on-the-rise/</guid>
		<description><![CDATA[I’ve received 3 inquiries in the last 2 weeks as to my “job availability status”, all surrounding my experience with F# on my resume. I’m no longer looking for a job (thankfully) and thought I had removed all references to my resume from my website, Dice, Monster, etc.  I suppose there are still a few [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve received 3 inquiries in the last 2 weeks as to my “job availability status”, all surrounding my experience with F# on my resume. I’m no longer looking for a job (thankfully) and thought I had removed all references to my resume from my website, Dice, Monster, etc.  I suppose there are still a few floating around somewhere.</p>
<p>I don’t know a lot of details about the positions these recruiters are trying to fill, but I did find out that one is in Columbus, OH and one is in Redmond, WA. In an effort to help out the F# community I figured I’d post about these jobs.  There’s nothing in it for me.</p>
<p>If you are interested, shoot me an email and I’ll send you the contact information for the folks who called/emailed/talked to me.</p>
<p>Here’s my info:</p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">let</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> email = <br />
</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"><span style="mso-spacerun: yes">    </span>[(5,<span style="color: maroon">"gmail"</span>); (2,<span style="color: maroon">"."</span>); (3,<span style="color: maroon">"valerio"</span>); (6,<span style="color: maroon">"."</span>); (1,<span style="color: maroon">"matt"</span>);<br />
     (7,<span style="color: maroon">"com"</span>); (4,<span style="color: maroon">"@"</span>)]<br />
</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"><span style="mso-spacerun: yes">    </span>|&gt; List.sortBy fst<br />
</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"><span style="mso-spacerun: yes">    </span>|&gt; List.map snd<br />
</span><span style="line-height: 115%; font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"><span style="mso-spacerun: yes">    </span>|&gt; List.reduce (^)</span></p>
<p> <img src='http://thevalerios.net/matt/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/ValerioDotNet/~4/SwWyrQyfNXs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://thevalerios.net/matt/2009/06/f-jobs-on-the-rise/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://thevalerios.net/matt/2009/06/f-jobs-on-the-rise/</feedburner:origLink></item>
		<item>
		<title>Hunting the Elusive ‘tail’ Opcode in F#</title>
		<link>http://feedproxy.google.com/~r/ValerioDotNet/~3/gu8SMTnSTwQ/</link>
		<comments>http://thevalerios.net/matt/2009/06/hunting-the-elusive-tail-opcode-in-f/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 05:00:53 +0000</pubDate>
		<dc:creator>matt</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[tail recursion]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://thevalerios.net/matt/2009/06/hunting-the-elusive-tail-opcode-in-f/</guid>
		<description><![CDATA[Awhile back I wrote a post about tail-call optimizations that the F# compiler used to eliminate stack overflows. Brian McNamera commented about another optimization that I didn’t illustrate – the ‘tail’ opcode that appears when mutually-recursive and indirectly-recursive functions are encountered. Tail-call optimization is one of the really powerful features of F#, so I really [...]]]></description>
			<content:encoded><![CDATA[<p>Awhile back I <a href="http://thevalerios.net/matt/2009/01/recursion-in-f-and-the-tail-recursion-police/">wrote a post</a> about tail-call optimizations that the F# compiler used to eliminate stack overflows. Brian McNamera commented about another optimization that I didn’t illustrate – the ‘tail’ opcode that appears when mutually-recursive and indirectly-recursive functions are encountered. Tail-call optimization is one of the really powerful features of F#, so I really wanted to see how this worked under the hood.</p>
<p>The first thing we need is a pair of mutually-recursive functions.  The easiest (laziest? <img src='http://thevalerios.net/matt/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ) way to do this is to write one function (e.g. f1) and duplicate its implementation as another name (e.g. f2):</p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; color: green; font-size: 9.5pt;">// Hunting for tail calls</span></p>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"> </span></div>
<p><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; color: green; font-size: 9.5pt;">// 6.17.09</span></p>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"> </span></div>
<p><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; color: blue; font-size: 9.5pt;">open</span></p>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"> System </span></div>
<p><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; color: blue; font-size: 9.5pt;">let</span></p>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"> <span style="color: blue;">rec</span> sum1 n acc = </span></div>
<p><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes">    </span><span style="color: blue;">match</span> n <span style="color: blue;">with</span> </span></div>
<p><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes">    </span>| 0 <span style="color: blue;">-&gt;</span> acc </span></div>
<p><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes">    </span>| _ <span style="color: blue;">-&gt;</span> sum2 (n-1) (acc+n) </span></div>
<p><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; color: blue; font-size: 9.5pt;">and</span></p>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"> sum2 n acc = </span></div>
<p><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes">    </span><span style="color: blue;">match</span> n <span style="color: blue;">with</span> </span></div>
<p><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes">    </span>| 0 <span style="color: blue;">-&gt;</span> acc </span></div>
<p><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes">    </span>| _ <span style="color: blue;">-&gt;</span> sum1 (n-1) (acc+n) </span></div>
<p><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes">    </span></span></div>
<p><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; color: blue; font-size: 9.5pt;">let</span></p>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"> sum n = sum1 n 0 </span></div>
<p><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; color: blue; font-size: 9.5pt;">let</span></p>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"> main () = </span></div>
<p><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes">   </span>Console.WriteLine(<span style="color: maroon;">&#8220;Hello&#8221;</span>) </span></div>
<p><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes">   </span>printfn <span style="color: maroon;">&#8220;%A&#8221;</span> (sum 100000) </span></div>
<p><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes">   </span>Console.WriteLine(<span style="color: maroon;">&#8220;Press Enter to continue&#8230;&#8221;</span>) </span></div>
<p><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes">   </span>Console.ReadLine() |&gt; ignore </span></div>
<p><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes">    </span></span></div>
<p><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;">main () </span></div>
<p><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none">After compiling this, I popped open the resulting executable in Reflector. Of course I wasn’t going to find the ‘tail’ opcode by looking at the C# – I needed to disassemble the IL. I hunted and hunted for the ‘tail’ opcode, but couldn’t find it!  Every call to f1 from f2 (and f2 from f1) used the stack to pass around n and acc. Even stranger – this is the first F# program I’d written using Visual Studio 2010, and I could have sworn that I’d done the same thing with F# in Visual Studio 2008 a couple months ago.</p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none">After building the project again, I noticed the command-line arguments passed to fsc:</p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;">&#8212;&#8212; Build started: Project: HuntingTailcalls, Configuration: Debug Any CPU &#8212;&#8212; </span></div>
<p><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><span style="font-family: consolas; font-size: 9.5pt;"><span style="mso-tab-count: 2">              </span>C:\Program Files\Microsoft F#\v4.0\fsc.exe -o:obj\Debug\HuntingTailcalls.exe -g &#8211;debug:full &#8211;noframework &#8211;define:DEBUG &#8211;define:TRACE &#8211;optimize- <span style="background: yellow; mso-highlight: yellow">&#8211;tailcalls-</span> -r:&#8221;C:\Program Files\Microsoft F#\v4.0\FSharp.Core.dll&#8221; -r:&#8221;C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\mscorlib.dll&#8221; -r:&#8221;C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Core.dll&#8221; -r:&#8221;C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.dll&#8221; &#8211;target:exe &#8211;warn:3 &#8211;warnaserror:76 &#8211;vserrors &#8211;utf8output &#8211;fullpaths &#8211;flaterrors Program.fs </span></div>
<p><span style="font-family: consolas; font-size: 9.5pt;"> </p>
<p></span></p>
<p class="MsoNormal">“—tailcalls-“? Somehow tailcalls are being turned off. Maybe there’s something in the project settings that are disabling the tailcalls? Ah-ha! The checkbox was unchecked <img src='http://thevalerios.net/matt/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p class="MsoNormal"><a href="http://thevalerios.net/matt/wp-content/uploads/2009/06/image.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" src="http://thevalerios.net/matt/wp-content/uploads/2009/06/image-thumb.png" border="0" alt="image" width="644" height="369" /></a></p>
<p class="MsoNormal">After poking around a bit more, I discovered that by default the “Generate tail calls” box is unchecked fo Debug mode, and checked by default for Release mode.  Hmmm, interesting.</p>
<p class="MsoNormal">After switching to Release mode, I rebuild the project and opened the .exe in Reflector.  Here we go! There’s the elusive ‘tail’ opcode:</p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span style="font-family: &quot;Courier New&quot;; color: #1000a0; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;">.method</span></p>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"> <span style="color: #1000a0;">public</span> <span style="color: #1000a0;">static</span> <a title="int32" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:4.0.0.0:b77a5c561934e089/System.Int32"><span style="color: blue;">int32</span></a> <strong><a href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://HuntingTailcalls/Program/sum1(Int32,Int32):Int32"><span style="color: blue;">sum1</span></a></strong>(<a title="int32" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:4.0.0.0:b77a5c561934e089/System.Int32"><span style="color: blue;">int32</span></a> n, <a title="int32" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:4.0.0.0:b77a5c561934e089/System.Int32"><span style="color: blue;">int32</span></a> acc)<span style="color: #1000a0;"> cil managed</span> </span></div>
<p><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;">{ </span></div>
<p><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"><span style="mso-spacerun: yes">    </span><span style="color: #1000a0;">.maxstack</span> 5 </span></div>
<p><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"><span style="mso-spacerun: yes">    </span>L_0000: ldarg.0 </span></div>
<p><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"><span style="mso-spacerun: yes">    </span>L_0001: switch (L_0019) </span></div>
<p><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"><span style="mso-spacerun: yes">    </span>L_000a: nop </span></div>
<p><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"><span style="mso-spacerun: yes">    </span>L_000b: ldarg.0 </span></div>
<p><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"><span style="mso-spacerun: yes">    </span>L_000c: ldc.i4.1 </span></div>
<p><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"><span style="mso-spacerun: yes">    </span>L_000d: sub </span></div>
<p><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"><span style="mso-spacerun: yes">    </span>L_000e: ldarg.1 </span></div>
<p><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"><span style="mso-spacerun: yes">    </span>L_000f: ldarg.0 </span></div>
<p><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"><span style="mso-spacerun: yes">    </span>L_0010: add </span></div>
<p><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"><span style="mso-spacerun: yes">    </span>L_0011: <span style="background: yellow; mso-highlight: yellow">tail</span> </span></div>
<p><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"><span style="mso-spacerun: yes">    </span>L_0013: call <a title="int32" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:4.0.0.0:b77a5c561934e089/System.Int32"><span style="color: blue;">int32</span></a> <a title="Program" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://HuntingTailcalls/Program"><span style="color: blue;">Program</span></a>::<a title="sum2" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://HuntingTailcalls/Program/sum2(Int32,Int32):Int32"><span style="color: blue;">sum2</span></a>(<a title="int32" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:4.0.0.0:b77a5c561934e089/System.Int32"><span style="color: blue;">int32</span></a>, <a title="int32" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:4.0.0.0:b77a5c561934e089/System.Int32"><span style="color: blue;">int32</span></a>) </span></div>
<p><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"><span style="mso-spacerun: yes">    </span>L_0018: ret </span></div>
<p><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"><span style="mso-spacerun: yes">    </span>L_0019: ldarg.1 </span></div>
<p><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"><span style="mso-spacerun: yes">    </span>L_001a: ret </span></div>
<p><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;;"> </p>
<p></span><span style="line-height: 115%; font-family: &quot;Courier New&quot;; font-size: 10pt; mso-fareast-font-family: &quot;Times New Roman&quot;; mso-ansi-language: en-us; mso-fareast-language: en-us; mso-bidi-language: ar-sa;">}</span></p>
<p class="MsoNormal">(The IL code for sum2 looks identical.) Interestingly enough, the C# code from Reflector looks exactly the same between Debug and Release modes (with and without tail calls) – C# doesn’t have the capability to make tail calls.</p>
<p class="MsoNormal">Well, there you have it! We finally found the elusive ‘tail’ opcode.</p>
<p class="MsoNormal">That being said, be sure to keep this in mind – the default settings of Visual Studio 2010 for F# development are drastically different between Debug and Release mode.  Bugs might crop up in Debug mode (e.g. StackOverflowExceptions) that don’t rear their heads in Release mode.</p>
<p class="MsoNormal">I think the motivation for this is that using tail calls severely limit the usefulness of the Visual Studio debugger since it relies on traversing the stack frame (that the tail opcode destroys) to display debugging information.</p>
<p class="MsoNormal">For example, without tailcall optimizations setting a breakpoint on sum1 looks like this:</p>
<p class="MsoNormal"><a href="http://thevalerios.net/matt/wp-content/uploads/2009/06/image1.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" src="http://thevalerios.net/matt/wp-content/uploads/2009/06/image-thumb1.png" border="0" alt="image" width="244" height="145" /></a></p>
<p class="MsoNormal"><a href="http://thevalerios.net/matt/wp-content/uploads/2009/06/image2.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" src="http://thevalerios.net/matt/wp-content/uploads/2009/06/image-thumb2.png" border="0" alt="image" width="628" height="297" /></a></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none">The callstack shows some useful debugging information, specifically the values of n and the accumulator.</p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none">However, if we enable tailcall optimization, this breaks down after running through the breakpoint 10 times, each time it shows one line with different information:</p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><a href="http://thevalerios.net/matt/wp-content/uploads/2009/06/image3.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" src="http://thevalerios.net/matt/wp-content/uploads/2009/06/image-thumb3.png" border="0" alt="image" width="496" height="107" /></a></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none"><a href="http://thevalerios.net/matt/wp-content/uploads/2009/06/image4.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" src="http://thevalerios.net/matt/wp-content/uploads/2009/06/image-thumb4.png" border="0" alt="image" width="496" height="107" /></a></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none">… You get the idea.</p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none">Hope that sheds some light on tailcall optimization, as well as some of the new features of F# in Visual Studio 2010!</p>
<img src="http://feeds.feedburner.com/~r/ValerioDotNet/~4/gu8SMTnSTwQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://thevalerios.net/matt/2009/06/hunting-the-elusive-tail-opcode-in-f/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://thevalerios.net/matt/2009/06/hunting-the-elusive-tail-opcode-in-f/</feedburner:origLink></item>
		<item>
		<title>Hosting Subversion In the Cloud with Live Mesh</title>
		<link>http://feedproxy.google.com/~r/ValerioDotNet/~3/603fdiE5B1k/</link>
		<comments>http://thevalerios.net/matt/2009/02/hosting-subversion-in-the-cloud-with-live-mesh/#comments</comments>
		<pubDate>Sun, 22 Feb 2009 05:43:55 +0000</pubDate>
		<dc:creator>matt</dc:creator>
				<category><![CDATA[Utilities]]></category>
		<category><![CDATA[cloud]]></category>
		<category><![CDATA[live]]></category>
		<category><![CDATA[mesh]]></category>
		<category><![CDATA[subversion]]></category>
		<category><![CDATA[tortoisesvn]]></category>

		<guid isPermaLink="false">http://thevalerios.net/matt/2009/02/hosting-subversion-in-the-cloud-with-live-mesh/</guid>
		<description><![CDATA[This afternoon I was going back through some of the code I&#8217;d written for various blog posts that I&#8217;d kept in a Subversion repository.&#160; During the move things have been in limbo and I haven&#8217;t had time to set up the SVN server again. I thought &#8220;Hmm, I wonder if I could host my Subversion [...]]]></description>
			<content:encoded><![CDATA[<p>This afternoon I was going back through some of the code I&#8217;d written for various blog posts that I&#8217;d kept in a <a href="http://subversion.tigris.org/">Subversion</a> repository.&nbsp; During the move things have been in limbo and I haven&#8217;t had time to set up the SVN server again. I thought &#8220;Hmm, I wonder if I could host my Subversion repository in the cloud&#8221;.</p>
<p>Enter <a href="https://www.mesh.com/">Live Mesh</a>. It lets you add multiple devices to your mesh network and automatically synchronize your files between devices.&nbsp; Pretty cool stuff.</p>
<p>At first I thought it would be a great place to put all of my source code &#8212; then I could have the code on every computer.&nbsp; However, what if you have working code on your desktop, then open up the code on your laptop and introduce a few bugs.&nbsp; When you go to open up the project on the desktop again, those bugs are there automatically.&nbsp; The synchronization is great, but there&#8217;s no way to keep version information in case you want to revert to a previous snapshot of the files.</p>
<p>Anyone familiar with Subversion will remember that there are two main parts to an installation &#8212; the Subversion repository (could be local or remote) and another folder with the checked-out files. A not-uncommon setup for personal development work is to have a local Subversion repository as a directory on the local file system.&nbsp; I wonder what would happen if I used slapped a local repository installation into a Live Mesh folder? Well, it would get automatically synchronized between machines. All devices in the network would have an SVN client installed (e.g. <a href="http://tortoisesvn.tigris.org/">TortoiseSVN</a>) and pointed to the Mesh-synchronized folder.&nbsp; I think this just might work <img src='http://thevalerios.net/matt/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>For anyone interested, here are the steps that I followed to set this up.</p>
<p>Open up your Live Mesh Folders from My Computer:<a href="http://thevalerios.net/matt/wp-content/uploads/2009/02/image.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="354" alt="image" src="http://thevalerios.net/matt/wp-content/uploads/2009/02/image-thumb.png" width="423" border="0"></a> </p>
<p>Set up a new folder named &#8220;Subversion&#8221;. Make sure that all of the devices in your Live Mesh network are set to &#8220;When files are added or modified&#8221; in the synchronization options.<a href="http://thevalerios.net/matt/wp-content/uploads/2009/02/image2.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="446" alt="image" src="http://thevalerios.net/matt/wp-content/uploads/2009/02/image-thumb2.png" width="487" border="0"></a>  </p>
<p>Browse over to the location (Subversion folder on my desktop), open it up, and create another folder inside called &#8220;Repository&#8221;.</p>
<p>Then, right-click on the Repository folder -&gt; TortoiseSVN -&gt; Create repository here. It&#8217;s important to note here that the extra &#8220;Repository&#8221; directory is important since you can&#8217;t directly create the repository in the &#8220;Subversion&#8221; folder one level up.&nbsp; There is some interaction between TortoiseSVN and Live Mesh that keeps it from working.</p>
<p>Then, go back to the desktop (or any other place) and make a folder called &#8220;Checkout&#8221;.&nbsp; Right-click on the Checkout folder and select &#8220;SVN Checkout&#8230;&#8221;</p>
<p><a href="http://thevalerios.net/matt/wp-content/uploads/2009/02/image1.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="350" alt="image" src="http://thevalerios.net/matt/wp-content/uploads/2009/02/image-thumb1.png" width="456" border="0"></a> </p>
<p>Make sure that the &#8220;URL of repository&#8221; field is pointed towards the &#8220;Subversion/Repository&#8221; directory and the &#8220;Checkout directory&#8221; field is pointed towards the &#8220;Checkout\Repository&#8221; directory and click OK.</p>
<p>There you go &#8212; use the checked-out subversion repository as you wish and just point your SVN client on each device to the Mesh-synchronized folder.</p>
<p>Hope someone finds this useful! <img src='http://thevalerios.net/matt/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>UPDATE:</strong> Looks like <a href="http://www.zimmergren.net/archive/2009/02/05/backing-up-your-local-subversion-repositories-using-live-mesh.aspx">I&#8217;m not the first to think of doing this</a> <img src='http://thevalerios.net/matt/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/ValerioDotNet/~4/603fdiE5B1k" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://thevalerios.net/matt/2009/02/hosting-subversion-in-the-cloud-with-live-mesh/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://thevalerios.net/matt/2009/02/hosting-subversion-in-the-cloud-with-live-mesh/</feedburner:origLink></item>
		<item>
		<title>Big Changes</title>
		<link>http://feedproxy.google.com/~r/ValerioDotNet/~3/wcF3tECzzG4/</link>
		<comments>http://thevalerios.net/matt/2009/02/big-changes/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 06:08:49 +0000</pubDate>
		<dc:creator>matt</dc:creator>
				<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://thevalerios.net/matt/2009/02/big-changes/</guid>
		<description><![CDATA[The blog&#8217;s been pretty quiet lately for good reason &#8212; I simply haven&#8217;t had a spare second to post anything. I have quite a few interesting posts in the wings just waiting for a last bit of polishing. Why so busy?&#160; Well, since the end of November I applied for, interviewed for, was offered, and [...]]]></description>
			<content:encoded><![CDATA[<p>The blog&#8217;s been pretty quiet lately for good reason &#8212; I simply haven&#8217;t had a spare second to post anything. I have quite a few interesting posts in the wings just waiting for a last bit of polishing.</p>
<p>Why so busy?&nbsp; Well, since the end of November I applied for, interviewed for, was offered, and accepted a job at Microsoft <img src='http://thevalerios.net/matt/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> &nbsp; As a consequence, I&#8217;ve been extremely busy getting ready for and making the cross-country move from Columbus, OH to Redmond, WA.&nbsp; Needless to say, it was a very exciting and nerve-wracking last few weeks.</p>
<p>I started as a SDE (software development engineer) in the <a href="http://www.microsoft.com/hsg/">Health Solutions Group</a> last Monday. I&#8217;m still learning the ropes and certainly relate to the feeling of &#8220;<a href="http://haacked.com/archive/2007/10/26/drinking-from-the-firehose.aspx">drinking from the fire hose</a>&#8220;. Today was a &#8220;momentous&#8221; occasion as I submitted my first bugfix, only to quickly discover the need to submit a bugfix for my bugfix. It&#8217;s quite humbling being completely surrounded by veritable developer rockstars.</p>
<p>Despite all of the stress, I am absolutely and totally pumped to be here &#8212; my project is awesome and the people I work with are awesome &#8212; there are very exciting times ahead <img src='http://thevalerios.net/matt/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Hopefully as things start to settle down I&#8217;ll get some time to braindump about all of the cool things going on. <img src='http://thevalerios.net/matt/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/ValerioDotNet/~4/wcF3tECzzG4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://thevalerios.net/matt/2009/02/big-changes/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://thevalerios.net/matt/2009/02/big-changes/</feedburner:origLink></item>
		<item>
		<title>Recursion in F# and the Tail Recursion Police</title>
		<link>http://feedproxy.google.com/~r/ValerioDotNet/~3/LsEfiDLMjgw/</link>
		<comments>http://thevalerios.net/matt/2009/01/recursion-in-f-and-the-tail-recursion-police/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 16:53:32 +0000</pubDate>
		<dc:creator>matt</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[Reflector]]></category>
		<category><![CDATA[tail recursion]]></category>

		<guid isPermaLink="false">http://thevalerios.net/matt/2009/01/recursion-in-f-and-the-tail-recursion-police/</guid>
		<description><![CDATA[A helpful comment on a post over at HubFS by Brian McNamara really helped me wrap my mind around tail recursion and why it&#8217;s immensely important to understand in F#. Brian coined the phrase &#8220;tail recursion police&#8221;, hence the title Recursion pops up quite a bit in functional programming so it&#8217;s a good idea to [...]]]></description>
			<content:encoded><![CDATA[<p>A helpful comment on a <a href="http://cs.hubfs.net/forums/thread/8022.aspx">post</a> over at <a href="http://cs.hubfs.net/forums/default.aspx">HubFS</a> by <a href="http://lorgonblog.spaces.live.com/blog/">Brian McNamara</a> really helped me wrap my mind around tail recursion and why it&#8217;s immensely important to understand in F#. Brian coined the phrase &#8220;tail recursion police&#8221;, hence the title <img src='http://thevalerios.net/matt/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Recursion pops up quite a bit in functional programming so it&#8217;s a good idea to understand the risks/tradeoffs/benefits. A gross simplification of an analogy would be:</p>
<blockquote><p>For loops : C# :: Recursion : F#</p>
</blockquote>
<p>Sure, you can do for-loops and imperative-style coding in F#, but it doesn&#8217;t showcase the beauty of the functional style programming that F# offers.</p>
<p>Recursive functions (in both C# and F#) are just functions that call themselves.&nbsp; They do so by pushing the current state of the function onto the stack, popping them off when calls start completing. Because the stack is a limited resource, it can be exhausted (StackOverflowException) if the function is called recursively too many times.&nbsp; </p>
<p>These kinds of runtime exceptions can be difficult to find unless explicitly tested for, leading to hard-to-find bugs.&nbsp; Oftentimes tests will only exercise simple cases of algorithms and then fail at runtime when large (real-world) inputs are used. After all, &#8220;everything works for small N&#8221;.</p>
<p>F#, however, has a very interesting feature where (under certain conditions), it can recognize recursive functions and perform a &#8220;tail call optimization&#8221;, turning the recursive function into a while-loop that uses <em>no stack space whatsoever</em>. This is called tail recursion.</p>
<p>The rule for tail-recursion is simple:</p>
<blockquote><p>If the recursive call to the function is the very last thing that happens in the algorithm, then the function can be made tail-recursive.</p>
</blockquote>
<p>In most cases a recursive algorithm can be expressed in both recursive and tail-recursive forms. Other times, it may not even be possible to implement a specific algorithm tail-recursively. </p>
<p>Let&#8217;s look at some examples.</p>
<h4>Tail Recursive or Not?</h4>
<p>One way to implement the &#8220;map&#8221; function that applies a specific function, f, to every element in a list would be:</p>
<div style="font-size: 10pt; background: white; color: black; font-family: consolas">
<p style="margin: 0px"><span style="color: blue">let</span> <span style="color: blue">rec</span> map f l =</p>
<p style="margin: 0px">&nbsp; <span style="color: blue">match</span> l <span style="color: blue">with</span></p>
<p style="margin: 0px">&nbsp; | [] <span style="color: blue">-&gt;</span> []</p>
<p style="margin: 0px">&nbsp; | h::t <span style="color: blue">-&gt;</span> f h :: map f t</p>
</div>
<p>While this implementation easily expresses the algorithm (splitting a list into its head and tail, applying the function f to the head, and consing it to the list obtained from applying the function to the tail), it is NOT tail recursive! The recursive call to &#8220;map&#8221; must happen, then f is applied to h, and then the results are cons&#8217;d (:: operator).&nbsp; The recursive call to &#8220;map&#8221; is not last in this instance, so it&#8217;s not tail-recursive.</p>
<p>How can we make this map operation tail-recursive? Typically this involves 2 things &#8212; factoring out an inner loop that is recursive and threading an accumulator through that inner loop:</p>
<div style="font-size: 10pt; background: white; color: black; font-family: consolas">
<p style="margin: 0px"><span style="color: blue">let</span> mapTR f l =</p>
<p style="margin: 0px">&nbsp; <span style="color: blue">let</span> <span style="color: blue">rec</span> loop f2 l2 acc =</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">match</span> l2 <span style="color: blue">with</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; | [] <span style="color: blue">-&gt;</span> acc</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; | h::t <span style="color: blue">-&gt;</span> loop f2 t ((f2 h)::acc)</p>
<p style="margin: 0px">&nbsp; loop f l [] |&gt; List.rev</p>
</div>
<p>In the 2nd-to-last line, f2 is applied to the head, then cons&#8217;d with the accumulator, and that result (along with f2 and the tail) are passed to the recursive call to &#8220;loop&#8221;.&nbsp; In this case, &#8220;loop&#8221; is the very last thing that happens, so the F# compiler can implement this tail-recursively.</p>
<h4>Double-Check with Reflector</h4>
<p>We can double-check that the F# compiler created a recursive function for the &#8220;map&#8221; case by using Reflector.&nbsp; While F#-compiled-to-IL-decompiled-to-C# can be a bit verbose thanks to all of the generics and partial function application (FastFunc), it can still be seen that the map function recursively calls itself.</p>
<p><a href="http://thevalerios.net/matt/wp-content/uploads/2009/01/image3.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="166" alt="image" src="http://thevalerios.net/matt/wp-content/uploads/2009/01/image-thumb3.png" width="339" border="0"></a> </p>
<p>If the Tail Recursion Police were out, we&#8217;d be totally busted.</p>
<p>For the &#8220;mapTR&#8221; case, the implementation is a bit hidden behind some closures, but can be found with a little work.&nbsp; The meat of the tail-recursive function is implemented with a while(true) loop! This is fantastic since it uses no stack space. The Tail Recursion Police would let us off clean on this one.</p>
<p><a href="http://thevalerios.net/matt/wp-content/uploads/2009/01/image4.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="226" alt="image" src="http://thevalerios.net/matt/wp-content/uploads/2009/01/image-thumb4.png" width="276" border="0"></a> </p>
<p>Sidenote: The List&lt;T&gt; type here is not the standard System.Collections.Generic.List&lt;T&gt;.&nbsp; It is a singly-linked list that is part of the F# core libraries. In F# the Generic.List&lt;T&gt; is known as ResizeArray&lt;T&gt; since it more resembles a resizable array than a singly-linked list.</p>
<p>Sidenote 2: I&#8217;ve heard some things about the <a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.tailcall(VS.95).aspx">&#8220;.tail&#8221; IL opcode</a>, but don&#8217;t see it popping up anywhere in this code. The while loop seems more efficient to me than a recursive call that drops the current stack frame, though. I&#8217;m not entirely sure what the relationship is between the .tail opcode is and tail recursion in F#.</p>
<p>Let&#8217;s walk through another example and dig into a little more detail.</p>
<h4>Kaboom! StackOverflowException Strikes</h4>
<p>When working on some code over the weekend, I ran into the need for a slightly different version of List.map. The &#8220;map&#8221; function has the signature &#8220;(&#8216;a -&gt; &#8216;b) -&gt; &#8216;a list -&gt; &#8216;b list&#8221; &#8212; that is, it takes a function that takes a &#8216;a and returns a &#8216;b as well as a &#8216;a list to operate on, producing a &#8216;b list. It simply applies the given function to each item in the list and returns a new list with the result of each function application.</p>
<p>What I needed, however, was a method that took a list of one type and returned a list of another type, but applied a function that accepted a list of values and produces a resulting value.&nbsp; That is, I needed a function with a signature &#8220;(&#8216;a list -&gt; &#8216;b) -&gt; &#8216;a list -&gt; &#8216;b list&#8221;. When you think about this, I needed an algorithm that would operate on the initial list by calling a method by &#8220;snapping off&#8221; successive portions of the list, producing another list.&nbsp; So, if we had a list [1;2;3;4], the mapping function would be called in succession with input lists [1;2;3;4], [2;3;4], [3;4], [4], and []. Since it&#8217;s still doing a mapping, though making the entire list (instead of just the head of the list) available to the mapping function, I decided to call it &#8220;mapl&#8221; for &#8220;map list&#8221;.</p>
<p>My first stab at an implementation was straightforward:</p>
<div style="font-size: 10pt; background: white; color: black; font-family: consolas">
<p style="margin: 0px"><span style="color: blue">module</span> List =</p>
<p style="margin: 0px">&nbsp; <span style="color: blue">let</span> <span style="color: blue">rec</span> mapl f l =</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">match</span> l <span style="color: blue">with</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; | [] <span style="color: blue">-&gt;</span> []</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; | h::t <span style="color: blue">-&gt;</span> f l :: mapl f t </p>
</div>
<div style="font-size: 10pt; background: white; color: black; font-family: consolas">
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&gt; <span style="color: blue">let</span> test = [1;2;3;4];;</p>
<p style="margin: 0px"><span style="color: blue">val</span> test : int list</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&gt; test |&gt; List.mapl (printfn <span style="color: maroon">&#8220;%A&#8221;</span>);;</p>
<p style="margin: 0px">[1; 2; 3; 4]</p>
<p style="margin: 0px">[2; 3; 4]</p>
<p style="margin: 0px">[3; 4]</p>
<p style="margin: 0px">[4]</p>
</div>
<p>Good so far. However, it&#8217;s not tail-recursive, so let&#8217;s see if we can break it:</p>
<div style="font-size: 10pt; background: white; color: black; font-family: consolas">
<p style="margin: 0px">&gt; [1..100000] |&gt; List.mapl List.hd;;</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">Process is terminated due <span style="color: blue">to</span> StackOverflowException.</p>
<p style="margin: 0px">Session termination detected. Press Enter <span style="color: blue">to</span> restart.</p>
</div>
<p><a href="http://thevalerios.net/matt/wp-content/uploads/2009/01/image5.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="262" alt="image" src="http://thevalerios.net/matt/wp-content/uploads/2009/01/image-thumb5.png" width="501" border="0"></a> </p>
<p>Kaboom! The Tail Recursion Police are already on the scene, cleaning up the remains of fsi.exe&#8230; </p>
<h4>Tail-Recursion Tweaks and Performance</h4>
<p>Ok, so let&#8217;s make this &#8220;mapl&#8221; function tail-recursive. I wanted to investigate 3 different approaches and compare the performance based on slight nuances between the implementations.</p>
<p>Here&#8217;s my code for mapl_tr1, mapl_tr2, and mapl_tr3:</p>
<div style="font-size: 10pt; background: white; color: black; font-family: consolas">
<p style="margin: 0px"><span style="color: blue">module</span> List =</p>
<p style="margin: 0px">&nbsp; <span style="color: blue">let</span> mapl_tr1 f l =</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">let</span> <span style="color: blue">rec</span> loop l2 acc =</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">match</span> l2 <span style="color: blue">with</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | [] <span style="color: blue">-&gt;</span> acc</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | h::t <span style="color: blue">-&gt;</span> loop t ((f l2)::acc)</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; loop l []</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp; <span style="color: blue">let</span> mapl_tr2 f l =</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">let</span> <span style="color: blue">rec</span> loop f2 l2 acc =</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">match</span> l2 <span style="color: blue">with</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | [] <span style="color: blue">-&gt;</span> acc</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | h::t <span style="color: blue">-&gt;</span> loop f2 t ((f2 l2)::acc)</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; loop f l []</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp; <span style="color: blue">let</span> mapl_tr3 f l =</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">let</span> <span style="color: blue">rec</span> loop (f2, l2, acc) =</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">match</span> l2 <span style="color: blue">with</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | [] <span style="color: blue">-&gt;</span> acc</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | h::t <span style="color: blue">-&gt;</span> loop (f2, t, ((f2 l2)::acc))</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; loop (f, l, [])</p>
</div>
<p>You can see that we factored out an inner loop that is recursive, added an accumulator list, and made sure that the loop function call was the last operation before recursing.</p>
<ul>
<li>mapl_tr1: This function captures the function &#8220;f&#8221; (a closure) from within the inner loop.</li>
<li>mapl_tr2: This function explicitly passes the function &#8220;f&#8221; to the inner loop to test if a closure slows things down</li>
<li>mapl_tr3: This function creates tuples when passing values to the inner loop and recursing to test if tuples are faster than partial function applications</li>
</ul>
<p>To test these functions out, I used a simple test harness:</p>
<div style="font-size: 10pt; background: white; color: black; font-family: consolas">
<p style="margin: 0px"><span style="color: blue">let</span> main () =</p>
<p style="margin: 0px">&nbsp; printfn <span style="color: maroon">&#8220;Tail Recursion Test&#8221;</span></p>
<p style="margin: 0px">&nbsp; <span style="color: blue">let</span> run_test m =</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; time_iter 10 (<span style="color: blue">fun</span> () <span style="color: blue">-&gt;</span> [1 .. 100000] |&gt; m List.hd |&gt; ignore)</p>
<p style="margin: 0px">&nbsp; <span style="color: blue">let</span> methods = [List.mapl_tr1; List.mapl_tr2; List.mapl_tr3]</p>
<p style="margin: 0px">&nbsp; <span style="color: blue">let</span> names = [<span style="color: maroon">"mapl_tr1"</span>; <span style="color: maroon">"mapl_tr2"</span>; <span style="color: maroon">"mapl_tr3"</span>]</p>
<p style="margin: 0px">&nbsp; methods</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; |&gt; List.map run_test</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; |&gt; List.zip names</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; |&gt; List.iter (<span style="color: blue">fun</span> i <span style="color: blue">-&gt;</span> printfn <span style="color: maroon">&#8220;%s: %f ms&#8221;</span> (fst i) (snd i))</p>
<p style="margin: 0px">&nbsp; press_enter ()</p>
</div>
<p>This takes a list of the three methods (isn&#8217;t it great to pass around code as data in functional languages?), run a test by timing it and taking the average of 10 executions, then print out the results.</p>
<div style="font-size: 10pt; background: white; color: black; font-family: consolas">
<p style="margin: 0px">Tail Recursion Test</p>
<p style="margin: 0px">mapl_tr1: 46.616836 ms</p>
<p style="margin: 0px">mapl_tr2: 42.788645 ms</p>
<p style="margin: 0px">mapl_tr3: 65.175500 ms</p>
<p style="margin: 0px">Press Enter to continue&#8230;</p>
</div>
<p>Wow, that&#8217;s really interesting. mapl_tr1 and mapl_tr2 are almost the same running time, but mapl_tr2 is slightly faster.&nbsp; So we can conclude that the closure adds a very small amount of overhead, which isn&#8217;t surprising.&nbsp; What caught me off guard is that mapl_tr3 takes more than 50% longer than mapl_tr2, owing to the constant creation of tuple objects under the hood. Looks like we have a winner, mapl_tr2!</p>
<p>For completeness, the implementations of time_iter and press_enter are given below:</p>
<div style="font-size: 10pt; background: white; color: black; font-family: consolas">
<p style="margin: 0px"><span style="color: blue">#light</span></p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px"><span style="color: blue">open</span> System</p>
<p style="margin: 0px"><span style="color: blue">open</span> System.Diagnostics</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px"><span style="color: blue">let</span> press_enter () =</p>
<p style="margin: 0px">&nbsp; printfn <span style="color: maroon">&#8220;Press Enter to continue&#8230;&#8221;</span></p>
<p style="margin: 0px">&nbsp; Console.ReadLine() |&gt; ignore</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px"><span style="color: blue">let</span> time f =</p>
<p style="margin: 0px">&nbsp; <span style="color: blue">let</span> sw = <span style="color: blue">new</span> Stopwatch()</p>
<p style="margin: 0px">&nbsp; sw.Start()</p>
<p style="margin: 0px">&nbsp; f ()</p>
<p style="margin: 0px">&nbsp; sw.Stop()</p>
<p style="margin: 0px">&nbsp; sw.Elapsed.TotalMilliseconds</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px"><span style="color: blue">let</span> time_iter n f =</p>
<p style="margin: 0px">&nbsp; [0 .. n]</p>
<p style="margin: 0px">&nbsp; |&gt; List.map (<span style="color: blue">fun</span> _ <span style="color: blue">-&gt;</span> time f)</p>
<p style="margin: 0px">&nbsp; |&gt; List.average</p>
</div>
<p>(time_iter can be optimized so it doesn&#8217;t create a 0..n list and uses sequence expressions&#8230;)</p>
<h4>Conclusions</h4>
<p>F# makes it really easy to write recursive functions that succinctly express our algorithms, but care must be taken to address tail recursion so that StackOverflowExceptions aren&#8217;t encountered in production code. Also, tail-recursive functions can be further optimized by ensuring that tuple objects aren&#8217;t created at each iteration, and by explicitly passing functions to inner loops and not relying on closures.</p>
<p>Whenever you write &#8220;let rec&#8221;, don&#8217;t give the Tail Recursion Police reason to take you downtown&#8230;</p>
<p>Hope that helps someone and eliminates some of the smoke/mirrors surrounding tail recursion!&nbsp; <img src='http://thevalerios.net/matt/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/ValerioDotNet/~4/LsEfiDLMjgw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://thevalerios.net/matt/2009/01/recursion-in-f-and-the-tail-recursion-police/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://thevalerios.net/matt/2009/01/recursion-in-f-and-the-tail-recursion-police/</feedburner:origLink></item>
		<item>
		<title>Assembly Information for F# Console Applications</title>
		<link>http://feedproxy.google.com/~r/ValerioDotNet/~3/tjOdhR1adwo/</link>
		<comments>http://thevalerios.net/matt/2009/01/assembly-information-for-f-console-applications/#comments</comments>
		<pubDate>Sun, 04 Jan 2009 23:18:59 +0000</pubDate>
		<dc:creator>matt</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[AssemblyInfo]]></category>
		<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">http://thevalerios.net/matt/2009/01/assembly-information-for-f-console-applications/</guid>
		<description><![CDATA[Right on the heels of my last post about an AssemblyInfo.fs file for F# Libraries (DLLs), we can do the same thing for F# console applications with a slight modification. The default F# console application contains a single Program.fs source file. By default, this puts everything in that file into a module named &#8220;Program&#8221;. The [...]]]></description>
			<content:encoded><![CDATA[<p>Right on the heels of my <a href="http://thevalerios.net/matt/2009/01/assembly-information-for-f-libraries/">last post about an AssemblyInfo.fs file for F# Libraries (DLLs),</a> we can do the same thing for F# console applications with a slight modification.</p>
<p>The default F# console application contains a single Program.fs source file. By default, this puts everything in that file into a module named &#8220;Program&#8221;.</p>
<p>The entry point of an F# console application is typically the top-most code in the last .fs file of the project.&nbsp; This leads to the standard F# programming pattern of the last file in the project (typically Program.fs) containing something along the lines of:</p>
<div style="font-size: 10pt; background: white; color: black; font-family: consolas">
<p style="margin: 0px"><span style="color: blue">&#8230;</span></p>
<p style="margin: 0px"><span style="color: blue">let</span> main () =</p>
<p style="margin: 0px">&nbsp; &#8230;</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">main ()</p>
</div>
<p>Now, to add all of the assembly metadata to the project, we need to attribute the &#8220;main ()&#8221; function call with all of the attributes.&nbsp; We can&#8217;t use the &#8220;unit&#8221; operator &#8220;()&#8221; like we did in the F# Library case, since our console application actually does have an entry point that we care about.</p>
<p>We can accomplish this by moving the &#8220;main ()&#8221; line to the AssemblyInfo.fs file, requiring us to fully-qualify it to &#8220;Program.main ()&#8221; since it resides in the Program module.</p>
<p>If we add the AssemblyInfo.fs file from the previous post, it must go last, for example:</p>
<p><a href="http://thevalerios.net/matt/wp-content/uploads/2009/01/image2.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="105" alt="image" src="http://thevalerios.net/matt/wp-content/uploads/2009/01/image-thumb2.png" width="217" border="0"></a> </p>
<p>Now, in Program.fs we only have:</p>
<div style="font-size: 10pt; background: white; color: black; font-family: consolas">
<p style="margin: 0px">&#8230;</p>
<p style="margin: 0px"><span style="color: blue">let</span> main () =</p>
<p style="margin: 0px">&nbsp; &#8230;</p>
</div>
<p>And at the end of AssemblyInfo.fs we have:</p>
<div style="font-size: 10pt; background: white; color: black; font-family: consolas">
<p style="margin: 0px">&#8230;</p>
<p style="margin: 0px">[&lt;assembly: AssemblyVersion(<span style="color: maroon">"1.0.0.0"</span>)&gt;]</p>
<p style="margin: 0px">[&lt;assembly: AssemblyFileVersion(<span style="color: maroon">"1.0.0.0"</span>)&gt;]</p>
<p style="margin: 0px">Program.main ()</p>
</div>
<p>Just like in the library case, it would be nice if the default F# console application template included both Program.fs and AssemblyInfo.fs and included a definition for &#8220;main&#8221; to avoid any confusion as to the entry point of the application.</p>
<p>Hope that helps!</p>
<img src="http://feeds.feedburner.com/~r/ValerioDotNet/~4/tjOdhR1adwo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://thevalerios.net/matt/2009/01/assembly-information-for-f-console-applications/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://thevalerios.net/matt/2009/01/assembly-information-for-f-console-applications/</feedburner:origLink></item>
	</channel>
</rss>
