<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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/"
	>

<channel>
	<title>So Jake Says &#187; Generalization</title>
	<atom:link href="http://www.jakevoytko.com/blog/tag/generalization/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jakevoytko.com/blog</link>
	<description>Ye Olde Computer Science Blogge</description>
	<lastBuildDate>Sun, 17 Jan 2010 15:16:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Lisp is Changing My C++</title>
		<link>http://www.jakevoytko.com/blog/2008/10/06/lisp-is-changing-my-c/</link>
		<comments>http://www.jakevoytko.com/blog/2008/10/06/lisp-is-changing-my-c/#comments</comments>
		<pubDate>Mon, 06 Oct 2008 04:00:59 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Generalization]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Paul Graham]]></category>
		<category><![CDATA[Peter Norvig]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=167</guid>
		<description><![CDATA[I am a mild-mannered C#/C++ programmer by day, and I have been learning Lisp in my spare time. Comparatively speaking, I do not spend much time programming for personal projects (only an hour or two a day), but I noticed something awesome recently: Lisp is influencing the way I write C++ in a few different [...]]]></description>
			<content:encoded><![CDATA[<p>I am a mild-mannered C#/C++ programmer by day, and I have been learning Lisp in my spare time. Comparatively speaking, I do not spend much time programming for personal projects (only an hour or two a day), but I noticed something awesome recently:</p>
<p>Lisp is influencing the way I write C++ in a few different ways.</p>
<h2>To Generalize&#8230;</h2>
<p>Imagine my horror: Lisp caused me to realize that I&#8217;ve been refactoring the <em>wrong way</em>! For a few years!</p>
<p>Well, that is not <em>entirely</em> accurate. My code did the same thing after I was done. I broke code down into small logical units. Unless I was careless, the code still worked. My functions were more readable, and one could usually see what they did at a glance.</p>
<p>I asked myself questions along the lines of, &#8220;What can I do to break this up logically?&#8221;, and therein lies the problem:</p>
<blockquote><p><strong>Weak questions have weak answers.</strong></p></blockquote>
<p>Powerful questions are much better suited to refactoring!</p>
<ul>
<li>&#8220;Can I use an existing solution to solve this?&#8221;</li>
<li>&#8220;Could this solve a completely unrelated problem?&#8221;</li>
<li>&#8220;Can I rearrange things elsewhere so that this code isn&#8217;t even needed?&#8221;</li>
</ul>
<p>Sometimes, the answer is no. Some boilerplate code (like exception catching) is an overhead that can not be avoided, can not usually be generalized, and sticks out like a sore thumb in C++ style languages.</p>
<p>Why did Lisp fundamentally shape my refactoring practices? Paul Graham and Peter Norvig are both to blame for this, and for different reasons.</p>
<p>First up, Graham. I taught myself from &#8220;<a href="http://www.jakevoytko.com/blog/2008/07/14/review-of-ansi-common-lisp/">ANSI Common Lisp</a>&#8220;. At first, I attacked the problems in the book from a C++ perspective, trying to wedge loops and functions into my solutions. Doing the Lisp examples forced me to think simpler. Within a month I started being able to think of functional solutions instead of C++ solutions (reaching for reduce and lambdas instead of loops and classes).</p>
<p>I worked problems out of this book every night for a month and a half, and it still sits next to me as a quick reference. After 8 years of C++ programming, it took a while for things like &#8220;reduce&#8221; to become the most obvious solution to a problem.</p>
<p>I haven&#8217;t reached that magical enlightening moment that Lisp causes. I don&#8217;t have the &#8220;ooh, shiny!&#8221; awe over macros that seasoned Lisp programmers seem to have, so I&#8217;m still hacking away.</p>
<p>Norvig has sent me towards enlightenment with <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.com%2FParadigms-Artificial-Intelligence-Programming-Studies%2Fdp%2F1558601910&amp;tag=jakvoyshom-20&amp;linkCode=ur2&amp;camp=1789&amp;creative=9325">Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp</a><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=jakvoyshom-20&amp;l=ur2&amp;o=1" border="0" alt="" width="1" height="1" />. In these pages, I found something that was missing from Graham: elegance</p>
<p>Some of the solutions that I see in this book give me a sense of awe and enlightenment that I haven&#8217;t had since I took Abstract Algebra. He presents them in a very straightforward and honest style that can only come with experience.</p>
<p>This book has given me a whole new level of quality to try to reach. I hope one day to be able to do this.</p>
<h2>Tools</h2>
<p>A year ago, I would have described my programming process like this:</p>
<ol>
<li>Design</li>
<li>Code + document</li>
<li>Neaten + prune</li>
<li><em>Return to 1, as needed</em></li>
</ol>
<p>Not bad. However, working with Lisp and seeing that there are different tools in the world has led me to fundamentally restructure how I program. I would now describe my process like this:</p>
<ol>
<li>Design</li>
<li>Code + document</li>
<li>&#8220;Would a tool/function/class have made this task easier?&#8221;</li>
<li><em>Return to 1, as needed</em>.</li>
</ol>
<p>For instance, I&#8217;m writing some image processing libraries in CL right now, and I&#8217;ve written a few tools that I might never have thought of generalizing from a pure-C++ mindset! After all, iterating over an image is really easy in C++, so why bother making it simpler?</p>
<p>However, <code>map-image</code> is invaluable: apply a function at each pixel of an image, and return a new image. Combine this with the <code>lambda</code> construct, and you have a level of flexibility possible only with some of the goofier Boost libraries.</p>
<h2>In Short&#8230;</h2>
<p>While working through the Graham examples, I found that I was playing Code Golf against myself.</p>
<p>I&#8217;ve started to see this carry over to my coding at work as well. In the past, I might have done something like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> num_times <span style="color: #000080;">=</span> SomeFunction<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">return</span> AnotherFunction<span style="color: #008000;">&#40;</span>num_times<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>This is a reasonable approach. The local variable name reflects the role of the function, and adds a little bit of clarity for the human reader. Using this style is fine if these are your priorities.</p>
<p>However, I&#8217;ve started giving functions better names (when I can), and cutting out the middleman:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">return</span> AnotherFunction<span style="color: #008000;">&#40;</span>NumberOfTimes<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>This is effectively the same code, and there&#8217;s a good chance that it would compile down to the same assembly language. It&#8217;s just as readable. The difference is the line count. It&#8217;s a difference small enough for a <a href="http://www.bikeshed.com/">bikeshed issue holy war</a>, but over the course of small projects (all I&#8217;ve done so far in Lisp), being able to fit more code on my screen is a big win for me (all else equal).</p>
<p>I am starting to prefer reading and writing compressed code as a result of trying to solve Graham&#8217;s examples in as few lines as possible.</p>
<h2>Future Directions</h2>
<p>I&#8217;m still looking for my &#8220;Lisp Enlightenment&#8221; with respect to macros, which leads me to believe that there&#8217;s something significant that I&#8217;m still not getting about Common Lisp. I&#8217;m not sure where all of these new techniques are going to lead me, but in the meantime, it is starting to fundamentally change how I approach programming problems.</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=167&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2008/10/06/lisp-is-changing-my-c/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
