<?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</title>
	<atom:link href="http://www.jakevoytko.com/blog/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>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A Code Workaround for a Busted Furnace</title>
		<link>http://www.jakevoytko.com/blog/2010/01/17/a-code-workaround-for-a-busted-furnace/</link>
		<comments>http://www.jakevoytko.com/blog/2010/01/17/a-code-workaround-for-a-busted-furnace/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 15:16:00 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[boost]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[furnace]]></category>
		<category><![CDATA[threading]]></category>
		<category><![CDATA[workaround]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=406</guid>
		<description><![CDATA[warmup.cpp: [code]
Late last December, I woke up and it was freezing in my apartment. I checked the thermostat, and it was already less than 60 degrees! I turned it from hot to cold to hot. And again. I turned it all the way up. The furnace never clicked on. COME ON! I checked, and the [...]]]></description>
			<content:encoded><![CDATA[<p>warmup.cpp: [<a href="http://github.com/jakevoytko/warmup">code</a>]</p>
<p>Late last December, I woke up and it was <strong>freezing</strong> in my apartment. I checked the thermostat, and it was already less than 60 degrees! I turned it from hot to cold to hot. And again. I turned it all the way up. The furnace never clicked on. COME ON! I checked, and the pilot light was on, so I had no choice but to call maintenance to get it fixed.</p>
<p>Most days this wouldn&#8217;t have been a problem, since it would be fixed while I was at work, but I was in my end-of-year vacation-burning crunch. Maintenance always fix minor problems really fast, so I knew it would take all day to come and fix the heater. Sure enough, help didn&#8217;t arrive until 6PM.</p>
<p>I tried lying motionless on the floor under some blankets and played some Assassin&#8217;s Creed 2. This worked for an hour or so, but if I&#8217;m at home all day and not working on something on the computer, I start to feel like a waste. This was in the middle of trying to get <a href="http://www.vocabdojo.com/">a side project</a> of mine off the ground, and I wanted to get a beta version working before returning to work in January.</p>
<p>Could I use my laptop as a heater? It gets pretty warm when the CPU runs at full blast on one core. If I kept both going for hours, would that be enough?</p>
<p>8 minutes later, I had a C++ program that ran 2 CPUs at full blast. I hate picking C++ for a personal project, but I&#8217;ve used it for 10 years and I&#8217;m just not familiar enough with any other language&#8217;s CPU usage patterns to do the work that quickly. I probably didn&#8217;t have to worry, but I was cold!</p>
<p>The program, <code>warmup</code>, runs NUM_CPU copies of this thread:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> fn<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">bool</span> <span style="color: #000040;">*</span>go<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">double</span> a <span style="color: #000080;">=</span> <span style="color:#800080;">0</span>;
  <span style="color: #0000ff;">static</span> mt19937 rng<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">static_cast</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">unsigned</span><span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #0000dd;">time</span><span style="color: #008000;">&#40;</span><span style="color:#800080;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>;
  normal_distribution<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">double</span><span style="color: #000080;">&gt;</span> norm_dist<span style="color: #008000;">&#40;</span><span style="color:#800080;">1.0</span>, <span style="color:#800080;">1.0</span><span style="color: #008000;">&#41;</span>;
  variate_generator<span style="color: #000080;">&lt;</span>mt19937<span style="color: #000040;">&amp;</span>, normal_distribution<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">double</span><span style="color: #000080;">&gt;</span> <span style="color: #000080;">&gt;</span>
  normal_sampler<span style="color: #008000;">&#40;</span>rng, norm_dist<span style="color: #008000;">&#41;</span>;
&nbsp;
  <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>go<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">double</span> val <span style="color: #000080;">=</span> normal_sampler<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>;
    a <span style="color: #000040;">+</span><span style="color: #000080;">=</span> val;
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0000ff;">return</span> <span style="color:#800080;">0</span>;
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>I already had Boost, and I&#8217;d never used their pRNGs before, so I decided if it was a copy/paste job, I would give them a test run. Sure enough, it plugged right in.</p>
<p>The main function is even simpler:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">bool</span> go <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span>;
  boost<span style="color: #008080;">::</span><span style="color: #007788;">scoped_ptr</span><span style="color: #000080;">&lt;</span>boost<span style="color: #008080;">::</span><span style="color: #007788;">thread</span><span style="color: #000080;">&gt;</span> threads<span style="color: #008000;">&#91;</span>NUM_THREADS<span style="color: #008000;">&#93;</span>;
&nbsp;
  <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:#800080;">0</span>; i<span style="color: #000080;">&lt;</span>NUM_THREADS; <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span>
  threads<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">reset</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">new</span> boost<span style="color: #008080;">::</span><span style="color: #007788;">thread</span><span style="color: #008000;">&#40;</span>fn, <span style="color: #000040;">&amp;</span>go<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>;
&nbsp;
  std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> asdf;
  <span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;enter any input and hit &lt;Enter&gt; to kill&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl;
  <span style="color: #0000dd;">cin</span><span style="color: #000080;">&gt;&gt;</span>asdf;
&nbsp;
  go <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span>;
&nbsp;
  <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:#800080;">0</span>; i<span style="color: #000080;">&lt;</span>NUM_THREADS; <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span>
    threads<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>join<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>;
&nbsp;
  <span style="color: #0000ff;">return</span> <span style="color:#800080;">0</span>;
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Within 12 minutes of starting, I was nice and toasty!</p>
<p>I posted the code on Github in case I needed it somewhere else: [<a href="http://github.com/jakevoytko/warmup">code</a>]<br />
﻿</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=406&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2010/01/17/a-code-workaround-for-a-busted-furnace/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>A Wrinkle in (Deconstructed) Time</title>
		<link>http://www.jakevoytko.com/blog/2009/10/19/a-wrinkle-in-deconstructed-time/</link>
		<comments>http://www.jakevoytko.com/blog/2009/10/19/a-wrinkle-in-deconstructed-time/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 04:00:15 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Are We There Yet]]></category>
		<category><![CDATA[Clojure]]></category>
		<category><![CDATA[Expiration Problem]]></category>
		<category><![CDATA[language design]]></category>
		<category><![CDATA[Rich Hickey]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=383</guid>
		<description><![CDATA[Or, &#8220;Hey, you blogged again! It&#8217;s about time!&#8221;
In &#8220;Are We There Yet? A Deconstruction of Object Oriented Time,&#8221;[slides] [interview] Clojure creator Rich Hickey looks beyond concurrency support in popular programming languages to see what&#8217;s on the horizon. He asserts that wrapping our objects in locks &#8211; the popular solution &#8211; is an attempt to treat [...]]]></description>
			<content:encoded><![CDATA[<p><em>Or, &#8220;Hey, you blogged again! It&#8217;s about time!&#8221;</em></p>
<p>In &#8220;<em>Are We There Yet? A Deconstruction of Object Oriented Time</em>,&#8221;[<a href="http://wiki.jvmlangsummit.com/images/a/ab/HickeyJVMSummit2009.pdf">slides</a>] [<a href="http://www.artima.com/articles/hickey_on_time.html">interview</a>] <a href="http://clojure.org/">Clojure</a> creator Rich Hickey looks beyond concurrency support in popular programming languages to see what&#8217;s on the horizon. He asserts that wrapping our objects in locks &#8211; the popular solution &#8211; is an attempt to treat concurrent objects as if they were accessed by a single thread. Hickey describes the problems with this approach in two slides:</p>
<blockquote><p>We don&#8217;t make decisions about things in the world by taking turns rubbing our brains on them.</p>
<p>Nor do we get to stop the world when we want to look around.</p></blockquote>
<p>Instead of this awkward locking model, he suggests a versioned approach to representing object instances. He also presents some programming constructs to be used in his versioned model.</p>
<p>Below, I present his idea of a pure function to update the view of objects to a new state. I also introduce a problem that this doesn&#8217;t handle, the <strong>expiration problem</strong>, that language designers should either address or state they don&#8217;t handle.</p>
<h3>Background</h3>
<p>In Java and C++ (and many other languages), it&#8217;s very easy to write concurrent code that causes inconsistent results across multiple reads. So easy, in fact, that it&#8217;s the default for everything! You&#8217;d have to do external locking or go to heroic lengths to avoid doing it. It happens regardless of the thread safety of the underlying class!</p>
<p>Let&#8217;s look at an example: Assume that some Evil Crime Syndicate is trying to kill me. Even worse, they have cracked all government mainframes and always possess my contact information. Like all Evil Crime Syndicates, they have a gigantic code base written in Java that runs the whole operation. To save themselves some personnel costs, they wrote an automated assassin dispatching system.</p>
<p>Let&#8217;s look at a scenario where the assassin acts on an inconsistent kill order:</p>
<p><em>Assassin code</em></p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">class</span> Assassin
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">void</span> killPerson<span style="color: #009900;">&#40;</span>Person person<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">String</span> personAddress <span style="color: #339933;">=</span> person.<span style="color: #006633;">getAddress</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
      <span style="color: #666666; font-style: italic;">// --- INTERMEDIATE ACTION BY ANOTHER THREAD ---</span>
      <span style="color: #666666; font-style: italic;">// I enter the Witness Protection Program, and my</span>
      <span style="color: #666666; font-style: italic;">// name and address are changed.</span>
&nbsp;
      <span style="color: #003399;">String</span> personName <span style="color: #339933;">=</span> person.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
      <span style="color: #666666; font-style: italic;">// Some private method</span>
      actOnKillOrder<span style="color: #009900;">&#40;</span>personName, personAddress<span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p><em>Application code</em></p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">Person nextTarget <span style="color: #339933;">=</span> TargetFactory.<span style="color: #006633;">makeTarget</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Jake&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">// Not shown: logging, passing the target info to a central</span>
<span style="color: #666666; font-style: italic;">// database, etc</span>
&nbsp;
currentAssassin.<span style="color: #006633;">killPerson</span><span style="color: #009900;">&#40;</span>nextTarget<span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>If I officially enter the Witness Protection Program while <code>killPerson()</code> performs sequential reads, the assassin will look for a new name at an old address. In the best case, he&#8217;ll get some puzzled looks when he asks around for a new identity at the old location.</p>
<p>We&#8217;ll call the worst case deadlock <img src='http://www.jakevoytko.com/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<h3>A second chance for the Evil Crime Syndicate</h3>
<p>Could language features help the Evil Crime Syndicate kill me? Hickey proposes that instead of looking at objects as a single mutable data point, we treat objects as a succession of states. Each view perceives an immutable state at some point in time, and references it until it is updated. If the view needs to know the current state of the object, then it can call a function that returns the object&#8217;s newest state. If the object is constantly being updated by other views, you won&#8217;t see any of these changes until you call this function and get the new version of the object.</p>
<p>This is a version control system for objects. You query the object once and do stuff with the data that you get back. When you want to pull the current updates for the new object, just call the system&#8217;s update command and get a new version! The system handles all of the reads and writes behind the scene, just like a real database. Interesting aside, databases and version control systems concurrent systems that Joe Coder already uses on a massive scale with minimal concurrency problems.</p>
<p>To imagine this feature in Java, let&#8217;s call our pure function <code>Update()</code>. It takes the current view of a Java object and returns the newest value of the object. Yes, I know that adding a function to Java completely breaks the consistency of Java&#8217;s all-object model, but <a href="http://www.jakevoytko.com/blog/2008/03/01/people-are-the-problem-not-operator-overloading/">you&#8217;ll notice that I have no control over the language features added to Java</a>. This continues to be a good decision made by Sun Microsystems.</p>
<p>It turns out that <code>Update()</code> makes it easy to show that the assassin always acts on consistent data!</p>
<p><em>Assassin code</em></p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">class</span> Assassin
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">void</span> killPerson<span style="color: #009900;">&#40;</span>Person person<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">String</span> personAddress <span style="color: #339933;">=</span> person.<span style="color: #006633;">GetAddress</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
      <span style="color: #666666; font-style: italic;">// If my data is updated here, this view's</span>
      <span style="color: #666666; font-style: italic;">// view of 'person' doesn't change. All reads</span>
      <span style="color: #666666; font-style: italic;">// are consistent!</span>
&nbsp;
      <span style="color: #003399;">String</span> personName <span style="color: #339933;">=</span> person.<span style="color: #006633;">GetName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
      actOnKillOrder<span style="color: #009900;">&#40;</span>personName, personAddress<span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p><em>Application code</em></p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">Person nextTarget <span style="color: #339933;">=</span> TargetFactory.<span style="color: #006633;">makeTarget</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;That Jake guy&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">// Not shown: passing the target to a central database, etc</span>
&nbsp;
person <span style="color: #339933;">=</span> Update<span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span>;
currentAssassin.<span style="color: #006633;">killPerson</span><span style="color: #009900;">&#40;</span>nextTarget<span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>In this code snippet, the fact that <code>Update()</code> is called before dispatching the assassin ensures that the assassin is acting on a consistent and relatively recent view of me. I mean, I could enter Witness Protection as the assassin travels, or immediately after <code>Update()</code> is called. However, that&#8217;s a logistics problem for the assassin, and he won&#8217;t get any sympathy from me!</p>
<h3>The Expiration Problem</h3>
<p>Now the wrinkle! What if we query real-world objects for their state? Let&#8217;s say that you have an atomic clock hooked up to your computer via a serial port.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">     AtomicTime time <span style="color: #339933;">=</span> atomicClock.<span style="color: #006633;">queryTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Let&#8217;s assume that the delta between the <code>AtomicClock</code> time and the system time is known and constant, which is not usually a reasonable assumption. For the sake of discussion, let&#8217;s also assume that <code>Update()</code> can update an <code>AtomicTime</code> to the most recent value pulled from the <code>AtomicClock</code>. How does <code>Update()</code> interact with this real-time object?</p>
<p>Most of the time, it works very well. We can use <code>Update()</code> to ensure that our view of the time is consistent if we read hours, microseconds, and nanoseconds sequentially.</p>
<p>As we use it more, we run into a problem! Let&#8217;s assume that we want to display the atomic time to the user. We could just constantly <code>Update()</code> the time returned from the atomic clock, but I&#8217;m not a big fan of polling. Why? If we hit unexpected latency or blocking and we poll slower than we expect, we&#8217;re going to display times to the user that are no longer accurate. <strong>The data changed and nobody told us.</strong></p>
<p>I call this the <em>expiration problem</em>. Sometimes it&#8217;s easy to fix &#8212; <em>dude, just poll the clock faster. Duh.</em> &#8212; and other times it&#8217;s not so easy. Just look at the assassin example. Even when we could show the function always worked consistently, the assassin could still be dispatched to an old address looking for an old name. This is obviously bad: the data rotted and the programming language stayed silent. The system knows everything it needs to say, &#8220;Hey, by the way, this data isn&#8217;t good anymore,&#8221; but it doesn&#8217;t.</p>
<p>The front office of the Evil Crime Syndicate naturally doesn&#8217;t understand the distinction and will totally give my would-be assassin a hard time. &#8220;What do you mean you killed the wrong person? Our system always gives a consistent and up-to-date target. Obviously this is your fault. You&#8217;re getting a pay cut after you finish this assignment.&#8221;</p>
<p>Of course, there are also problems introduced when values are allowed to expire. The data doesn&#8217;t always <span style="text-decoration: underline;">need</span> to expire! An assassin could gather clues from old aliases and residences, for instance. You could want to store a list of <code>AtomicTime</code>s for processing elsewhere.</p>
<p>Any language that uses expiration as an explicit action needs to also allow the programmer to differentiate between the following:</p>
<ul>
<li><strong>Hard expiration</strong>: Occurs when using a dated value could be expensive or harmful. An exception is thrown if you attempt to use the object after expiration.</li>
<li><strong>Soft expiration:</strong> The programmer wants to know if the value goes bad, but the value is still usable in some views. This could be implemented through either callback handlers or exceptions.</li>
<li><strong>No expiration</strong>:  The default for most data.</li>
</ul>
<p>Ideally, this would be settable per each view of the data.</p>
<h3>Conclusion</h3>
<p>Concurrent programming has a lot of inherent wrinkles, and trying to remove all of them out is a very deep rabbit hole. Each solution usually points to another problem. Sometimes they don&#8217;t really matter, like showing an atomic clock time for an extra few nanoseconds. Sometimes they could cause expensive mistakes, like sending an assassin to the wrong place for the wrong target.</p>
<p>Rich Hickey&#8217;s pure function that returns the future as a function of the present is a great idea, and I&#8217;d love to see Hickey implement something like it in Clojure. It may also be possible to implement expiration! I recall that Clojure allows validators to be attached to objects to check for consistency across updates. I don&#8217;t see why expiration couldn&#8217;t be handled similarly to check for staleness on reads.</p>
<p>I would love to see some other language-level ideas and implementations for aborting a current action if the data fails a staleness check. &#8220;Data is guaranteed consistent and up-to-date!&#8221; would be a pretty good selling point for a language, even if the utility of having both at the same time is often low.</p>
<p>The expiration problem exists in every language, but if we&#8217;re actively trying to ease the pain of concurrent programming, it&#8217;s something that needs to be explicitly formalized and addressed. In fact, I&#8217;d bet a dollar that there are already 5 different formalizations for this phenomena. Does anyone know if this has been discussed in a more professional or academic publication?</p>
<p><em>Special thanks to <a href="http://sarahkbraun.com/">Sarah Braun</a> and <a href="http://www.babeled.com/">Greg Molyneux</a> for reading drafts</em></p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=383&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2009/10/19/a-wrinkle-in-deconstructed-time/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Languages and Axioms</title>
		<link>http://www.jakevoytko.com/blog/2009/03/10/languages-and-axioms/</link>
		<comments>http://www.jakevoytko.com/blog/2009/03/10/languages-and-axioms/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 04:00:04 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[Clojure]]></category>
		<category><![CDATA[Paul Graham]]></category>
		<category><![CDATA[reaxiomization]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=378</guid>
		<description><![CDATA[I learned Common Lisp last summer. I had been using some combination of Python, C++, C, and Java for about 8 years, and I wanted to break out of my popular language box. I was more or less attracted to the &#8220;data as code&#8221; idea, but really it had a bunch of selling points that [...]]]></description>
			<content:encoded><![CDATA[<p>I learned Common Lisp last summer. I had been using some combination of Python, C++, C, and Java for about 8 years, and I wanted to break out of my popular language box. I was more or less attracted to the &#8220;data as code&#8221; idea, but really it had a bunch of selling points that I would have appreciated:</p>
<ul>
<li> Its data was stored in the same structures as its program.</li>
<li> It had macros that were powerful and simple. That&#8217;s huge coming from C++.</li>
<li> It was REPL-based, something I never took advantage of with Python.</li>
<li> You could create new code on-the-fly at runtime from lists.</li>
<li> Completely dynamic and backed by garbage collection.</li>
<li>The language itself is very mutable.</li>
</ul>
<p>The sum of its parts was greater than anything that I had ever used before, and it quickly became my favorite language. I learned quite a bit about functional programming techniques, did some experimentation with image processing and database application programming, and worked through all of Graham&#8217;s  <a href="http://www.amazon.com/ANSI-Common-Prentice-Artificial-Intelligence/dp/0133708756?ie=UTF8&amp;s=books&amp;qid=1215999519&amp;sr=8-1">ANSI Common Lisp</a> and large swaths of Norvig&#8217;s <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></p>
<p>Recently, a nagging though started to cross my mind: <strong>What if I didn&#8217;t go far enough to break out of my box?</strong></p>
<p>That didn&#8217;t happen until two weeks ago, when I started porting <a href="http://github.com/jakevoytko/genesis/tree/master">Genesis</a>, my toy <a href="http://en.wikipedia.org/wiki/Genetic_algorithm">genetic algorithm</a> framework, to <a href="http://clojure.org/">Clojure</a> for fun. I wasn&#8217;t very aware of the features of Clojure: I knew that it was a Lisp on the JVM and that it supported <a href="http://en.wikipedia.org/wiki/Software_Transactional_Memory">Software Transactional Memory</a>, but I didn&#8217;t think it would be  that different from Common Lisp aside from Java interoperability.</p>
<p>To clarify, I expected that the primary method of manipulating Clojure would be list-based, and that it would still mostly turn a blind eye towards mutable data and multithreading issues, with the option of phrasing memory updates in terms of transactions if you were using multithreaded applications.</p>
<p>Clojure turned out to be different than any language that I had ever tried &#8211; save my forays into <a href="http://haskell.org/">Haskell</a> to grok enough syntax to follow <a href="http://blog.sigfpe.com/">sigfpe</a>&#8217;s easier articles.  If you trust the language and work with the core Clojure data structures, it takes a lot of the worrying about parallel programming out of your hands. Its creator, Rich Hickey, ended up with a language that has a good balance between functional programming and practicality. It is definitely a good Modern Programming Language.</p>
<h2>You Said Something About A Box..?</h2>
<p>Right, I said I didn&#8217;t go far enough outside of my comfort zone when learning Common Lisp. That&#8217;s really only half true.</p>
<p>From the perspective of programming, Common Lisp is a great language. Using the functional programming primitives provided on top of lists feels very natural, and I was able to work on complicated problems with relative ease. Even though I don&#8217;t use the macro system in Common Lisp very often, it usually provides a great fallback for either refactoring or changing the way the problem can be represented. The REPL is just icing on the cake.</p>
<p>However, not all is flowers and roses. Looking at Common Lisp from perspectives other than single-threaded programming, it begins to look mortal! For instance, the Common Lisp standard was written without a thought to having multiple threads of execution. Although most implementations provide extra support for multithreading, you&#8217;re back in the same locking boat that you were with any of the other popular languages once you need to share mutable data. Naturally, the syntax for things like grabbing mutexes is easier with Lisp than with other languages thanks to the &#8220;with-&#8221; design pattern for macro writing, but you&#8217;re not gaining much with respect to avoiding race conditions.</p>
<p>Looking at it from a data-processing perspective, functional programming is easy in Common Lisp, but it isn&#8217;t required at all. There&#8217;s nothing stopping the programmer from falling back into using local mutable state, functions with side-effects on global variables, and destructive functions. Combined with the single-thread design, that&#8217;s fine. You can&#8217;t avoid state all of the time, and I don&#8217;t go out of my way to be strictly functional. That&#8217;s how most other languages are designed. Hell, the Common Lisp standard even offers a whole slew of functions that are destructive for the sake of efficiency!</p>
<p>I&#8217;m only really getting at the fact that even the ultra-flexible of Common Lisp wasn&#8217;t written to handle all computing scenarios.</p>
<p>Some languages have support for neat automatic threading features: Haskell has extensions where type annotations can allow automatic parallelization, and you can use <a href="http://openmp.org/wp/">OpenMP</a> in C++ for automatic parallel processing. For those wondering, Erlang is somewhere on my list of languages to take a look at, but it has to wait its turn! I&#8217;m still having too much fun with Lisps.</p>
<h2>Axiomization and Language Design</h2>
<p>Love him or hate him, Paul Graham has written quite a bit about the design of Arc. I&#8217;m not going to talk about it directly, but I will borrow his idea of <a href="http://www.paulgraham.com/core.html">reaxiomizing the core of a language</a> as a means of producing better code on top of it. He mentions axioms as neither primitives nor library functions, but I will consider primitives as well.</p>
<p>I&#8217;ve talked about this a little in the past. For instance, I&#8217;ve made the case for operator overloading in Java. It was explicitly verboten in Java, as decreed by the designers. In fact, written in an early Java whitepaper titled &#8220;<a href="http://java.sun.com/docs/white/langenv/Simple.doc2.html#4098">The Java Language Environment</a>&#8220;,</p>
<blockquote><p><strong>2.2.7 No More Operator Overloading</strong></p>
<p>There are no means provided by which programmers can overload the standard arithmetic operators. Once again, the effects of operator overloading can be just as easily achieved by declaring a class, appropriate instance variables, and appropriate methods to manipulate those variables. Eliminating operator overloading leads to great simplification of code.</p></blockquote>
<p>By the letter of their assertions, they were correct. A lot of programmers would <em>never</em> need operator overloading to produce good code. In fact, if you&#8217;re using it just for the sake of expressiveness, you&#8217;re going to produce an unintuitive hodgepodge most of the time. However, a small but powerful minority &#8211; particularly those who do mathematical work for a living &#8211; would absolutely benefit from being able to write inline operators. I mean honestly, have you ever tried to use a <code>BigInteger</code> to implement something more complicated than RSA?  The best that will happen is that it will end up&#8230; verbose.</p>
<p>Essentially, leaving out operator overloading forced Java to be more verbose and/or less readable than it needed to be. Leaving it in as an &#8216;axiom&#8217; could measurably produce more compact, readable code for programmers who used it judiciously.</p>
<p>As an aside for those who think I&#8217;m just trolling Java, there is definitely a great Java feature for each one that misses the mark. For instance, the <code>enum</code>s in Java follow the idea to its logical conclusion.</p>
<p>If you&#8217;ve been incensed by my Java discussion or are otherwise unsure that axiomization could lead to a better language, let&#8217;s look back at Lisp. Lisp changes quite a bit if you remove utility functions like <code>reduce</code>: it would break a lot of the compactness of the language and ruin some otherwise good functional programming. Now, it doesn&#8217;t support operator overloading, per se, but there&#8217;s nothing stopping you from changing the language to support it.</p>
<h2>Where the Hell is All of This Going?</h2>
<p>Back to Clojure! Clojure is based on a much different set of axioms than many of the popular languages. At its core, we see support for thread-safe data structures whose data are immutable and persistent. These were written in as axioms to the language, and you can make some good predictions for the language, assuming people stick with it.</p>
<p>The biggest one is that as libraries are converted to Clojure or abstract away their Java API, Clojure will develop a large set of libraries that could be given a thread-safe guarantee.  For my day job, I work in a C++ shop that uses a bunch of C libraries, and we need to constantly be aware of whether or not our base libraries are thread-safe. My workplace recently developed a non-Clapack fork of our linear algebra abstraction for this reason: using libraries that aren&#8217;t designed to be threadsafe in a multithreaded environment builds bombs.</p>
<p>I&#8217;m too young to get a sense of what the next 10 years of programming languages might look like &#8211; but if you pressed me, it would look a lot like it does today, and we wouldn&#8217;t be better off for it. The languages that are currently in the design phase are slowly adding quite a bit of features while remaining mostly the same: for instance, C++ is getting a makeover that includes concepts, lambdas, futures, and a multithreading primitive.  I&#8217;ve known C++ for over half of my life (holy shit!), and a lot of the features are sorely needed. However, it&#8217;ll be an incremental improvement at the cost of obscure compiler bugs and increased complexity.</p>
<p>These aren&#8217;t easy problems: for instance, if you didn&#8217;t care a lick about any external concerns like backwards compatibility and wanted to change C++ to support lambdas, how could you without just tacking it on? However, we have new languages that can pick up where the old languages left off, and continuing Fred Brooks&#8217; prediction, there will be no silver bullet, only incremental improvements. Languages like Clojure are a step in the right direction as far as testing new computing ideas.</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=378&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2009/03/10/languages-and-axioms/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Underground by Suelette Dreyfus: Hackers, Crackers, and Feds (Oh My!)</title>
		<link>http://www.jakevoytko.com/blog/2009/02/16/underground-by-suelette-dreyfus-hackers-crackers-and-feds-oh-my/</link>
		<comments>http://www.jakevoytko.com/blog/2009/02/16/underground-by-suelette-dreyfus-hackers-crackers-and-feds-oh-my/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 04:00:08 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Book Review]]></category>
		<category><![CDATA[Quote]]></category>
		<category><![CDATA[Cracker]]></category>
		<category><![CDATA[Hacker]]></category>
		<category><![CDATA[Mendax]]></category>
		<category><![CDATA[Phreaker]]></category>
		<category><![CDATA[Suelette Dreyfus]]></category>
		<category><![CDATA[The Parmaster]]></category>
		<category><![CDATA[Theorem]]></category>
		<category><![CDATA[Underground]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=375</guid>
		<description><![CDATA[Available formats: [.txt] [.pdf] [.mp3]
So What&#8217;s It About?
&#8220;Underground&#8221; by Suelette Dreyfus is a great historical fiction narrative detailing the rise and fall of some crackers and phreakers and their opponents: phone company employees, system administrators, and Feds. It takes place between 1988 and 1994, when multi-user BBSes were starting to pop up and a telephone [...]]]></description>
			<content:encoded><![CDATA[<p><em>Available formats</em>: [<a href="http://www.gutenberg.org/etext/4686">.txt</a>] [<a href="http://jakevoytko.com/Underground.pdf">.pdf</a>] [<a href="http://lottalinuxlinks.com/podcast/underground.xml">.mp3</a>]</p>
<h2>So What&#8217;s It About?</h2>
<p>&#8220;Underground&#8221; by Suelette Dreyfus is a great historical fiction narrative detailing the rise and fall of some crackers and phreakers and their opponents: phone company employees, system administrators, and Feds. It takes place between 1988 and 1994, when multi-user BBSes were starting to pop up and a telephone call was the ONLY way to access other computers. It&#8217;s about 99 times better than this review. In fact, I request that you stop reading this immediately and just read it yourself. It&#8217;s really that good.</p>
<p>That being said, most of the stories are told from the perspective of the attackers, except for a few spurious tales of BBS operators and virus attacks. All of the attacker stories have similar plot themes:</p>
<ul>
<li> <strong>Personal Background:</strong> Who were their friends? How was their family life?</li>
<li> <strong>The first taste:</strong> How did they get into the scene? Who were their friends and mentors? How did they get free international calling? Were they chasing a white whale, or were they just enjoying themselves?</li>
<li> <strong>The glory days:</strong> Usually describes a list of big fish that the attacker accessed, as well as the social support the attacker got in his home community. At this point, the Feds notice the attacker and start playing cat-and-mouse with him.</li>
<li> <strong>The Law Won:</strong> The Feds make an identification, and either the attacker goes on the lam or gets busted at his computer. Nobody who got away would be at liberty to contribute to the book, so logically the attackers mentioned in the book are all caught. The court cases are described, as well as public reaction.</li>
<li> <strong>Where they are today:</strong> How the legal system treated them, what they did after their legal troubles were over, and are they currently employed?</li>
</ul>
<h2>Literary Freeware</h2>
<p>The author, Suelette Dreyfus released the book as &#8220;Literary Freeware,&#8221; allowing anyone with an internet connection to download the book for free. The text of the book was released to <a href="http://www.gutenberg.org/etext/4686">Project Gutenberg</a>, and the author herself wrote a special forward for the free version of the book, describing the reasons a book&#8217;s author might decide NOT to get paid for the book:</p>
<blockquote><p><em>From the forward: </em></p>
<p>Because part of the joy of creating a piece of art is in knowing that many people can &#8211; and are &#8211; enjoying it. Particularly people who can&#8217;t otherwise afford to pay $11 USD for a book. People such as cash strapped hackers. This book is about them, their lives and obsessions. It rubs clear a small circle in the frosted glass so the reader can peer into that hazy world. ‘Underground’ belongs on the Net, in their ephemeral landscape.</p>
<p>[...]</p>
<p>By releasing this book for free on the Net, I&#8217;m hoping more people will not only enjoy the story of how the international computer underground rose to power, but also make the journey into the minds of hackers involved. When I first began sketching out the book&#8217;s structure, I decided to go with depth. I wanted the reader to think, `NOW I understand, because I too was there.&#8217; I hope those words will enter your thoughts as you read this electronic book.</p></blockquote>
<p><span style="text-decoration: line-through;">The book&#8217;s website also hosted dozens of different formats/languages of the book. However, don&#8217;t try to go there anymore! It&#8217;s been taken over by some crappy hacker book e-commerce website.</span> A reader named Rory Gordon searched the internet much more thoroughly than I did and discovered that <a href="http://www.underground-book.net">http://www.underground-book.net</a> is apparently the website&#8217;s actual address. Also the Wayback Machine <a href="http://web.archive.org/web/20080203205459/http://www.underground-book.com/"> has you covered!</a> It even archived the 26 language/file format combinations the author made available.</p>
<h2>How Was It?</h2>
<p>It is good. How good? I dislike reading e-books. I like to read on my bed and sometimes write on my copy of the book. By the time I drag my desktop computer over to my bed and plug it in, I&#8217;m no longer in the mood for reading, and when I draw on the monitor, I can&#8217;t use it for other things. I actually ordered a hard copy of <a href="http://www.wolframscience.com/"><span style="text-decoration: underline;">A New Kind of Science</span></a> &#8212; review coming, whenever I summon the courage to read the last two chapters &#8212; so that I didn&#8217;t have to print out 1000 pages or read it on my computer monitor.</p>
<p>Despite all of this, I read the whole PDF (~300 pages) within 48 hours when I first got my hands on it &#8212; summer of 2007, for those keeping track. I stayed up late, I snuck peeks in my school IT job, and I couldn&#8217;t put it down to save my life.</p>
<p>At times, the pace is a lot like <span style="text-decoration: underline;">The Da Vinci Code</span>, but with much longer chapters. If you don&#8217;t like that, the pacing is a lot like <span style="text-decoration: underline;">Harry Potter</span>.  If you don&#8217;t like that, the pacing is similar to the action sequences of <span style="text-decoration: underline;">Snow Crash</span>. If you don&#8217;t like any of them, then I feel you need a good hug and some cake.</p>
<h2>Highlights</h2>
<p>First and foremost, it is well-written from a technical standpoint. There aren&#8217;t any &#8220;Law and Order&#8221; moments where images are super enhanced and Visual Basic GUIs are prototyped to track IP addresses. The author accessibly writes like she knows Unix systems, which is refreshing, given the number of errors I find in technical articles written by non-techies.</p>
<p>As for content, the story of Par is by far the best out of all of them. On a tip, he found a computer that was almost literally a gold mine: dialing it at certain times of the day caused it to spit out a large list of valid credit card numbers owned by rich Saudis. He ends up handing them out and using them like they&#8217;re free candy, which ends up drawing the eye of the Secret Service. He ends up having to go on the lam, surviving on the kindness of fellow hackers &#8212; and his 23-year-old Swiss girlfriend, Theorem. What follows is a great description of the techniques the Secret Service used to track him down (involving agencies working together on a few different continents), as well as his dumb luck in evading capture for so long.</p>
<p>Also, the book is chalk-full of moments like this:</p>
<blockquote><p>If, however, the line did not have a trace on it, the company’s modem would search for its lost connection to the hacker’s modem. Without the continuous flow of electronic signals, the NorTel modem would hang up after a few seconds. If no-one reactivated the line at the NorTel end, the connection would time-out 90 seconds later and the telephone exchange would disconnect the call completely.</p>
<p>Mendax listened anxiously as the NorTel modem searched for his modem by squealing high-pitched noises into the telephone line. No modem here. Go on, hang up.</p>
<p>Suddenly, silence.</p>
<p>OK, thought Mendax. Just 90 seconds to go. Just wait here for a minute and a half. Just hope the exchange times out. Just pray there’s no trace.</p>
<p>Then someone picked up the telephone at the NorTel end. Mendax started. He heard several voices, male and female, in the background. Jesus. What were these NorTel people on about? Mendax was so quiet he almost stopped breathing. There was silence at the receivers on both ends of that telephone line. It was a tense waiting game. Mendax heard his heart racing.</p>
<p>A good hacker has nerves of steel. He could stare down the toughest, stony-faced poker player. Most importantly, he never panics. He never just hangs up in a flurry of fear.</p>
<p>Then someone in the NorTel office&#8211;a woman&#8211;said out loud in a confused voice, ‘There’s nothing there. There’s nothing there at all.’</p>
<p>She hung up.</p>
<p>Mendax waited. He still would not hang up until he was sure there was no trace. Ninety seconds passed before the phone timed out. The fast beeping of a timed-out telephone connection never sounded so good. Mendax sat frozen at his desk as his mind replayed the events of the past half hour again and again. No more NorTel. Way too dangerous.</p></blockquote>
<h2>In Conclusion</h2>
<p><a href="http://jakevoytko.com/Underground.pdf">Read the book! This whole paragraph is a big link to the PDF. Just read it! It&#8217;s good!</a></p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=375&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2009/02/16/underground-by-suelette-dreyfus-hackers-crackers-and-feds-oh-my/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<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 professionally. I [...]]]></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 or by [...]]]></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 a complete introduction, I recommend this e-book. If you only want [...]]]></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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</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 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 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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">*</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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>add<span style="color: #66cc66;">-</span>to<span style="color: #66cc66;">-</span><span style="color: #b1b100;">list</span> 'load<span style="color: #66cc66;">-</span>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<span style="color: #66cc66;">-</span>hook 'lisp<span style="color: #66cc66;">-</span>mode<span style="color: #66cc66;">-</span>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<span style="color: #66cc66;">-</span>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<span style="color: #66cc66;">-</span>hook 'inferior<span style="color: #66cc66;">-</span>lisp<span style="color: #66cc66;">-</span>mode<span style="color: #66cc66;">-</span>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<span style="color: #66cc66;">-</span>slime<span style="color: #66cc66;">-</span>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<span style="color: #66cc66;">-</span>lisp<span style="color: #66cc66;">-</span>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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>require 'asdf<span style="color: #66cc66;">-</span>install<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; 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;">&gt;</span> <span style="color: #66cc66;">&#40;</span>asdf<span style="color: #66cc66;">-</span>install:<span style="color: #555;">install</span> 'postmodern<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; 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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>asdf:<span style="color: #555;">oos</span> 'asdf:<span style="color: #555;">load</span><span style="color: #66cc66;">-</span>op :<span style="color: #555;">postmodern</span><span style="color: #66cc66;">&#41;</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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #cc66cc;">3</span></pre></div></div>

<h3>Printing</h3>

<div class="wp_syntax"><div class="code"><pre class="lisp lisp" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;Hello, ~A~%&quot;</span> <span style="color: #ff0000;">&quot;Jacob&quot;</span><span style="color: #66cc66;">&#41;</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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> adding<span style="color: #66cc66;">-</span><span style="color: #b1b100;">function</span> <span style="color: #66cc66;">&#40;</span>arg1 arg2<span style="color: #66cc66;">&#41;</span>
    <span style="color: #ff0000;">&quot;Adds arg1 and arg2.&quot;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</span> arg1 arg2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
ADDING<span style="color: #66cc66;">-</span><span style="color: #b1b100;">FUNCTION</span>
&nbsp;
<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>adding<span style="color: #66cc66;">-</span><span style="color: #b1b100;">function</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #cc66cc;">3</span>
&nbsp;
<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</span> <span style="color: #66cc66;">&#40;</span>adding<span style="color: #66cc66;">-</span><span style="color: #b1b100;">function</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> adding<span style="color: #66cc66;">-</span><span style="color: #b1b100;">function</span> <span style="color: #66cc66;">&#40;</span>arg1 arg2<span style="color: #66cc66;">&#41;</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 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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defvar my<span style="color: #66cc66;">-</span>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<span style="color: #66cc66;">-</span>var 'a<span style="color: #66cc66;">-</span><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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> my<span style="color: #66cc66;">-</span>var <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>when <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
    t<span style="color: #66cc66;">&#41;</span>
t
<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>unless <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> expression
    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 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<span style="color: #66cc66;">-</span>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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">dotimes</span> <span style="color: #66cc66;">&#40;</span>i <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">dolist</span> <span style="color: #66cc66;">&#40;</span>i '<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>reduce #'<span style="color: #66cc66;">+</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</span> x <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>reduce <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>x y<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</span> x y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</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 progress in [...]]]></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>&#8217;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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>square<span style="color: #66cc66;">-</span>root<span style="color: #66cc66;">-</span>sample <span style="color: #66cc66;">&lt;</span>em<span style="color: #66cc66;">&gt;</span>generations population<span style="color: #66cc66;">-</span>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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>funcall<span style="color: #66cc66;">-</span>best <span style="color: #66cc66;">*</span>CURRENT<span style="color: #66cc66;">-</span>POPULATION<span style="color: #66cc66;">*</span> #'sample<span style="color: #66cc66;">-</span>fitness<span style="color: #66cc66;">-</span><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>3</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 proper random [...]]]></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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> genetic<span style="color: #66cc66;">-</span>algorithm <span style="color: #66cc66;">&#40;</span>generations starting<span style="color: #66cc66;">-</span>rules rule<span style="color: #66cc66;">-</span>fun
                          fitness<span style="color: #66cc66;">-</span>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<span style="color: #66cc66;">-</span>rules starting<span style="color: #66cc66;">-</span>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<span style="color: #66cc66;">-</span>num generations<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>run<span style="color: #66cc66;">-</span>generation rule<span style="color: #66cc66;">-</span>fun fitness<span style="color: #66cc66;">-</span>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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>genetic<span style="color: #66cc66;">-</span>algorithm <span style="color: #cc66cc;">32000</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">/</span> num <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</span> num .2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                   #'sample<span style="color: #66cc66;">-</span>rule<span style="color: #66cc66;">-</span>fun #'sample<span style="color: #66cc66;">-</span>fitness<span style="color: #66cc66;">-</span>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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defvar <span style="color: #66cc66;">*</span>RULES<span style="color: #66cc66;">*</span> <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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defvar <span style="color: #66cc66;">*</span>sample<span style="color: #66cc66;">-</span>nodes<span style="color: #66cc66;">*</span> #<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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defvar <span style="color: #66cc66;">*</span>sample<span style="color: #66cc66;">-</span>values<span style="color: #66cc66;">*</span>
    #<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">-</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<span style="color: #66cc66;">-</span>generate<span style="color: #66cc66;">-</span>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<span style="color: #66cc66;">-</span>array<span style="color: #66cc66;">-</span>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<span style="color: #66cc66;">-</span>errors <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">/</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>when <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: #66cc66;">/</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 lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> sample<span style="color: #66cc66;">-</span>rule<span style="color: #66cc66;">-</span>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<span style="color: #66cc66;">-</span>array<span style="color: #66cc66;">-</span>element <span style="color: #66cc66;">*</span>sample<span style="color: #66cc66;">-</span>nodes<span style="color: #66cc66;">*</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>leaf <span style="color: #66cc66;">&#40;</span>sample<span style="color: #66cc66;">-</span>generate<span style="color: #66cc66;">-</span>leaf <span style="color: #66cc66;">*</span>sample<span style="color: #66cc66;">-</span>values<span style="color: #66cc66;">*</span><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<span style="color: #66cc66;">-</span>least<span style="color: #66cc66;">-</span>once #'sample<span style="color: #66cc66;">-</span>rule<span style="color: #66cc66;">-</span>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<span style="color: #66cc66;">-</span>generate<span style="color: #66cc66;">-</span>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<span style="color: #66cc66;">-</span>array<span style="color: #66cc66;">-</span>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<span style="color: #66cc66;">-</span>errors <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">/</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>when <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: #66cc66;">/</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 try 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>
	</channel>
</rss>
