<?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; Computer Science</title>
	<atom:link href="http://www.jakevoytko.com/blog/category/computer-science/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>Starting From Scratch</title>
		<link>http://www.jakevoytko.com/blog/2009/02/02/starting-from-scratch/</link>
		<comments>http://www.jakevoytko.com/blog/2009/02/02/starting-from-scratch/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 05:00:36 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Misc]]></category>
		<category><![CDATA[Politics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[Linus Torvalds]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Podcast]]></category>
		<category><![CDATA[starting from scratch]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=367</guid>
		<description><![CDATA[I believe that everyone hates the feeling of starting from scratch, regardless of age or experience. This will cause people who should know better to make bad decisions, even misleading people who are conscious of the effect. In my mental model of knowledge, I split skills between three major mastery levels: Those we can use [...]]]></description>
			<content:encoded><![CDATA[<p>I believe that everyone <em>hates</em> the feeling of starting from scratch, regardless of age or experience. This will cause people who should know better to make bad decisions, even misleading people who are conscious of the effect.</p>
<p>In my mental model of knowledge, I split skills between three major mastery levels:</p>
<ul>
<li><strong>Those we can use professionally</strong>. I program professionally, and I practice daily to strive for mastery. I&#8217;m getting to the point where I can handle any task that is thrown at me.</li>
<li><strong>Those we can use</strong>. I can cook without killing myself of burning down my apartment complex, even though I can&#8217;t make many types of food and wouldn&#8217;t recognize a Mother Sauce if it was poured down my shirt.</li>
<li><strong>Those we couldn&#8217;t <em>really</em> use if our life depended on it.</strong> I&#8217;m not good at drawing. I&#8217;ve never spent time on it, and I have no motivation to learn. I can &#8220;draw&#8221; diagrams and get my point across, and this is enough for me.</li>
</ul>
<p>All people treat the skills from the first category differently than the other two: we use them with confidence. Some people use them with arrogance. We know these fields well enough to absorb innovations and new ways of thinking. We can confidently discuss them with other people.</p>
<p>In my mental model, starting from scratch in our expert area causes people to reject a change. For instance: an Excel or Word guru would never willingly switch to a completely unrelated product. Relearning things line inserting line-breaks and adding pictures to a text document is completely orthogonal to anything they actually have to do on the job, much less something more complicated like Mail Merge. Instead of using it like it should be used, they would likely try using it like they used their old product, declare that it can&#8217;t ramrod the flimjam, and switch back to their old way of doing things, <em>regardless of which is really better.</em></p>
<h2>Interfaces and APIs</h2>
<p>Altered interfaces are difficult for the end user. This isn&#8217;t just about GUIs or the placement of buttons in a menu; this extends to software APIs as well.</p>
<p>An altered menu causes all users to waste time looking for new ways to do something they could do 5 minutes ago. Changes to an API cause the programmers relearn a library they already knew &#8212; not to mention the laundry list of changes that need to happen to the new stuff!</p>
<h3>APIs and Interfaces</h3>
<p>The interface for a library or class develops massive unavoidable dependencies with use. I&#8217;ve called this &#8220;<a href="http://www.jakevoytko.com/blog/2008/11/17/inertia-and-large-code-bases/">code inertia</a>&#8221; in the past. Joel and Jeff&#8217;s <a href="http://www.joelonsoftware.com/items/2009/01/31.html">recent discussion</a> of dependencies between unit tests and code is a perfect example of code inertia: making nontrivial changes to an application can force a nontrivial number of changes to your automated testing environment.</p>
<p>This has not only a physical effect on the code, but also a mental effect for programmers! I&#8217;ve actually heard arguments from programmers based on the IDEA of possible future change: <em> we shouldn&#8217;t use this library because it&#8217;s changed significantly in the past, and it might change again!</em> Having to relearn the API combined with the frustration of updating broken code leaves a giant red flag in a programmer&#8217;s mind, even though <strong>any</strong> codebase that is being properly maintained will go through interface changes as new versions are released.</p>
<h3>Excel</h3>
<p>I was fortunate enough to take a Numerical Methods and Analysis class during my senior year of college.</p>
<p>If you&#8217;re not familiar, it&#8217;s the mathematics of computation: finding efficient approximations, determining rate of convergence, and knowing the margin of error at each stage.</p>
<p>This was my only college class where a calculator was ever useful. Most of our homework involved finding the values and error in iterative processes, so the professor suggested a &#8220;complex math package&#8221; for our homework: Excel.</p>
<p>Despite my initial skepticism, Excel is <em><strong>AWESOME</strong></em> with iterative numerical computation: type in a relative formula, highlight 50 rows, and click &#8220;fill down.&#8221; Voila! You can put a bunch of results in adjacent columns and compare convergence rates visually before you even prove that it converges at a certain rate. It&#8217;s quick, it&#8217;s interactive, and it supports easy experimentation.</p>
<p>Why is this relevant? My college upgraded to the Microsoft Office 2007 suite the previous summer, <a href="http://lifehacker.com/software/screenshot-tour/screenshot-tour--microsoft-word-and-excel-2007-225875.php">toolstrip and all</a>. Every single class was punctuated by my professor loudly complaining that she HATED the new Excel because the interface was WORSE and she couldn&#8217;t find anything.</p>
<p>Even though she knew where everything was after 2 weeks of use, we got the same complaint every week. She had already made up her mind: it was worse.</p>
<h3>KDE and Linus</h3>
<p>A few years ago, Linus Torvalds <a href="http://mail.gnome.org/archives/usability/2005-December/msg00021.html">publicly</a> announced his Linux desktop preference on the GNOME message boards:</p>
<blockquote><p>I personally just encourage people to switch to KDE.</p>
<p>This &#8220;users are idiots, and are confused by functionality&#8221; mentality of Gnome is a disease. If you think your users are idiots, only idiots will use it. I don&#8217;t use Gnome, because in striving to be simple, it has long since reached the point where it simply doesn&#8217;t do what I need it to do.</p>
<p>Please, just tell people to use KDE.</p>
<p>Linus</p></blockquote>
<p>The different camps typed their representative emoticons &#8212; <img src='http://www.jakevoytko.com/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  and D: &#8212; and then life continued as normal. However, Linus was eventually forced to switch when the next version of KDE <a href="http://www.techworld.com.au/article/273956/open_source_identity_linux_founder_linus_torvalds?pp=4">changed too much</a>:</p>
<blockquote><p><strong>Interviewer</strong>: Another open source project that underwent a big change was KDE with version 4.0. They released a lot of fundamental architectural changes with 4.0 and it received some negative reviews. As a KDE user how has this impacted you?</p>
<p><strong>Linus</strong>: I used to be a KDE user. I thought KDE 4.0 was such a disaster I switched to GNOME. I hate the fact that my right button doesn&#8217;t do what I want it to do. But the whole &#8220;break everything&#8221; model is painful for users and they can choose to use something else.</p>
<p>I realise the reason for the 4.0 release, but I think they did it badly. They did so may[sic] changes it was a half-baked release. It may turn out to be the right decision in the end and I will re-try KDE, but I suspect I&#8217;m not the only person they lost.</p>
<p>I got the update through Fedora and there was a mismatch from KDE 3 to KDE 4.0. The desktop was not as functional and it was just a bad experience for me. I&#8217;ll revisit it when I reinstall the next machine which tends to be every six to eight months.</p>
<p>The GNOME people are talking about doing major surgery so it could also go the other way.</p></blockquote>
<p>Linus realizes that it might be the <em>right</em> decision in the long run, but there&#8217;s no escaping the fact that even the big guns in the field can&#8217;t spend his time relearning the basics every day, even if it&#8217;s the right thing to do. He has a mental model of how the desktop is supposed to work, and when KDE broke it, he fixed the problem.</p>
<h2>Adult Learning and Malcolm Knowles</h2>
<p>To me, a lot of the principles of API and interface design fall under adult learning. To state the obvious, most of the indended clients for interfaces are adults. We&#8217;d like them to learn how to use our products with the minimal amount of fuss.</p>
<p>However, if your interface is exactly like every other interface, there would be no reason to use it. You&#8217;re trying to solve a problem for yourself, and you&#8217;re trying to solve a problem for your intended users. Your users will have to learn how to solve their problems with your interface, and it had better be easy.</p>
<p>Enter the research of <a href="http://www.infed.org/thinkers/et-knowl.htm">Malcolm Knowles</a>. He is one of the big-shots in the field of adult learning, having done a lot of the early theoretical work. <a href="http://www.infed.org/thinkers/et-knowl.htm&gt;Here&lt;/a&gt; are his listed assumptions about what drives the education of adults:&lt;/p&gt; &lt;blockquote&gt;&lt;p&gt; 1. Self-concept: As a person matures his self concept moves from one of being a dependent personality toward one of being a self-directed human being.&lt;br /&gt; 2. Experience: As a person matures he accumulates a growing reservoir of experience that becomes an increasing resource for learning.&lt;br /&gt; 3. Readiness to learn. As a person matures his readiness to learn becomes oriented increasingly to the developmental tasks of his social roles.&lt;br /&gt; 4. Orientation to learning. As a person matures his time perspective changes from one of postponed application of knowledge to immediacy of application, and accordingly his orientation toward learning shifts from one of subject-centeredness to one of problem centredness.&lt;br /&gt; 5. Motivation to learn: As a person matures the motivation to learn is internal. &lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;I highly recommend looking through either the above link, or this one from the &lt;a href=">This site</a> has a good description of his life&#8217;s work with researching, for the interested.</p>
<p>In particular, Knowles attempted to find the theory of what caused adults to best learn. Believe it or not, there hadn&#8217;t been extensive research in the field before he started. The closest that he was able to come was his theory of <em>informal learning</em>, through group sessions or other kinds of informal meetings. He also developed his assumptions by which adults learn:</p>
<blockquote><p><strong>Malcolm Knowle&#8217;s Assumptions on Adult Learning</strong></p>
<ol>
<li>Self-concept: As a person matures his self concept moves from one of being a dependent personality toward one of being a self-directed human being.</li>
<li>Experience: As a person matures he accumulates a growing reservoir of experience that becomes an increasing resource for learning.</li>
<li>Readiness to learn. As a person matures his readiness to learn becomes oriented increasingly to the developmental tasks of his social roles.</li>
<li>Orientation to learning. As a person matures his time perspective changes from one of postponed application of knowledge to immediacy of application, and accordingly his orientation toward learning shifts from one of subject-centeredness to one of problem centredness.</li>
<li>Motivation to learn: As a person matures the motivation to learn is internal.</li>
</ol>
</blockquote>
<p>The salient bullet point is #2: experience. It&#8217;s <em>directly</em> listed as a basis for adult learning. I&#8217;m not saying that this is a direct proof of my mental model of knowledge, because quite honestly it&#8217;s just not a proper conclusion. However, designers of APIs and GUIs should take this as an indication that experience is something to build on, not disregard.</p>
<p>To use an aforementioned example, I really like the ribbon interface in Office 2007! I was proficient after only a few hours, and I enjoyed using fewer keystrokes to achieve my goals. I thought that it mapped pretty well to the old menu-based methods, and it was pretty obvious when it didn&#8217;t. If I could use only keyboard shortcuts to navigate, it would be the ideal control.</p>
<p>However, it&#8217;s probably a bad idea releasing it in the same year as Vista, which already scrambled some menus, changed some positions, altered some interfaces, renamed some popular folders, added UAC, etc. It all added up to too much of a change: when people sat down at it for the first time, nobody couldn&#8217;t <strong>use</strong> the darned thing.</p>
<p>Live and learn!</p>
<blockquote><p><strong>Footnote</strong></p>
<p>I remember a minor fuss when Linux 2.6.0 came out because the device driver interface changed. Linus himself descended from the cloud to state that he makes the right design choices for Linux without regard to compatibility issues. It was going to tie in really nicely with my point, and my Google Fu has failed me. up. The <a href="http://lwn.net/Articles/driver-porting/">device driver interface changed</a> from 2.4 to 2.6, and that there was a <a href="http://www.google.com/search?q=2.6.0+device+driver+problem&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=com.ubuntu:en-US:unofficial&amp;client=firefox-a"></a>of device driver complaints. At this point, I just want to know if I was making this up.</p></blockquote>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=367&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2009/02/02/starting-from-scratch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MIT&#8217;s BattleCode Competition</title>
		<link>http://www.jakevoytko.com/blog/2009/01/12/mits-battlecode-competition/</link>
		<comments>http://www.jakevoytko.com/blog/2009/01/12/mits-battlecode-competition/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 05:08:21 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Archon]]></category>
		<category><![CDATA[BattleCode]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[MIT]]></category>
		<category><![CDATA[Strategy]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=358</guid>
		<description><![CDATA[BattleCode is an annual programming competition hosted by MIT students. Teams of 1 to 4 people design real-time strategy AI and compete head-to-head with other teams. Overview of BattleCode The competition&#8217;s focus tends to vary from year to year. Last year&#8217;s competition could be won either by capturing most of the towers on the map [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://battlecode.mit.edu/2009/info">BattleCode</a> is an annual programming competition hosted by MIT students. Teams of 1 to 4 people design real-time strategy AI and compete head-to-head with other teams.</p>
<h2>Overview of BattleCode</h2>
<p>The competition&#8217;s focus tends to vary from year to year. Last year&#8217;s competition could be won either by capturing most of the towers on the map or by completely wiping out the players on the other team.</p>
<p>It&#8217;s even better this year: it&#8217;s primarily a resource-managing competition. Certain tiles on the game board contains a resource (flux) that you can gather. If you have more when the round ends &#8212; or cross a magic threshold during the match &#8212; then your team will be declared the winner.</p>
<p><a href="http://www.jakevoytko.com/blog/wp-content/uploads/2009/01/battlecode-screenshot.png"><img class="aligncenter size-full wp-image-362" title="battlecode-screenshot" src="http://www.jakevoytko.com/blog/wp-content/uploads/2009/01/battlecode-screenshot.png" alt="A BattleCode round in action." /></a>It&#8217;s probably still possible to create a winning team that is entirely militaristic, but I have a feeling that the organizers are trying to reward teams that split-up. Most of the teams that split up into groups last year were defeated by monolithic swarms, but this mechanism of resource mining and control might better side with teams that split.</p>
<p>The good news is that it seems like there is a core group of functionality that remains the same from year to year, so code that is written for a previous BattleCode tournament is still usable the next year. I just found out about it, so I&#8217;m in the process of developing a player from scratch.</p>
<h2>Great Design Decisions</h2>
<p>A great BattleCode design decision involves the maximum number of units: it&#8217;s self-regulating. There is no &#8216;official&#8217; limit to the number of units that can run at any one time, but all units must return to one of 6 central energon-producing units (Archons) from time-to-time or they die. The Archons produce Energon at a constant rate, preventing too many units from existing at once.</p>
<p>The genius of the design of the soft-cap is that it allows two extra strategies into the game: first, you can send some of your units on a suicide run guilt-free! If a unit decides that it&#8217;s FUBAR, it can send a message home saying &#8220;Just trying to kill some bugs, sir!&#8221; and its parent can feel free to spawn a new unit at its earliest convenience.</p>
<p>As a result, the Archons are as valuable as a King in chess: it&#8217;s not very powerful, but you can&#8217;t afford to lose it.</p>
<h2>Java-Based Programming</h2>
<p>The competition software uses the Java VM to control the matches. The user overrides a Runnable class named RobotPlayer. The <code>run()</code> function lets the user define the behavior of the AI agent. They provide a default function that solely moves the AI, but it probably won&#8217;t get you very far in the main competition.</p>
<p>The <code>run()</code> function is written as a loop, and robots don&#8217;t have a clear idea of a separation of turns: they are limited to 3000 Java bytecodes per &#8220;turn,&#8221; and the controller switches to the next robot automatically. A &#8220;<code>yield()</code>&#8221; function is provided that artificially ends a turn.</p>
<h2>Very Algorithm-Friendly</h2>
<p>There is no centralized user-written player to pass strategy and tactics to individual units: they have to decide that amongst themselves. You need to tell them how to move, how to fight, and they need to be able to manage their own health.</p>
<p>The two major categories of algorithms that need to be developed are dynamic path-finding algorithms (because a robot needs to know where it&#8217;s going!) and distributed organization. You need to be able to use your units to protect your Archons, they need to be effective fighters, and it would be a poor unit that gets stuck on the first non-passable square it encounters.</p>
<p>I have no doubt that some teams are eventually able to publish papers on some of the organization techniques that they use: quite a bit of work can be accomplished in 3000 bytecodes, and I bet that some of the organization and attack techniques are more sophisticated than many papers that have been actually published.</p>
<h2>My Plans</h2>
<p>My goal is to have a force that can fully spawn, move, and fight by the end of January, and my final strategy and tactics to be decided by the end of February. I will most likely merge with a team that has a few of my friends by the end of the month, but you&#8217;ll be able to find me by my username (jakevoytko).</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=358&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2009/01/12/mits-battlecode-competition/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Everything You Need To Get Started With Common Lisp</title>
		<link>http://www.jakevoytko.com/blog/2008/12/29/everything-you-need-to-get-started-with-common-lisp/</link>
		<comments>http://www.jakevoytko.com/blog/2008/12/29/everything-you-need-to-get-started-with-common-lisp/#comments</comments>
		<pubDate>Mon, 29 Dec 2008 05:00:23 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[asdf-install]]></category>
		<category><![CDATA[emacs]]></category>
		<category><![CDATA[getting started with lisp]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[lisp implementation]]></category>
		<category><![CDATA[Paul Graham]]></category>
		<category><![CDATA[SBCL]]></category>
		<category><![CDATA[SLIME]]></category>
		<category><![CDATA[Superior Lisp Interaction Mode for Emacs]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=343</guid>
		<description><![CDATA[Quick-Links for the TL;DR Crowd About Lisp: Describes Lisp as an overview. High Level Overview: Describes in general what is needed to program with Lisp. All you need SBCL Emacs SLIME asdf-install Code examples: A few rudimentary Lisp snippets. Other Lisp Features: Features that I did not feel like explaining Resources About Lisp Note: For [...]]]></description>
			<content:encoded><![CDATA[<blockquote>
<h2>Quick-Links for the TL;DR Crowd</h2>
<p><span style="text-decoration: underline;"><a href="#about">About Lisp</a>:</span> Describes Lisp as an overview.<br />
<span style="text-decoration: underline;"><a href="#high-level">High Level Overview</a>:</span> Describes in general what is needed to program with Lisp.<br />
<span style="text-decoration: underline;"><a href="#what-you-need">All you need</a></span></p>
<ul>
<li><span style="text-decoration: underline;"><a href="#sbcl">SBCL</a></span></li>
<li><span style="text-decoration: underline;"><a href="#emacs">Emacs</a></span></li>
<li><span style="text-decoration: underline;"><a href="#slime">SLIME</a></span></li>
<li><span style="text-decoration: underline;"><a href="#asdf-install">asdf-install</a></span></li>
</ul>
<p><span style="text-decoration: underline;"><a href="#starting">Code examples</a></span>: A few rudimentary Lisp snippets.<br />
<span style="text-decoration: underline;"><a href="#other-features">Other Lisp Features</a>:</span> Features that I did not feel like explaining <img src='http://www.jakevoytko.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<span style="text-decoration: underline;"><a href="#resources">Resources</a></span></p></blockquote>
<p><a name="about"></a></p>
<h2>About Lisp</h2>
<p><em><strong>Note:</strong> For a complete introduction, I recommend this <a href="http://www.gigamonkeys.com/book/">e-book</a>. If you only want to learn enough to get started, read on!</em></p>
<h3>Interactive</h3>
<p>Most languages take an input file and produce an executable or run a script. Lisp is interactive: all input is entered into the REPL, and the computation&#8217;s result is printed to the screen.</p>
<blockquote><p><strong>REPL:</strong> <strong>R</strong>ead <strong>E</strong>valuate <strong>P</strong>rint <strong>L</strong>oop. A C++ coder can think of it like this:</p>
<pre lang="c++">void lisp()
{
    while(true){
        cout&lt;&lt;"&gt;"; // prompt
        cout&lt;&lt;evaluate(read())&lt;&lt;endl;
    }
}</pre>
</blockquote>
<p>If you&#8217;ve ever used Python&#8217;s command line prompt, you&#8217;ve used a REPL. A bash prompt is a REPL. So is Lisp.</p>
<h3>Prefix</h3>
<p>Most expressions in C-style languages are prefix, but exceptions are made for arithmetic, such as 1/(x+3). All expressions in Lisp are prefix expressions: the first argument of an S-expression (Sexp) is a function name, and the rest of the atoms of the S-expression are the arguments for the function.  The following are examples of Lisp function calls:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>/ <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#40;</span>+ x <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>         <span style="color: #808080; font-style: italic;">; 1/(x+3)</span>
<span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;~A~%&quot;</span> x<span style="color: #66cc66;">&#41;</span>    <span style="color: #808080; font-style: italic;">; prints the value of 'x' and a newline.</span></pre></div></div>

<h3>All Expressions Return Values</h3>
<p>All expressions in Lisp return values. For instance, the &#8216;<code>if</code>&#8216; statement in Lisp not only chooses which execution branch to follow, but returns the result of the executed expression. &#8216;<code>nil</code>&#8216; is the default return value of an expression, which also evaluates to false (&#8216;<code>t</code>&#8216; is true).</p>
<p>In C-style languages, expressions don&#8217;t necessarily return values. The expression &#8216;<code>1+3</code>&#8216; will return &#8216;<code>4</code>&#8216;, but <del datetime="2008-12-29T13:36:49+00:00">&#8216;<code>cout&lt;&lt;1+3</code>&#8216; doesn&#8217;t return anything</del><em> (They return a reference to an <code>ostream</code>, so this is a mistake</em>) void functions don&#8217;t return anything; they can&#8217;t be used as a sub-expression for a larger statement of code.</p>
<p>Functions in Common Lisp can return multiple values. The &#8216;<code>multiple-value-bind</code>&#8216; macro is used to bind multiple return values to multiple variables.<br />
<a name="high-level"></a></p>
<h2>High Level Overview: What You Need To Get Started</h2>
<p>1) <strong>An implementation</strong>. All you need is an implementation that conforms to the standards. All of them have an edge case or two, but odds are it won&#8217;t affect you. If you absolutely need to switch later (for CLisp&#8217;s fast arbitrary precision arithmetic, for instance), nothing is stopping you; most libraries support all popular implementations.</p>
<p>2) <strong>An editor</strong>. I personally recommend Emacs with the SLIME extension. You can also use <a href="http://www.vim.org/">Vim</a> and <a href="http://vim.sourceforge.net/scripts/script.php?script_id=2219">Limp</a> if you either prefer Vim or you can&#8217;t stand Emacs. I left Vim before I started using Emacs, so I&#8217;ve never used Limp. <em>Caveat emptor!</em></p>
<p>You don&#8217;t absolutely need to use an editor that interacts with Lisp. It&#8217;s POSSIBLE to run a Lisp program like a script, but it usually makes as much sense as opening a bag of chips with a hammer. You will want to use the REPL for most of your Lisp tasks, and this will only be bearable from within a good editor.</p>
<p>3) <strong>A Lisp tutorial or reference</strong>. I highly recommend &#8220;<span style="text-decoration: underline;"><a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.com%2FANSI-Common-Prentice-Artificial-Intelligence%2Fdp%2F0133708756%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1215999519%26sr%3D8-1&amp;tag=jakvoyshom-20&amp;linkCode=ur2&amp;camp=1789&amp;creative=9325">ANSI Common Lisp</a></span>&#8221; by Paul Graham [<a href="http://www.jakevoytko.com/blog/2008/07/14/review-of-ansi-common-lisp/">see my review</a>].</p>
<p>4) <strong>An installation system</strong>, like <a href="http://www.cliki.net/ASDF-Install">asdf-install</a>. Installation systems automatically load Lisp projects into your current image, and are essential for working with a large project. asdf-install allows you to download libraries from cliki.net and load them into your Lisp image from within the interactive Lisp command prompt!<br />
<a name="what-you-need"></a></p>
<h2>Details: What You Need To Get Started</h2>
<p><a name="sbcl"></a></p>
<h3>SBCL</h3>
<p><strong>About</strong></p>
<p>Steel Bank Common Lisp (SBCL) was forked from Carnagie-Mellon University Common Lisp (CMUCL) in 1999. It is named after Carnagie and Mellon&#8217;s respective industries, steel and banking.</p>
<p>Most Lisps can be interpreted or compiled, depending on the context. However, SBCL compiles all Lisp code it receives. Having used SBCL for 6 months or so, there&#8217;s not much of a pause even for large Lisp files. On the other hand, the compiler is very verbose, but this can be tweaked.</p>
<p><strong>Why SBCL: Popular</strong></p>
<p>Naturally, I can&#8217;t find good numbers on the popularity of SBCL. However, it is noteworthy that <a href="http://www.cliki.net/ASDF-Install">asdf-install</a> and LIMP were initially designed to work with <a href="http://www.vim.org/scripts/script.php?script_id=2219">SBCL</a>. This seems to be par for the course for all of the libraries I&#8217;ve used.</p>
<p><strong>Why SBCL: Active Development</strong></p>
<p>Development of SBCL is still roaring along, with 1,228 commits to their <a href="http://sourceforge.net/projects/sbcl/">Sourceforge</a> page at the time of writing, including several in the past few weeks. New releases come at the beginning of every month, and this month was no exception: version 1.0.23 was produced December 1, 2008.</p>
<h3>Installing SBCL</h3>
<p><strong>Installing SBCL: Linux</strong></p>
<p>SBCL can be installed like any other package:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">aptitude</span> <span style="color: #c20cb9; font-weight: bold;">install</span> sbcl
<span style="color: #666666; font-style: italic;"># Or your local variant.</span></pre></div></div>

<p>Once that&#8217;s finished, it&#8217;s easy to test the installation:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">jake<span style="color: #000000; font-weight: bold;">@</span>justalaptop:~<span style="color: #000000; font-weight: bold;">/</span>code<span style="color: #000000; font-weight: bold;">/</span>genesis$ sbcl
<span style="color: #7a0874; font-weight: bold;">&#40;</span>+ <span style="color: #000000;">2</span> <span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">*</span>
<span style="color: #000000;">4</span></pre></div></div>

<p><strong>Installing SBCL: Windows</strong></p>
<p>There is an &#8220;experimental&#8221; Windows installation binary: [<a href="http://prdownloads.sourceforge.net/sbcl/sbcl-1.0.22-x86-windows-binary.msi">link</a>].</p>
<p>I&#8217;ve ran it for brief periods and it seems to work fine, so I&#8217;m not sure what&#8217;s so experimental about it. Again, <em>Caveat emptor</em>!</p>
<p>After installation, run SBCL. You will get a command-line prompt, and type the following:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (+ 2 2)</span>
*
<span style="color: #cc66cc;">4</span></pre></div></div>

<p>It works!<br />
<a name="emacs"></a></p>
<h3>Emacs</h3>
<p>You can use whatever editor that you want to write Lisp. Emacs has good advantages:</p>
<p><strong>ELisp:</strong> Emacs is scripted in its own subset of Common Lisp. Called &#8216;elisp&#8217;, anybody who has been working with Lisp for a few months and can type &#8220;<a href="http://www.google.com/search?q=elisp+manual&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=com.ubuntu:en-US:unofficial&amp;client=firefox-a">ELisp manual</a>&#8221; into Google can customize Emacs to their taste.</p>
<blockquote><p><strong>Irony alert:</strong> I typed &#8220;elisp manula&#8221; into Google on my first try.</p></blockquote>
<p><strong>Automatic indentation:</strong> Yeah, I know, every editor in the history of the world does automatic indentation. However, Emacs&#8217; authors primarily write Lisp code, so they paid close attention to how Emacs interacts with Lisp. I rarely see Emacs do the Wrong Thing. When it does, I&#8217;m usually writing awful code.</p>
<p><strong>SLIME:</strong> The <strong>S</strong>uperior <strong>L</strong>isp <strong>I</strong>nteraction <strong>M</strong>ode for <strong>E</strong>macs. In order to effectively code in Lisp, you need an editor mode that will interact with a Lisp implementation. Typing the code directly into a command line is nice for small experiments, but is painful for anything more than 30 lines of code (for me at least.. your pain tolerance may vary).<br />
<a name="slime"></a></p>
<h3>SLIME</h3>
<p>SLIME combines the editing power of Emacs with the interactive nature of Lisp.</p>
<p>If you&#8217;ve ever used Python&#8217;s command-line prompt, you&#8217;re aware that it&#8217;s painful to use for any amount of time. The editing capability is limited to that of your console, and when you exit Python, all of your code disappears!</p>
<p>SBCL (and all other Lisp implementations) is no different. If this were the only option for working with Lisp, nobody would. You must work with the REPL from within a real editor. You COULD write code in an external file and load it into SBCL from the command line, but that removes the interactive nature of Lisp.</p>
<p>Enter SLIME (Superir Lisp Interactive Mode for Emacs). SLIME spawns a new Lisp process and acts as the liaison between you and the process.</p>
<p>To start the whole process, just type &#8220;<code>M-x slime</code>&#8220;, and a new REPL buffer opens. Any Lisp command can be entered into this buffer, and you get all of the editing commands from Emacs. <code>slime-mode</code> begins in all of your open Lisp buffers, which gives you code completion and function argument hints for all compiled functions.</p>
<p>If <code>slime-mode</code> is enabled in a Lisp buffer, you can compile the file with &#8220;<code>C-c C-k</code>&#8220;, or compile a single function with &#8220;<code>C-c C-c</code>&#8221; (it looks to me like dependencies are propagated correctly). SLIME knows what capability your Lisp has, so it can take advantage of implementation-specific capabilities like debugging.</p>
<p><strong>Installing SLIME</strong></p>
<p>SLIME is installed the same way as any other Emacs plugin:</p>
<ul>
<li>Download and extract.</li>
<li>Add the load path to your .emacs file</li>
<li>Add any other code snippets required.</li>
</ul>
<p>In this case, you&#8217;re going to be adding the following code to your .emacs file (open using &#8220;<code>M-x M-f ~/.emacs</code>&#8220;):</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>add-to-<span style="color: #b1b100;">list</span> 'load-path <span style="color: #ff0000;">&quot;&lt;em&gt;/the/path/to/slime&lt;/em&gt;&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>require 'slime<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>add-hook 'lisp-mode-hook <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>slime-mode t<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>add-hook 'inferior-lisp-mode-hook <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>inferior-slime-mode t<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> inferior-lisp-program <span style="color: #ff0000;">&quot;sbcl&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>I haven&#8217;t tested it on Windows, so I&#8217;m not sure if there are any extra gotchas. If you find that you&#8217;re running into undefined function errors, one possible workaround is to install Cygwin and add C:\cygwin\bin and c:\cygwin\usr\bin to your <code>%PATH%</code> environment variable. Some people run away in horror at the idea, so if you don&#8217;t like it, you&#8217;re on your own.<br />
<a name="asdf-install"></a></p>
<h3>asdf-install</h3>
<p>One thing that you should research is <a href="http://www.cliki.net/ASDF-Install">asdf-install</a>, a package manager for Lisp. If you find that you need a package that is found on cliki.net, you can download it with one step:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (require 'asdf-install) ; Not the one step.</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ASDF-INSTALL&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (asdf-install:install 'postmodern) ; The one step.</span>
<span style="color: #b1b100;">NIL</span></pre></div></div>

<p>The above installs <a href="http://common-lisp.net/project/postmodern/">Postmodern</a>, a simple <a href="http://www.postgresql.org/">Postgres</a> interface for Common Lisp. Once you&#8217;ve done this, you only need to load the package into your Lisp image. This is also accomplished using <code>asdf-install</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (asdf:oos 'asdf:load-op :postmodern)</span>
<span style="color: #b1b100;">NIL</span></pre></div></div>

<p>Using those 3 lines, you can now start to define database connections and operations on the database, without ever leaving Emacs!<br />
<a name="starting"></a></p>
<h2>Starting to Code</h2>
<p>I highly recommend that you find a real reference or a real tutorial and start using that to write code. However, if you&#8217;re looking for things to enter into the REPL, I&#8217;ll give you some overviews of the basics of Lisp.</p>
<h3>Arithmetic</h3>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (+ 2 2)</span>
<span style="color: #cc66cc;">4</span></pre></div></div>

<p>This adds 2 and 2. Notice that Lisp arithmetic (and all Lisp functions) have prefix notation: the function name always comes first. You can nest values like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (+ (+ 1 1) 1)</span>
<span style="color: #cc66cc;">3</span></pre></div></div>

<h3>Printing</h3>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (format t &quot;Hello, ~A~%&quot; &quot;Jacob&quot;)</span>
<span style="color: #ff0000;">&quot;Hello, Jacob&quot;</span>
<span style="color: #b1b100;">nil</span></pre></div></div>

<p>&#8216;<code>format</code>&#8216; is the function name. This is the Lisp-version of &#8216;<code>printf</code>&#8216;,</p>
<p>The second argument is the destination stream. You can give this &#8216;<code>t</code>&#8216; or &#8216;<code>nil</code>&#8216; for true or false, or the name of a stream.</p>
<p>When &#8216;<code>t</code>&#8216; is given, the value is printed to <code>Standard Output</code>. When &#8216;<code>nil</code>&#8216; is given, the value is returned as a string.</p>
<p>The third argument is the formatting string. &#8220;<code>~</code>&#8221; is the escape character. &#8220;<code>~A</code>&#8221; means that it takes an argument that follows (like <code>printf</code>), and <code>~%</code> is a newline.</p>
<p>There are other functions that you can use for printing, like &#8216;<code>princ</code>&#8216;. I usually stick with &#8216;<code>format</code>&#8216;, but there are different print semantics for different functions.</p>
<h3>Defining functions</h3>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (defun adding-function (arg1 arg2)</span>
    <span style="color: #ff0000;">&quot;Adds arg1 and arg2.&quot;</span>
    <span style="color: #66cc66;">&#40;</span>+ arg1 arg2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
ADDING-<span style="color: #b1b100;">FUNCTION</span>
&nbsp;
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (adding-function 1 2)</span>
<span style="color: #cc66cc;">3</span>
&nbsp;
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (+ (adding-function 1 2) 1)</span>
<span style="color: #cc66cc;">4</span></pre></div></div>

<p>Here we see a function definition and two function calls. Let&#8217;s examine the function definition:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (defun adding-function (arg1 arg2)</span></pre></div></div>

<p>This can be considered the prototype line. &#8216;adding-function&#8217; is the name of the function. It takes two mandatory parameters, &#8216;arg1&#8242; and &#8216;arg2&#8242;, both with no default values.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #ff0000;">&quot;Adds arg1 and arg2.&quot;</span></pre></div></div>

<p>This is the &#8220;documentation string.&#8221; A string may optionally be the first atom of a function, and it can be accessed from within Lisp using Lisp&#8217;s documentation reading abilities. Note that unlike wimpy strings in most languages, Lisp strings can contain newlines with no extra syntactical work.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>+ arg1 arg2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This is the executed statement within the function body. A Lisp function returns the return value of the last statement. Since this is the only statement, it returns the addition of <code>arg1</code> and <code>arg2</code>.</p>
<h3>Defining variables</h3>
<p>Defining a variable for the first time in SBCL:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defvar my-var<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; my-var is 'nil'.</span>
<span style="color: #66cc66;">&#40;</span>defvar my-var 'a-<span style="color: #b1b100;">value</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; my-var is 'a-value'.</span></pre></div></div>

<p>Notice the quote in <code>'a-value</code>. The single quotation mark indicates that the following expression is <strong>data, not code</strong>. Otherwise, it would try to look up the value of the variable <code>a-value</code>, which we may or may not have defined. Either way, it&#8217;s not what we want.</p>
<p>If a variable is already defined, you can use &#8216;<code>setf</code>&#8216;:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (setf my-var 3)</span>
<span style="color: #cc66cc;">3</span></pre></div></div>

<h3>Conditionals</h3>
<p>For statements that only execute when something is true, use &#8216;<code>when</code>&#8216;. For statements that only execute when something is false, use &#8216;<code>unless</code>&#8216;.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (when (= 1 1)</span>
    t<span style="color: #66cc66;">&#41;</span>
t
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (unless (= 1 1)</span>
    t<span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">nil</span></pre></div></div>

<p>For statements of the form &#8220;<code>if (true), do (x), otherwise do (y)</code>&#8220;, use &#8216;<code>if</code>&#8216;.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (if expression</span>
    x
    y<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>If &#8216;<code>expression</code>&#8216; doesn&#8217;t evaluate to &#8216;<code>nil</code>&#8216;, then &#8216;<code>x</code>&#8216; is executed and its return value is returned. Otherwise, &#8216;<code>y</code>&#8216; is executed and its return value is returned.</p>
<p>For statements involving a lot of if, else-if clauses in other languages, use cond:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>test1<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>expression1<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>test2<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>expression2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #808080; font-style: italic;">; ...</span>
      <span style="color: #66cc66;">&#40;</span>t <span style="color: #66cc66;">&#40;</span>default-expression<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h3>Iteration</h3>
<p>The two easiest ways to iterate are the macros &#8216;<code>dotimes</code>&#8216; and &#8216;<code>dolist</code>&#8216;.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (dotimes (i 3)</span>
    <span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;~A&quot;</span> i<span style="color: #66cc66;">&#41;</span>
012
<span style="color: #b1b100;">nil</span></pre></div></div>

<p>&#8216;<code>dotimes</code>&#8216; is exactly as it sounds: it executes a statement a number of times. You assign a variable (in our case, &#8216;<code>i</code>&#8216;) and tell it the number of times it shall execute, and the variable takes on the values of all of the integers in the range <code>[0, n)</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (dolist (i '(1 2 3))</span>
    <span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;~A&quot;</span> i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #cc66cc;">123</span>
<span style="color: #b1b100;">nil</span></pre></div></div>

<p>&#8216;<code>dolist</code>&#8216; iterates through a list much in the same way &#8216;<code>dotimes</code>&#8216; does.</p>
<h3>Reduce</h3>
<p>The idea of &#8216;<code>reduce</code>&#8216; is to iterate through a set, applying a function to each value that acts as an accumulator. For instance, we can do this with addition:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (reduce #'+ '(1 2 3))</span>
<span style="color: #cc66cc;">6</span></pre></div></div>

<h3>Lambdas</h3>
<p>If you want to define your own functions for &#8216;<code>reduce</code>&#8216; without writing formal functions, you can do them as a &#8216;<code>lambda</code>&#8216;. That&#8217;s just a fancy word for &#8216;function with no name&#8217;. It consists of the name, the argument list, and a function body to execute:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (lambda (x) (+ x 2))</span></pre></div></div>

<p>This defines an anonymous function that adds 2 to the input given. The only difference from &#8216;<code>reduce</code>&#8216; is that we need to provide two parameters: the accumulated value and the next value in the list.</p>
<p>You&#8217;re going to use this. A lot. Formally defining every function is not worth it. A lot of Lisp code ends up much cleaner with functional approaches.</p>
<p>To define our own addition function for &#8216;<code>reduce</code>&#8216;:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (reduce (lambda (x y) (+ x y)) '(1 2 3))</span>
<span style="color: #cc66cc;">6</span></pre></div></div>

<p><a name="other-features"></a></p>
<h2>Other features of Common Lisp</h2>
<ul>
<li>Macros: Creating code on-the-fly</li>
<li>CLOS: Common Lisp Object System. Lisp&#8217;s answer to Object Oriented programming.</li>
<li>Packages: This is how Lisp does namespacing.</li>
<li>More iteration constructs than you know what to do with.</li>
<li>&#8216;<code>mapcar</code>&#8216;: Apply a function to each element of a list</li>
<li>Hash tables</li>
<li>Arbitrary precision integers and numbers</li>
<li><code>cons</code>: &#8220;Construct&#8221; function. This is the function that builds lists.<code>car</code>: Returns the first element of a list.</li>
<li><code>cdr</code>: Returns the tail of a list (everything but the car).</li>
<li>Arbitrary binary manipulation</li>
<li>&#8216;<code>eval</code>&#8216;: Generate code from data at runtime.</li>
<li>File streams, input streams, output streams, stream redirections, oh my!</li>
<li>FFI: Foreign function interface. This lets you interface Lisp with C. You&#8217;ll probably need to do this if you want to add functionality to Common Lisp. It&#8217;s actually surprisingly easy to use.</li>
</ul>
<p><a name="resources"></a></p>
<h2>Resources</h2>
<p><strong>Books</strong></p>
<p><span style="text-decoration: underline;"><a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.com%2FANSI-Common-Prentice-Artificial-Intelligence%2Fdp%2F0133708756%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1215999519%26sr%3D8-1&amp;tag=jakvoyshom-20&amp;linkCode=ur2&amp;camp=1789&amp;creative=9325">ANSI Common Lisp</a></span> by Paul Graham. A great first Lisp book. It can be used as a textbook.</p>
<p><span style="text-decoration: underline;"><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%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1216001266%26sr%3D8-1&amp;tag=jakvoyshom-20&amp;linkCode=ur2&amp;camp=1789&amp;creative=9325">Paradigms of Artificial Intelligence Programming</a></span> by Peter Norvig. The best programming book I have ever read. It does have quite a bit on AI, but it also has extensive sections on advanced Lisp programming.</p>
<p><strong>Websites</strong></p>
<p><a href="http://www.sbcl.org/">SBCL:</a> The Lisp implementation I currently use.</p>
<p><a href="http://common-lisp.net/project/slime/">SLIME</a>: A Lisp interaction plugin for Emacs.</p>
<p><a href="http://vim.sourceforge.net/scripts/script.php?script_id=2219">Limp</a>: A SBCL plugin for Vim.</p>
<p><a href="http://cliki.net">cliki.net</a>: A general Lisp resource. The home of all projects remotely installable using asdf-install.</p>
<p><a href="http://cl-cookbook.sourceforge.net/">Common Lisp Cookbook</a>: An incomplete, yet still helpful, recipe book for Lisp code.</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=343&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2008/12/29/everything-you-need-to-get-started-with-common-lisp/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Evolving Genetic Algorithms in Lisp</title>
		<link>http://www.jakevoytko.com/blog/2008/12/16/evolving-genetic-algorithms-in-lisp/</link>
		<comments>http://www.jakevoytko.com/blog/2008/12/16/evolving-genetic-algorithms-in-lisp/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 04:00:40 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[asdf-install]]></category>
		<category><![CDATA[Genesis]]></category>
		<category><![CDATA[Genetic Algorithms]]></category>
		<category><![CDATA[Genetic Programming]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Mona Lisa]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=329</guid>
		<description><![CDATA[Or, now with 100% more Genetic Algorithm! I&#8217;ve been programming a lot recently (instead of blogging about programming!). I caught the genetic algorithm bug along with the rest of the internet, so I&#8217;m in the middle of writing a Lisp library to make it easier to develop genetic algorithms: Genesis [Github link]. I&#8217;ve made some [...]]]></description>
			<content:encoded><![CDATA[<p><em>Or, now with 100% more Genetic Algorithm!</em></p>
<p>I&#8217;ve been programming a lot recently (instead of blogging about programming!). I caught the genetic algorithm bug along with the rest of the internet, so I&#8217;m in the middle of writing a Lisp library to make it easier to develop genetic algorithms: Genesis [<a href="http://github.com/jakevoytko/genesis/">Github link</a>].</p>
<p>I&#8217;ve made some progress in the past few weeks!</p>
<p>The first version of my program was only a genetic programming library, and barely. It produced a rule set that could be evaluated as a function, but it wasn&#8217;t anything different than hill-climbing (a phrase I learned from snobby Reddit commenters). It was inefficient, it was unwieldy, but it produced great results when it ran long enough.</p>
<p><strong>I&#8217;ve added a population!</strong> An arbitrary number of critters evolve in parallel at the moment. Previously, the individual would just mutate. I also added a basic gene-sharing algorithm: random merging.</p>
<p><strong>It&#8217;s faster!</strong> In my first version, I used two &#8216;<code>eval</code>&#8216;s per rule per round. Now I use 1, and I will cut it down to 2 per rule evaluation (instead of per round) soon.</p>
<p><strong>It&#8217;s in a package!</strong> I finally decided to <a href="http://www.jakevoytko.com/blog/2008/09/15/buddy-can-you-spare-a-namespace/">follow my own advice</a> and namespace everything properly. The package layout still needs work, as I&#8217;ve been too busy coding to figure out the ASDF system for installing packages.</p>
<p>The package system in Lisp is a lot more flexible than most module systems I&#8217;ve ever used, so I&#8217;ve been reading a few other popular Lisp packages to see how they are organized before I jump off the deep end without my swimmies.</p>
<p><strong>I started to add tests!</strong> Some of the functions are very fundamental in nature, so I&#8217;ve started to add solid tests for them.</p>
<p><strong>I segregated the example!</strong> There are currently 2 different files: genesis.lisp and square-root-sample.lisp. square-root-sample shows the code necessary to produce a simple/stupid example: finding an algorithm to calculate square roots. As I&#8217;ve mentioned before, the results are very good:</p>
<p style="text-align: center;"><img class="aligncenter" src="http://www.jakevoytko.com/blog/wp-content/uploads/2008/12/sqrt-plot.gif" alt="Plot of f(x), abs(f(x)), error(x), and sqrt(x)" width="469" height="312" /></p>
<p>All you need to do in order to run this example is load genesis.lisp and square-root-sample.lisp and call the following:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>square-root-sample <span style="color: #66cc66;">&lt;</span>em<span style="color: #66cc66;">&gt;</span>generations population-size<span style="color: #66cc66;">&lt;</span>/em<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>After it has finished (run a small number of generations to get a sense for the time it takes), you can call the following to get the best answer so far:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>funcall-best *CURRENT-POPULATION* #'sample-fitness-<span style="color: #b1b100;">function</span> <span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h2>Work Needed</h2>
<p>A recent popular example of genetic programming is the <a href="http://rogeralsing.com/2008/12/07/genetic-programming-evolution-of-mona-lisa/">production of the Mona Lisa using random polygons.</a> My program should <em>almost</em> be able to handle this. I don&#8217;t allow for a &#8220;modify rule&#8221; function at the moment, only a &#8220;new rule&#8221; function. It would probably work, but convergence would be even slower than the example given.</p>
<p>I&#8217;m also not allowing for one of the most powerful methods of genetic algorithm development: binary serialization. Imagine: if you can represent your whole algorithm as a binary string, then crossover, reproduction, and mutation are all made trivial. I am instead using this idea for lists, which is convenient in Lisp, but I feel it lacks some of the punch.</p>
<h2>On The Horizon</h2>
<p>Of course, this is still in the toy phase. I still have a pretty substantial <a href="http://github.com/jakevoytko/genesis/tree/master/todo.org">to-do list</a>, but here are some of the high-notes:</p>
<ul>
<li>Finish writing tests for core functions.</li>
<li>Multiple populations, including cross-breeding between populations.</li>
<li>Add multithreading.</li>
<li>Give the user much greater control over (genetic-algorithm) by allowing extra key arguments.</li>
<li>Self-awareness: Gather statistics on which alterations produce better improvements, and make beneficial changes more likely to occur.</li>
</ul>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=329&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2008/12/16/evolving-genetic-algorithms-in-lisp/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Prototyping Genetic Algorithms in Lisp</title>
		<link>http://www.jakevoytko.com/blog/2008/12/04/prototyping-genetic-algorithms-in-lisp/</link>
		<comments>http://www.jakevoytko.com/blog/2008/12/04/prototyping-genetic-algorithms-in-lisp/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 12:00:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[asdf-install]]></category>
		<category><![CDATA[Genetic Algorithm]]></category>
		<category><![CDATA[genetic algorithm design]]></category>
		<category><![CDATA[Git]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Sqrt]]></category>
		<category><![CDATA[Square root]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=320</guid>
		<description><![CDATA[Source code for Genesis is here until I get asdf-install going. What are Genetic Algorithms? Genetic algorithms are one of the best things that computer science has produced. Rather than figuring out a good algorithm yourself, you let your random number generator find one while you sleep! Well, after an up-front design cost to generate [...]]]></description>
			<content:encoded><![CDATA[<p><em>Source code for Genesis is <a href="http://github.com/jakevoytko/genesis/tree/master">here</a> until I get asdf-install going.<br />
</em></p>
<h2>What are Genetic Algorithms?</h2>
<p>Genetic algorithms are one of the best things that computer science has produced. Rather than figuring out a good algorithm yourself, you let your random number generator find one while you sleep! Well, after an up-front design cost to generate proper random functions, and evaluating them, and determining how you&#8217;ll evolve them, and how long they should run&#8230;</p>
<p>I&#8217;ve long been interested in genetic algorithms, and now that I&#8217;ve learned my first language with an eval &#8212; Lisp &#8212; I&#8217;m starting to make a library. I spent the past few days prototyping and testing my code,<br />
and I got some encouraging initial results: I got a great almost-linear approximation to my simple test problem (finding square roots) using only addition, subtraction, multiplication, and division (see below).</p>
<p>I know, square roots. What a scorcher. But real people are making real solutions to real problems using genetic algorithms! NASA engineers produced an antenna. They determined acceptable design parameters for the antenna, figured out how to evaluate new designs, and let randomness determine the rest.</p>
<p>The result is described <a href="http://ti.arc.nasa.gov/projects/esg/research/antenna.htm">here</a>.</p>
<h2>My Genetic Algorithm Plans</h2>
<p>I want to produce a Lisp package over the next few weeks (since I should have a boatload of freetime around Christmas) that makes it easy to define new genetic algorithms. I&#8217;m going to do it in a few stages, and blog about intermediate results if and when they are interesting.</p>
<p>This will also give internet denizens plenty of opportunity to criticize my awful Lisp code! I am a nublet when it comes to Lisp, and I have no illusions about the quality of the code I&#8217;m writing.</p>
<p>This week: prototyping. I picked an easy problem to work out the basics of genetic algorithms. My problem? Finding square roots between 0 and 99, inclusive, with just one rule set.</p>
<blockquote><p><strong>Disclaimer</strong>: This is potentially the stupidest use of genetic programming known to man. Attempting to regularly solve numeric problems with genetic algorithms can lead to blindness, irrational exuberance towards Republican politics, and death.</p></blockquote>
<p>To give you a sense of the data it ingests, I present the main interface:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> genetic-algorithm <span style="color: #66cc66;">&#40;</span>generations starting-rules rule-fun
                          fitness-fun<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Runs 'generations' number of generations. The rules are initially
  set to to 'starting-rules', the rule generating function is
  'rule-fun', and the evaluation function is 'fitness-fun'&quot;</span>
  <span style="color: #66cc66;">&#40;</span>prep-rules starting-rules<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">dotimes</span> <span style="color: #66cc66;">&#40;</span>gen-num generations<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>run-generation rule-fun fitness-fun<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Here is the invocation I used for my square root problem:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>genetic-algorithm <span style="color: #cc66cc;">32000</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>/ num <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>+ num <span style="color: #66cc66;">.</span>2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                   #'sample-rule-fun #'sample-fitness-fun<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The typical work for defining genetic algorithms is split into a few stages:</p>
<p>1) Problem definition: What do I even want to solve?<br />
2) Evaluation function: How far am I from right?<br />
3) Picking components: How can I build my algorithms randomly?</p>
<p><strong>Problem Definition</strong></p>
<p>Find the square root of an integer, n, in the range [0, 99].</p>
<p><strong>Evaluation Function</strong></p>
<p>Sum of the squared error of all of the integers in the problem statement.</p>
<p>Interestingly, we can predict the quality and form of the results: since we are minimizing the sum of the squared errors, the result should mimic a least-squared approach. Since I&#8217;m only using basic arithmetic, it probably won&#8217;t be a very good one (linear at best..).</p>
<h2>Prototype Implementation</h2>
<p><em>Warning: There be dragons in the parens ahead.</em></p>
<p>The rules are stored as a list of lists:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defvar *RULES* <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>We also store the possible operators:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defvar *sample-nodes* #<span style="color: #66cc66;">&#40;</span>leaf<span style="color: #66cc66;"> + </span>leaf<span style="color: #66cc66;"> - </span>leaf<span style="color: #66cc66;"> / </span>leaf<span style="color: #66cc66;"> * </span>leaf<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Woah, what&#8217;s with all of the &#8220;leaf&#8221;s? Well, a balanced binary tree has about as many nodes as leafs (I&#8217;m generalizing&#8230;), so I started at leaf/operator parity and moved up until I stopped getting stack overflows when generating new functions, which happened at one <img src='http://www.jakevoytko.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>How do the leafs get translated into values? First, they are picked from an array of random values:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defvar *sample-values*
    #<span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">1</span> num num randomnum randomfrac<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> sample-generate-leaf <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Picks a random element from 'list' and translates it into the proper atom
  or list.&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>elt <span style="color: #66cc66;">&#40;</span>random-array-element <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">case</span> elt
         <span style="color: #66cc66;">&#40;</span>randomnum <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> <span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#40;</span>randomfrac
          <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>val <span style="color: #66cc66;">&#40;</span>ignore-errors <span style="color: #66cc66;">&#40;</span>/ <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> <span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> <span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null</span> val<span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> val <span style="color: #66cc66;">&#40;</span>/ <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            val<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#40;</span>t elt<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>These are pretty self descriptive (<em>num</em> is the input number, and it is included twice because I got better results <img src='http://www.jakevoytko.com/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> ). &#8216;<em>randomnum</em>&#8216; is a random integer in [0, 1023], and &#8216;<em>randomfrac</em>&#8216; is a fraction whose<br />
coefficients are in [0, 1023].</p>
<p>So how is all of this put together?</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> sample-rule-fun <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Example random rule generator. Generates a random arithmetical
  expression.&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>elt <span style="color: #66cc66;">&#40;</span>random-array-element *sample-nodes*<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">case</span> elt
      <span style="color: #66cc66;">&#40;</span>leaf <span style="color: #66cc66;">&#40;</span>sample-generate-leaf *sample-values*<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>t
       <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">append</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> elt<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>at-least-once #'sample-rule-fun<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> sample-generate-leaf <span style="color: #66cc66;">&#40;</span>arr<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Picks a random element from 'arr' and translates it into the proper atom
  or list.&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>elt <span style="color: #66cc66;">&#40;</span>random-array-element arr<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">case</span> elt
    <span style="color: #66cc66;">&#40;</span>randomnum <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> <span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>randomfrac
     <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>val <span style="color: #66cc66;">&#40;</span>ignore-errors <span style="color: #66cc66;">&#40;</span>/ <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> <span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> <span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null</span> val<span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> val <span style="color: #66cc66;">&#40;</span>/ <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">;; avoid divide by zero</span>
       val<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>t elt<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This is also fairly straightforward: if you pick an operator, generate a new list, if you pick a leaf, generate a new value.</p>
<h2>Results</h2>
<h3>Square Root Function Test</h3>
<p>I ran it for about 18 hours (overnight and after work), and when I came back, the results were nothing short of amazing:</p>
<p><a href="http://www.jakevoytko.com/blog/wp-content/uploads/2008/12/sqrt-plot.gif"><img class="alignnone size-full wp-image-322" title="sqrt-plot" src="http://www.jakevoytko.com/blog/wp-content/uploads/2008/12/sqrt-plot.gif" alt="" width="500" height="332" /></a></p>
<p>I included a range of values outside of the initial zone because the results are better than I could have hoped for.</p>
<p>I saved the data, and then was trying to save the rules (there were 18 in all), and then I lost them from my first Emacs freeze. C&#8217;est la vie. However, my initial guess that the data would fit as a least-squares appears to be a better guess than I ever could have hoped. I want to add arbitrary polynomials, and I hope to see a higher-order best-squares fit of the data plot, with much less error.</p>
<h3>Implementation Time</h3>
<p>The first implementation was done in a 4-hour chunk of one evening (during which I played X-Moto for at least an hour&#8230;), which I find impressive. I probably would have spent at least 3 times that on the prototype in C++. This problem is made extremely because of several of Lisp&#8217;s features:</p>
<p><strong>eval</strong>: My method is based on runtime list generation, and eval-ing it as a lambda.</p>
<p><strong>Insanely simple Lisp syntax</strong>: The same syntax that defines lists also defines their programs? This reduces random function generation to a random list generation problem, which is almost trivial.</p>
<p><strong>REPL:</strong> Lisp is a REPL (<strong>r</strong>ead-<strong>e</strong>val-<strong>p</strong>rint <strong>l</strong>oop) language, meaning that it has an interactive prompt. Combining this with Emacs&#8217; multiple buffer capabilities, and it&#8217;s super easy to make changes and reload.</p>
<h2>TODO</h2>
<p>It&#8217;s never finished!</p>
<h3>Rules</h3>
<p>Allow more than one rule set, define the maximum number of rules per set, define rules as arrays for better lookup, etc.</p>
<h3>Better use of eval</h3>
<p>I eval the rules every time I need them, as opposed to the once I should generate them. I will probably see a significant speedup when I do this.</p>
<h3>Define a package</h3>
<p>Namespacing everything has obvious benefits.</p>
<h3>Cross-breed rules</h3>
<p>Allow true genetic properties by copying subsets of rules into other rules.</p>
<h3>Better rule trimming</h3>
<p>My current program produces some stupid rules: such as f(x)=x. In fact, my &#8220;amazing results&#8221; had 4 rules that evaluated a number to itself out of a total if 18 rules.</p>
<h3>Allowing the user to define rule inputs.</h3>
<h3>Putting the package in an easy-to-use location</h3>
<p>Stay tuned for more!</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=320&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2008/12/04/prototyping-genetic-algorithms-in-lisp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Inertia and Large Code Bases</title>
		<link>http://www.jakevoytko.com/blog/2008/11/17/inertia-and-large-code-bases/</link>
		<comments>http://www.jakevoytko.com/blog/2008/11/17/inertia-and-large-code-bases/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 04:00:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[bad code]]></category>
		<category><![CDATA[big code]]></category>
		<category><![CDATA[code base]]></category>
		<category><![CDATA[Code Inertia]]></category>
		<category><![CDATA[codebase]]></category>
		<category><![CDATA[good code]]></category>
		<category><![CDATA[large codebases]]></category>
		<category><![CDATA[MFC]]></category>
		<category><![CDATA[windows.h]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=235</guid>
		<description><![CDATA[Or: Long live the king. What is &#8220;Code Inertia&#8221;? I&#8217;ve been interested in &#8220;code inertia&#8221; for a few years now. It should sound familiar to anyone with a basic understanding of physics: code with a lot of inertia is very resistant to change. You&#8217;d either have to be crazy, uninformed, or very well paid to [...]]]></description>
			<content:encoded><![CDATA[<p><em>Or: Long live the king.</em></p>
<h2>What is &#8220;Code Inertia&#8221;?</h2>
<p>I&#8217;ve been interested in &#8220;code inertia&#8221; for a few years now. It should sound familiar to anyone with a basic understanding of physics: code with a lot of inertia is very resistant to change. You&#8217;d either have to be crazy, uninformed, or very well paid to try to shake the way the system is organized.</p>
<p>I met my first gigantic and arcane system at my college internship. It was a Java-based web platform with 8 years of cruft built on top. There was no documentation for even creating a development environment, and it had a complex and lengthy build system. It was a mountain of pain ready to strike. There was no way in hell that the system could be considered &#8220;beginner friendly,&#8221; and there was no clear path for gaining knowledge of the system.</p>
<p>It was so complex that only a handful of individuals had the Keys to the Kingdom. Everybody else used the tools produced by the Chosen Ones and went on their merry way. I tried to become a Holder of Knowledge, but the system proved too great for me, the college intern.</p>
<p>Why do I mention it? Because the whole system had a gigantic amount of inertia! There was so much code built on top of the mountain, that nothing could be changed. You couldn&#8217;t change the build system, because then you&#8217;d have to alter a mountain of configurations for hundreds of sites. Despite the amount of code, very little of it was actually reusable, and there was no central repository of knowledge for code reuse.</p>
<p>The most helpful changes became the ones that were the most impossible to make. It was a big, suffocating force, and the company eventually abandoned it for a shiny new toy called <a href="http://www.microsoft.com/sharepoint/default.mspx">Sharepoint</a>. The old way of doing things simply wasn&#8217;t sustainable. Whether they learned their lesson or not I&#8217;ll never know, because I have since left the company and landed a real programming job.</p>
<p>Because of the monstrosity I mentioned, I appreciate my current job&#8217;s gigantic, fairly documented, and accessible code base quite a bit. It&#8217;s not close to perfect, but it does a lot of real world tasks <em>very</em> well, and beginners can use a good chunk of it pretty quickly.</p>
<h2>Windows.h</h2>
<p>If you want <em>THE BEST</em> example of inertia, just look at &#8220;<a href="http://en.wikipedia.org/wiki/Windows.h">windows.h</a>&#8220;. It might have the greatest code inertia of any file not defined in a language standard. It is the basis for <strong>every</strong> Win32 application. Could it be modified? Potentially, but only by appending to the standard. The file itself could never be modified without wasting an impressive number of programmer hours.</p>
<p>This is unfortunate, because it has classic design errors. Windows programming still isn&#8217;t very &#8220;easy&#8221; with it, and you have to watch #include order and know what macros it defines. Yuck. However, none of these things can really be fixed, because even innocent-seeming changes would probably break builds for arcane reasons. Obviously, it should be replaced.</p>
<p>The .NET framework is the first Microsoft offering that has the potential to deliver a knockout blow to the old way of Win32 programming, since MFC wasn&#8217;t up to the task. Some people reject .NET for the runtime size, and others reject it because it&#8217;s Microsoft-specific, but it&#8217;s a lot easier to work with than Win32 for your run-of-the-mill windowed application.</p>
<h2>Reasons for Inertia</h2>
<p>Code can pick up inertia for different reasons:</p>
<p><strong>So many files depend on this code that changes would be prohibitively expensive.</strong></p>
<p>This is (in my personal experience) the most common kind of code inertia, as it&#8217;s hard to avoid even for well-designed systems. You design a few nice classes, build useful utilities around them, and use the library in 162 programs.</p>
<p>You could easily change the low-level code underneath the utility abstractions. Unfortunately, the top-level interface has picked up quite a bit of inertia. If you want your whole system to be consistent, you&#8217;ll need to make quite a few changes. Otherwise, you can do sneaky things that are good enough for most situations, like deprecate the interface while maintaining a backwards-compatibility layer.</p>
<p><strong>The code itself is so large that changes are prohibitively expensive. </strong></p>
<p><strong></strong>This is related to the previous point, but sans dependences. That finance application you were writing for that coal business? Well, they suddenly decided that they wanted to enter the asphalt business, and want the application to reflect that.</p>
<p><strong>The code is so complex and ill-written that changing it is likely to break everything</strong>.</p>
<p>This is likely to have been written &#8220;by somebody else.&#8221; Everybody tacks their changes on to the system instead of modifying how it works, and hope it falls apart when the next guy uses it.</p>
<p><strong>That guy who knew how everything works quit.</strong></p>
<p>&#8220;Fricksticks, Frank&#8217;s gone. Now nobody knows how the the whole Foo suite works. Frick frick frick.&#8221;</p>
<h2>Predictive Power of Inertia</h2>
<p>Let&#8217;s say that our company has two million lines of code that works wonders with widgets, and our CEO suddenly sees a golden opportunity in the gadget market.</p>
<p>We have a big problem: our code wasn&#8217;t designed with gadgets in mind! You might find some core libraries are abstracted enough to work with gadgets. The rest of it explicitly assumes widgets.</p>
<p>What&#8217;s going to happen? As nice as refactoring the entire code base would be&#8211; and I think I threw up a little inside thinking about it&#8211; it&#8217;s obvious that gadget code is going to be added <em>on top of</em> the widget code base, instead of working with it. That&#8217;s a prediction that anybody can make.</p>
<p>Adding the basic gadget functionality you need NOW will be much less effort than changing your code to handle widgets, gadgets, gizmos, and pandas. It&#8217;s the right way to go from a business perspective (less time, less expense, same results), but it&#8217;s unsatisfying from a technical perspective, and probably misses some nice opportunities for generalization.</p>
<p>Some people will point out that there are lost opportunities for the future. You may want to also be able to work with thingamajigs and whatchamacallits in the future. If you tack on gadget code, you&#8217;re going to be tacking on code for everything else.</p>
<p>It&#8217;s important to realize that there&#8217;s never a free lunch in any of this regard. Even if all of your library code has perfect abstractions, you&#8217;ll still need to build up some extra gadget-specific code that reads and writes them, interfaces with your existing utilities, etc. It&#8217;s all just a matter of ease of modification.</p>
<h2>Law of Large Code Bases</h2>
<blockquote><p>All code bases cross a size threshold where addition is less costly than modification.</p></blockquote>
<p>When you need new functionality in your code, you have two options:</p>
<ol>
<li>Modify the existing code to solve your problem.</li>
<li>Creating new code to solve your problem.</li>
</ol>
<p>Sometimes, Option 1 is unacceptable, and it&#8217;s unfortunate. In a perfect world, we would write code once, and it would work for all of our situations. In a less perfect world, we should always be able to modify our existing code to best be generalized. Unfortunately, software is a business, and rewriting/heavy modification isn&#8217;t always in the cards.</p>
<p>Option two is a short term solution, but an interesting corollary becomes immediately obvious:</p>
<blockquote><p>When a code base has crossed the threshold mentioned in the Law of Large Code Bases, adding additional adds additional inertia to the code.</p></blockquote>
<p>When code crosses this boundary, you can consider it a marked man until it is changed for the general case.</p>
<h2>Can We Plan Around Code Inertia?</h2>
<p>The planning phase of the project is obviously the best chance to take code inertia into account. Unfortunately, there&#8217;s not usually a whole lot that you can do. You&#8217;re not always under control of the scope of your project, and you don&#8217;t know what problems you&#8217;re going to need to solve in 2 years. That&#8217;s fine.</p>
<p>Trying to find flexible solutions is a great way to start. Just take a look at Steve Yegge&#8217;s post on <a href="http://steve-yegge.blogspot.com/2008/10/universal-design-pattern.html">Property Lists</a> as a programming paradigm. You can express a tremendous number of solutions in terms of<br />
property lists, and they&#8217;re very flexible if you use them right.</p>
<p>Another successful strategy is the One Thing Well philosophy. Unix systems come with a broad variety of tools that &#8220;do one thing, but do it well.&#8221; Chaining the tools together is easy, and any system programming problem can easily be solved in terms of these tools.</p>
<p>In writing this post, I noticed that deprecation/change seems to be the most successful way of supplanting old methodologies. Python 3000 is a great example of this. So is the module system of the Linux 2.6 kernel. It turns out that it&#8217;s really easy to change code that&#8217;s underneath a layer of abstraction, but you can&#8217;t change code that relies on the abstraction.</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=235&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2008/11/17/inertia-and-large-code-bases/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Taking Advantage of (Read: Working Around) Syntax</title>
		<link>http://www.jakevoytko.com/blog/2008/11/03/taking-advantage-of-read-working-around-syntax/</link>
		<comments>http://www.jakevoytko.com/blog/2008/11/03/taking-advantage-of-read-working-around-syntax/#comments</comments>
		<pubDate>Mon, 03 Nov 2008 04:00:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Abelman]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ assignment]]></category>
		<category><![CDATA[C++ errors]]></category>
		<category><![CDATA[C++ workaround]]></category>
		<category><![CDATA[Sussman]]></category>
		<category><![CDATA[TODO]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=185</guid>
		<description><![CDATA[Syntax Smells &#8220;Programs must be written for people to read, and only incidentally for machines to execute.&#8221; ~Abelson and Sussman, SICP If 8 years of programming has taught me anything, it&#8217;s that programmers need to give themselves every advantage imaginable. I&#8217;m not disparaging other programmers.  I say this as someone who has been humbled on [...]]]></description>
			<content:encoded><![CDATA[<h2>Syntax Smells</h2>
<blockquote><p>&#8220;Programs must be written for people to read, and only incidentally for machines to execute.&#8221;</p>
<p>~Abelson and Sussman, SICP</p></blockquote>
<p>If 8 years of programming has taught me anything, it&#8217;s that programmers need to give themselves every advantage imaginable. I&#8217;m not disparaging other programmers.  I say this as someone who has been humbled on the altar of bugs many times.</p>
<p>The fact that my first language (C++) is one of the most complex languages probably didn&#8217;t help. C++ isn&#8217;t my favorite language, but I always find myself coming home to it, whether for school assignments, or more recently for money. Plus, it ranks higher than Java in my book. Yuck!</p>
<p>Most programming languages have fundamental shortcomings that prevent them from being very human-friendly. Like what? Like nested parentheses and braces and brackets. Fortunately, along with programming languages evolved code editors that do great things with indentation, coloring, and matching brace highlighting. The actual problem isn&#8217;t fixed (a colored, indented C++ program is still the exact same thing as a non-colored, non-indented C++ program), but the tools exist to make it a non-issue.</p>
<p><strong>There is a wrong way to do it, and a right way to do it, but none of it is enforced by the compiler.</strong></p>
<h2>Assignment Within if(){</h2>
<p>When I was 14 and 15 and hadn&#8217;t been using the language for very long, I made a whole litany of embarrassing mistakes. For instance, I would spend an hour trying to figure out why the following wasn&#8217;t doing what I expected:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"> <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #0000dd;">10</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><span style="color: #008000;">&#123;</span>
     do_something<span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
 <span style="color: #008000;">&#125;</span></pre></div></div>

<p>Fortunately, I stopped falling for it after a few times. However, I couldn&#8217;t seem to shake the following error:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"> <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>my_ptr <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span> <span style="color: #666666;">// Wrong, but no compile error!</span>
     do_something<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
 <span style="color: #008000;">&#125;</span></pre></div></div>

<p>To the rookie, this looks correct at a quick glance. Even worse, it compiles without warning. In fact, this was one of the evil no-nos that Java outlawed altogether, even though it has legitimate uses.</p>
<p>No matter how many times I swore I&#8217;d look closer, 2 weeks later I&#8217;d find this same bug after 30 minutes of frustrated debugging.</p>
<p>Eventually, I eliminated a large subset of this error by writing the equality test differently:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"> <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span> <span style="color: #000080;">=</span> my_ptr<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span> <span style="color: #666666;">// compile error!</span>
     do_something<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
 <span style="color: #008000;">&#125;</span></pre></div></div>

<p>This caused a comparatively small boost in my productivity. However, this helped, and it required no effort. <strong>Every little bit counts.</strong> Interestingly, this kicked me out of my groove: I started making this error a whole lot less frequently because I was thinking more about my equalities. Unless projects have strict style guides, I still code it this way.</p>
<p>For being a simple style change, it comes with a few benefits:</p>
<ol>
<li>Anybody can understand this code.</li>
<li>When you screw up, it doesn&#8217;t compile.</li>
</ol>
<h2><strong>//TODO:</strong> Lists</h2>
<p>Some code generators, like Visual Studio 2003, fill generated functions with comments:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"> <span style="color: #0000ff;">void</span> importantFoo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
 <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// TODO: Implement this!</span>
 <span style="color: #008000;">&#125;</span></pre></div></div>

<p>For the authors of the code generator, this suits its purpose: informing the programmer that they have some work to do while generating compilable code.</p>
<p>Believe it or not, some code generators improve upon this. For instance, I&#8217;ve seen some code generators taking an exceptional approach:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"> <span style="color: #0000ff;">void</span> importantFoo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
     <span style="color: #0000ff;">throw</span> std<span style="color: #008080;">::</span><span style="color: #007788;">runtime_error</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;TODO: Implement function importantFoo()!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
 <span style="color: #008000;">&#125;</span></pre></div></div>

<p>This comes with benefits: unless the programmer is a complete ninny, this code will be executed, and the nascent program will be prematurely snuffed from this Earth.</p>
<p>If this doesn&#8217;t cause the programmer to get off his lazy rear end and code, then what will?</p>
<p>The // TODO! statement is certainly not unique to code generators: it <a href="http://www.google.com/codesearch?q=%2F%2F+TODO&amp;hl=en&amp;btnG=Search+Code">seems to be pretty popular</a> amongst programmers, too. I am no exception them, too.</p>
<p>For a while, I took the comment approach to the TODO:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"> <span style="color: #0000ff;">void</span> my_foo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
     <span style="color: #666666;">// TODO: Implement me! I am a yummy pizza!</span>
 <span style="color: #008000;">&#125;</span></pre></div></div>

<p>I&#8217;ve always felt a little dirty writing these. Realistically, your brain&#8217;s stack can only hold 5-9 items, so I can see an argument for temporarily using them. However, all you&#8217;re doing is passing the buck to an older, wiser future version of yourself in a brave new world with flying cars and butler monkeys.</p>
<p>It&#8217;s obvious that if you had flying cars and butler monkeys, the LAST thing that you would want to do would be to implement a // TODO statement left by a barbaric past self.</p>
<p>It&#8217;s my opinion that if you HAVE to write one, you should resolve the issue ASAP. For instance, before you  compile or interpret or &#8220;(read)&#8221; or whatever your language uses.</p>
<p>But recently, I found an even better TODO method for compiled languages:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"> <span style="color: #0000ff;">void</span> my_foo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
     TODO<span style="color: #008080;">:</span> Implement me<span style="color: #000040;">!</span> I am a yummy pizza<span style="color: #000040;">!</span>
 <span style="color: #008000;">&#125;</span></pre></div></div>

<p>In C++, this generates about a trillion compiler errors (more or less). Compiler errors, for whatever reason, put me on a mission to fix a problem, so this is is an easy and helpful motivator.</p>
<p>I call these &#8220;Quit your bitchin&#8217;&#8221; TODO lists: Quit your bitchin&#8217; and implement me! You have to do it sooner or later!</p>
<p>It is important to note that you should NEVER do this in Python. Python is expressive enough that this syntax will likely cause the International Space Station to fall to earth while buttering bread on the wrong side. &lt;/bad_joke&gt;</p>
<h2>Conclusion</h2>
<p>Modern programming languages are designed  with human readability in mind, but there isn&#8217;t a &#8220;perfect language&#8221; in this regard. Best practices develop around programming languages, and is imperative that newbies find out what these are ASAP, because they are only hurting themselves if they do things their own way.</p>
<p>In addition to using best practices, sometimes you can make things easier for yourself with little effort.</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=185&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2008/11/03/taking-advantage-of-read-working-around-syntax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Preparing Yourself, a Student, for a Programming Job</title>
		<link>http://www.jakevoytko.com/blog/2008/09/29/preparing-yourself-a-student-for-a-programming-job/</link>
		<comments>http://www.jakevoytko.com/blog/2008/09/29/preparing-yourself-a-student-for-a-programming-job/#comments</comments>
		<pubDate>Mon, 29 Sep 2008 04:00:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[LAPACK]]></category>
		<category><![CDATA[Looking for job]]></category>
		<category><![CDATA[modify code]]></category>
		<category><![CDATA[MPZ]]></category>
		<category><![CDATA[Programmer]]></category>
		<category><![CDATA[Programming job]]></category>
		<category><![CDATA[read code]]></category>
		<category><![CDATA[Student]]></category>
		<category><![CDATA[Use code]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=165</guid>
		<description><![CDATA[Congratulations, student! You&#8217;ve made it through college, aced your interview, and now you are starting a job as a programmer! If you&#8217;re like me, you want to know what you can do before you start. For my previous 2 programming jobs, I asked my interviewer what I could do to better prepare myself for my [...]]]></description>
			<content:encoded><![CDATA[<p>Congratulations, student! You&#8217;ve made it through college, aced your interview, and now you are starting a job as a programmer!</p>
<p>If you&#8217;re like me, you want to know what you can do before you start. For my previous 2 programming jobs, I asked my interviewer what I could do to better prepare myself for my first day. They both laughed and said, &#8220;Just show up!&#8221; True story.</p>
<p>I ended up working on personal projects instead of preparing for the job. It didn&#8217;t really hurt or help in the long run, but I felt that the time could have been better spent.</p>
<p>If you <em>really</em> only have a few weeks before you start your job, don&#8217;t keep preparing for your interview. Don&#8217;t practice arcane language trivia questions and don&#8217;t try to solve programming puzzles (unless you do it as a hobby!). Avoid trivia in this stage. If your interviewer was competent, s/he already thinks that you can do the work. Diving back into Meyers, the Gang of Four, or Knuth might give you a trick or two, but in the long run it&#8217;ll all get lost in the noise.</p>
<p>Don&#8217;t get me wrong! Studying is <em>never</em> a bad move. I graduated 3 months ago and I still spend a lot of my time studying. It&#8217;s not the best short-term solution to getting you started in a programming job.</p>
<p>One of the things you should do (but not the most important) is to read domain and company-specific material. Academic journals, conference proceedings, blogs, company websites, and Econ books all fall into this category.</p>
<p>If you&#8217;re still in school, take a Microeconomics course. Trust me, it&#8217;s worth it. Look at the press releases on your company&#8217;s website. They&#8217;ll give you a hint at the focus of your job specifics.</p>
<p>Why should you waste your time with this crap? Odds are that your company has a few goals:</p>
<ol>
<li>To contribute to its particular field of expertise.</li>
<li>To do better than its competitors, for some value of better.</li>
<li>To turn a profit.</li>
</ol>
<p>It is certainly possible to make good programming/GUI interface/business decisions without knowing anything about your field, your competitors, or your business.</p>
<p>Is it easier with the insider knowledge? Probably. If you didn&#8217;t spend time thinking about your field in college, you should at least flip through an Econ textbook or a few trade journals.</p>
<p>This shouldn&#8217;t command the bulk of your time. At the end of the day, you won&#8217;t master your new field in the few weeks before your job starts. I don&#8217;t mean to undercut the importance of this step! You will learn your business sooner or later, and starting early will give you a big help on your first day.</p>
<p>What will REALLY help you prepare?</p>
<p><strong>Use, read, and modify code written by other programmers. A lot. </strong></p>
<p>Why? You&#8217;re going to spend most of your career as a programmer doing two things:</p>
<p>1) Reading and correcting what others have written.<br />
2) Building on top of what others have designed.</p>
<p>Likewise, other people will spend time correcting what you have written, and building on top of your designs.</p>
<p>Unless you have donated a significant amount of time to Open Source projects, s/he has minimal experience dealing with unfamiliar code. Maybe a few libraries here and there, but not much else.</p>
<p>Many of my friends and I were very active programmers in our spare time, but we spent our time designing our own code, not using the code of others. I <a href="http://www.jakevoytko.com/blog/2007/12/22/reading-comprehension-will-make-you-a-better-programmer/">started seeing the light</a> towards the end of my senior year, but the full implications never struck me until I entered the work world as a professional programmer.</p>
<p>Most of my school assignments were based around creating new code. Sometimes I didn&#8217;t even have to design it! Use the professor&#8217;s framework, fill in the blanks, get an A, and move on to something more interesting.</p>
<p>I landed a Summer of Code project, but the project was based around creating a helper for some <a href="http://www.boost.org/">Boost</a> libraries (for being a messy solution to an ill-defined problem under time constraint while working another full-time job, it ended up producing decent results). I learned how to use a few of the useful Boost libraries, learned I never wanted to use others ever again, and got a window into the OSS world. Unfortunately for me, my experience reading and modifying the code of others was minimal.</p>
<p>Some of my harder assignments were still reasonable projects to complete from scratch. I took a cryptography class, and my final project was to implement the quadratic sieve. I used GNU&#8217;s <a href="http://gmplib.org/">MPZ library</a>, and built the linear algebra/number theoretic functions from scratch. I just as easily could have used a library like <a href="http://math.nist.gov/lapack++/">LAPACK++</a>. In fact, it&#8217;s very likely that such a library would have been much faster than my personal implementation, and the project would have been finished sooner.</p>
<p>If it is so important, why weren&#8217;t you asked about using the code of others in your interview? It could be any number of reasons, really. Maybe the company&#8217;s code base is so massive that EVERYONE needs time to digest the structure. Maybe they haven&#8217;t hired a junior developer recently. Maybe the company wants to completely mold the habits of new developers, before they learn naughty practices.</p>
<p>Will you be OK without doing this? Yes. I started my job with only minimal experience with unfamiliar code, and I haven&#8217;t starved to death on the streets. Yet. However, I wish I had spent more time figuring out how to use different libraries.</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=165&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2008/09/29/preparing-yourself-a-student-for-a-programming-job/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Explaining Lisp to my Father</title>
		<link>http://www.jakevoytko.com/blog/2008/07/21/explaining-lisp-to-my-father/</link>
		<comments>http://www.jakevoytko.com/blog/2008/07/21/explaining-lisp-to-my-father/#comments</comments>
		<pubDate>Mon, 21 Jul 2008 04:00:55 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Misc]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[Quote]]></category>
		<category><![CDATA[Brevity]]></category>
		<category><![CDATA[Explaining Lisp]]></category>
		<category><![CDATA[Lambdas]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Lisp Macros]]></category>
		<category><![CDATA[Self-Modifying code]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=102</guid>
		<description><![CDATA[Anyone who read my post last week knows that I&#8217;ve been trying to learn Lisp in my free time. My Dad has also noticed, and he occasionally checks on my progress. A week or two ago, he threw me a curveball: &#8220;How&#8217;s it going? Do you understand it all yet?&#8221; &#8220;No. I&#8217;m making progress, though. [...]]]></description>
			<content:encoded><![CDATA[<p>Anyone who read my post last week knows that I&#8217;ve been trying to learn Lisp in my free time. My Dad has also noticed, and he occasionally checks on my progress.</p>
<p>A week or two ago, he threw me a curveball:</p>
<blockquote><p>&#8220;How&#8217;s it going? Do you understand it all yet?&#8221;</p>
<p>&#8220;No. I&#8217;m making progress, though. I&#8217;m probably not going to have this down for a while. It&#8217;s a completely different way of thinking from the programming I normally do.&#8221;</p>
<p>&#8220;How is it different?&#8221;</p></blockquote>
<p>I froze. He&#8217;s &#8220;Mr.-Fix-It,&#8221; and understands the rock quarry/asphault business inside and out. He&#8217;s very analytical, and it&#8217;s almost impossible to stump him with my favorite &#8220;Devil&#8217;s advocate&#8221; questions. However, aside from what I&#8217;ve told him, he just doesn&#8217;t know much about programming.</p>
<p>&#8220;Metaprogramming&#8221; and &#8220;first-class functions&#8221; wouldn&#8217;t have meant anything. &#8220;It lets you write programs faster&#8221; is a cop-out; he&#8217;s given me detailed explanations of his business. I wanted him to understand.</p>
<p>I was able to stammer out the fact that the language encourages breaking problems into smaller versions of the same problem (recursion), but that other programming languages also supported it.</p>
<p>I eventually told him that I hadn&#8217;t even thought about explaining the difference to a non-programmer.</p>
<p>&#8220;Well, if you&#8217;re ever able to come up with it, let me know.&#8221;</p>
<p>In college, I was a Computer Science tutor. I could explain most CS concepts that students saw in early C++ and Java courses to the layman on the street. However, I had <strong><span style="text-decoration: underline;"><em>nothing</em></span></strong> for explaining the difference between Lisp and C++ to him, which was a little scary. I spent some time dreaming up examples.</p>
<h2>Functions</h2>
<blockquote><p>All models are wrong, but some are useful.</p>
<p>~<a href="http://en.wikiquote.org/wiki/George_E._P._Box">George Box</a></p></blockquote>
<p>Abstract math notions are boring to most people, so I try to relate programming concepts to real-world items.</p>
<p>A fast-food restaurant is a great example of a function: An order and some money is input, and you get cheeseburgers, fries, a soda, and your change. Whether a person likes math or not, they know what a function is: you put something in, you get something out.</p>
<p>The idea is easily extensible to Object Oriented programming: all Burger Kings have orders, and you can place a specific order at an instance of a Burger King. A Burger King &#8220;is-a&#8221; fast food restaraunt.</p>
<p>I will use this metaphor to explain why Lisp is different from the programming I usually do (C++/Java/C#/Python).</p>
<h2>Lambdas</h2>
<p>Any restaraunt probably has a few different ways to order food: over the phone, at a drive-through window, at a counter, through a waiter, etc. However, there might be unofficial ways to order food: Bill Gates might write a letter requesting a buffet with a few hundreds slipped in as proof. Someone might tell the owner on his way to work that he&#8217;d like him to come back with a pizza.</p>
<p>The owner can easily define new ways to order food through a restaraunt.</p>
<p>Lisp, through &#8220;lambdas&#8221;, lets a programmer define new functions as easily as a restaraunt owner can devise new ways to order. The inputs and outputs are the same, but making new functions can have very low overhead compared to other programming languages.</p>
<h2>Brevity</h2>
<p>&#8220;Wiz Wit&#8221;.</p>
<p>This is my usual order at <a href="http://en.wikipedia.org/wiki/Pat%27s_King_of_Steaks">Pat&#8217;s</a> in Philly. With just two words, I get a cheesesteak with Cheez-Wiz and fried onions in less than 5 seconds. The system is designed to maximize the number of cheesesteaks output per hour: orders are short, and you usually get your cheesesteak before you&#8217;re done paying [and sometimes, before you're done speaking!]</p>
<p>This lesson can be applied to writing: It&#8217;s easier to convey a short thought. Unless you are Charles Dickens or Neal Stephenson, economy of language should be a goal.</p>
<p>Lisp&#8217;s syntax is very simple and short, so you can usually convey a thought in Lisp much quicker than you can convey a thought in most other programming languages.</p>
<h2>Macros</h2>
<p>An order can come in several different forms: it can be written, it can be gestured, it can be be spoken, and it can be implicit.</p>
<p>A lot of programming languages let you generalize the details of what an order can receive. Most orders are the same, so you can handle the contents the same even though the orders are different. In C++, you could even use &#8220;templates&#8221; to write code that generates code that handles this idea.</p>
<p>Lisp takes this idea all the way to its logical conclusion through &#8220;macros.&#8221; You can easily write functions that define orders. The difference is that you can easily do this at any scope!</p>
<p>If you notice that all fast food restaraunts are the same except for color scheme, specials, and slogans, it is easy to write functions that define new fast food chains. If you notice that your functions for defining fast food chains are similar to your functions for defining hardware chains, you can write functions that generate these producers.</p>
<p>Lisp lets you easily do this manufacturing on a level that is unimaginable with any other language. It may be possible, but nothing beats Lisp&#8217;s ease-of-use.</p>
<h2>Self-Modifying Code</h2>
<p>The metaphor breaks down here, and I won&#8217;t force it. However, the idea is simple when it is explained.</p>
<p>Most programming languages are designed to modify memory. They all store their information in memory, retrieve it, modify it, and write it to memory again.</p>
<p>Some programming languages try to hide the fact that they&#8217;re based on a memory-modification model, but they are extremely similar to languages that don&#8217;t hide it.</p>
<p>Lisp, however, is written to modify lists. Lisp is also written using lists, so a Lisp program can be programmed to modify itself.</p>
<h2>Conclusion</h2>
<p>This is just the tip of the iceberg! There are a lot more ideas that lay beneath the surface here, and you can bet that I&#8217;ll think about how to explain them to an educated adult. However, this should get someone with passing familiarity to programming to understand what makes Lisp stand out: ease-of-use and brevity.</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=102&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2008/07/21/explaining-lisp-to-my-father/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>The Chabaud-Joux Attack Against SHA-0</title>
		<link>http://www.jakevoytko.com/blog/2008/06/30/the-chabaud-joux-attack-against-sha-0/</link>
		<comments>http://www.jakevoytko.com/blog/2008/06/30/the-chabaud-joux-attack-against-sha-0/#comments</comments>
		<pubDate>Mon, 30 Jun 2008 04:00:38 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Chabaud-Joux]]></category>
		<category><![CDATA[cryptographic hash functions]]></category>
		<category><![CDATA[hash functions]]></category>
		<category><![CDATA[local collisions]]></category>
		<category><![CDATA[message expansion]]></category>
		<category><![CDATA[SHA-0]]></category>
		<category><![CDATA[SHA-1]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=90</guid>
		<description><![CDATA[Look [here ] if you want to learn about cryptographic hash functions, their properties, or their applications. History of SHA-0 In 1993, SHA-0 descended from heaven. The NSA published SHA-0 in FIPS-180, which contained the algorithm with no explanation of its security. Concerns were thrown aside as the algorithm became widely adopted. The stated collision [...]]]></description>
			<content:encoded><![CDATA[<p>Look [<a href="http://en.wikipedia.org/wiki/Cryptographic_hash_function">here</a> ] if you want to learn about cryptographic hash functions, their properties, or their applications.</p>
<h2>History of SHA-0</h2>
<p>In 1993, SHA-0 descended from heaven. The NSA published SHA-0 in FIPS-180, which contained the algorithm with no explanation of its security. Concerns were thrown aside as the algorithm became widely adopted. The stated collision resistance [the resistance most pertinent to the attack listed below] was the theoretical maximum of 2^80.</p>
<p>In 1995, the NiST released an improvement on the algorithm in <a href="http://www.itl.nist.gov/fipspubs/fip180-1.htm">FIPS 180-1</a> . The new algorithm (called SHA-1) had just an extra shift operation in the message expansion. Other than this detail, the new algorithm was identical to SHA-0. According to the NSA, this corrected a weakness in the message expansion.</p>
<p>In 1998, researchers Chabard and Joux released an elementary attack on SHA-0, described below. It exploited a fundamental weakness in the simple round structure of SHA-1, combined with an exceptionally simple message expansion. Their attack employs a search algorithm that requires only examining 2^61 messages instead of 2^80 messages to find a collision.</p>
<p>This was only a theoretical break in SHA-0: the collision resistance was still improbably high (though breakable through a distributed computing project), and the only collisions produced were gibberish.</p>
<p>More than one weakness in SHA-0 was found in the meantime! Biham and Chen discovered another route to finding collisions and near-collisions, and Chinese researchers Yang, Yu, and Yin have effectively pwnt [to use a technical term] SHA-0. In their most recent attack, they found a search space that requires examining only 2^39 different messages. This is possible on a beefy desktop computer. They have only released the attack vector and a high-level overview of how they arrived at the vector. They have not released the details for how they developed their attack in an English language journal.</p>
<p>The Biham and Chen attack is actually cooler than the Chibaud-Joux attack, but much more complicated. If this essay is well-received, I will write a post on that.</p>
<h2>Description of SHA-0</h2>
<p>SHA-0 is a block hash with an input of 512 bits. This means that any input message is broken into 512-bit blocks. The output from processing the first block is the input for processing the second block, and so on and so forth. The output of the final processing step is your hash value.</p>
<p>Writing the description is far less clear than just reading the pseudocode, so I urge you to read the <a href="http://en.wikipedia.org/wiki/SHA1#SHA-1_algorithm">pseudocode listed on Wikipedia</a> or a reference <a href="http://www.jakevoytko.com/src/sha1.txt">implementation I wrote in Python</a> [note: Python is not my primary language, and I wrote it so that a Math major with no coding experience could understand it, so I make no guarantees about it.]</p>
<p>The overall structure of SHA is as follows:</p>
<p>1) <strong>Message padding: </strong> Because SHA-0 operates on blocks, we need to make sure that we can evenly break the message into 512-bit blocks. The length of the message must also be appended on the end of the message. If the message + the length is not 0 (mod 512), then in between the message and the appended length, the string &#8220;100&#8230;..000&#8243; is added to make the message the right length.</p>
<p>2) <strong>Message Expansion: </strong> The message is then divided into 512-bit blocks. Each 512-bit block can be broken down into 16 32-bit words. However, just plugging these 16 words into the round functions (described below) would give attackers too much control over the internal state of the round function, the 16 words are expanded into 80 words using the recurrence W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16] for 17&lt;i&lt;80. Note that the first 16 words of the expansion are the 16 words from the block.</p>
<p>3) <strong>80 rounds</strong> : The input of the hash function, in addition to the message, is also a 5 word magic constant. These 5 words are transformed using the results of the message expansion, and then added to the original magic word to produce the hash.</p>
<p>After that, the resulting 5 registers are added with the initial state of the 5 registers. For the first block, this means the result is added to the &#8220;magic number&#8221;.</p>
<h2>Two messages side-by-side</h2>
<p>If we take two messages, m1 and m2, and let m1=m2, obviously SHA-0(m1) = SHA-0(m2). Cryptographic hash functions MUST be deterministic by definition.</p>
<p>The ultimate goal is to find two messages that hash to the same value: Hash(some_message) = Hash(other_message). This, by itself, is overwhelming. So Chabaud and Joux decided to cheat: They would find collisions for successively stronger versions of SHA-0 until they found a collision attack against the full SHA-0.</p>
<h2>Local Collisions</h2>
<p>Chabaud and Joux needed better tools to attack SHA-0, so they decided to find necessary conditions for finding collisions for <em>just a few rounds</em> of SHA-0. They decided to see what happens if they flip a single bit in a round, and ignore the message expansion. So they are allowed to put <em>whatever they want</em> into each round of the hash function by the rules of this simplified attack.</p>
<p>Let&#8217;s go back to identical messages m1 and m2. Let&#8217;s alter m2 a little: we will flip a single bit. If we make a change to a bit in some round <em>r</em> , the register <em>A</em> will be the only register affected in round <em>r</em> . In round <em>r+1</em> , the registers <em>A</em> and <em>B</em> will be affected. So on and so forth, so it will take until round <em>r+4</em> for the bit difference in m2 to propagate all the way to the last register, <em>E</em> .</p>
<p>Chabaud and Joux decided that they wanted to find the minimum amount of rounds necessary to createa  local collision from <em>r</em> . It turns out that if you have full control over the input to the hash function, you can create a local collision at round <em>r+5</em> : The hash at round <em>r+5</em> of m1 and m2 is the same even though m1 and m2 are different functions. This is called a 6-round local collision (Rounds: <em>r, r+1, r+2, r+3, r+4, r+5</em> ).</p>
<h2>How?</h2>
<p>Let&#8217;s look at round <em>r+1</em> , the round after the first bit-change occurred. The changes that happen to the A register are defined as:</p>
<pre>A = (A &lt;&lt;&lt; 5) + <em>f</em>
(B, C, D) + E + round_constant + input[<em>r+1</em>
]</pre>
<p>where f is one of the 4 round functions defined in SHA-0. We have full control over the term &#8220;input[r+1]&#8220;.</p>
<p>and the changes that happen to the <em>B</em> register are:</p>
<pre>B = A</pre>
<p>We can&#8217;t correct the difference in B that propagates from A (because we have no additive term in this equation), but we CAN correct the difference that occurs in A. After all, we know what bit we changed, so getting A to match up in m1 and m2 is easy: we just have to do the math.</p>
<p>Chabaud and Joux define specific masks for <em>r+1, r+2, r+3, r+4, </em> and <em>r+5</em> , and voila! A local collision.</p>
<h2>Why does this work?</h2>
<p>This works because of a special fact about the <em>A</em> register: it is the only register that is directly modified by the message, AND it is the only register whose value is produced by the values of other registers. Since the other registers aren&#8217;t sharing bit differences, we only have to fight the changes in the <em>A</em> register in order to succeed.</p>
<h2>Problems!</h2>
<p>If it were really this easy to find a local collision, SHA-0 would have been completely unusable. Fortunately for its strength, there are a few complicating factors:</p>
<p><strong>Message expansion</strong> : The message expansion produces an additional constraint: this produces the need to chain together a bunch of local collisions inside of the 80 rounds in order to come up with one full collision. The constraints of the message expansion need to be satisfied by your chosen starting points for all of the local collisions (as your bit differences that you introduce will be scattered by the message expansion in the exact same way).</p>
<p><strong>Round-specific functions</strong> : To make matters worse, there are round-specific functions that change every 20 rounds. Each of the functions are functions of the registers <em>B</em> , <em>C</em> , and <em>D</em> , and can therefore be viewed as functions of past values of <em>A</em> . Two of the functions are <em>non-linear</em> : they can&#8217;t be modeled by an XOR, and they have their own specific constraints for when a local collision can be produced. The round functions prevent the local collision from being automatic: depending on the round we start the collision, we need to search for which bits we need to flip. The Chabaud-Joux attack on SHA-0 has a search space of 2^61 for bits that need to be flipped in order to find the correct hash.</p>
<h2>The weak message expansion</h2>
<p>However, there is still an extra foothold that Chabaud and Joux were able to use: the message expansion of SHA-0 is just an XOR. When they were satisfying the constraints of the bits they were flipping, they were able to keep flipping the same bit in each message word instead of needing to come up with complicated arrangements of bits that needed to be flipped. The extra rotation function added to SHA-1 caused this particular attack to be impractical: the search space for bits to be flipped rose dramatically, and new approaches had to be developed.</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=90&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2008/06/30/the-chabaud-joux-attack-against-sha-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
