<?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/"
	>

<channel>
	<title>A Rant A Day</title>
	<atom:link href="http://arantaday.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://arantaday.com/blog</link>
	<description>My Musings and Diatribes about the World</description>
	<pubDate>Wed, 10 Dec 2008 12:58:10 +0000</pubDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The Power of The Functional or: I&#8217;ll bet you can&#8217;t do this in Java</title>
		<link>http://arantaday.com/blog/the-power-of-the-functional/</link>
		<comments>http://arantaday.com/blog/the-power-of-the-functional/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 02:37:50 +0000</pubDate>
		<dc:creator>smanek</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[functional]]></category>

		<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://arantaday.com/blog/?p=76</guid>
		<description><![CDATA[Sorry for the longer-than-planned hiatus. I just got a bit busy with school and my job search (I ended up at an AI company where I&#8217;ll be programming mostly Java. Life has certainly got a sense of irony).
Before we get started I just want to say I know you should, theoretically, be able to do [...]]]></description>
			<content:encoded><![CDATA[<p>Sorry for the longer-than-planned hiatus. I just got a bit busy with school and my job search (I ended up at an AI company where I&#8217;ll be programming mostly Java. Life has certainly got a sense of irony).</p>
<p>Before we get started I just want to say I know you should, theoretically, be able to do all of these in Java. Java, like most languages, is <a href="http://en.wikipedia.org/wiki/Turing_complete" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/Turing_complete');">Turing Complete</a> - and therefore is capable of computing every computable function. However, completing these following feats in Java (or any other non-functionalish language) would be much uglier and more difficult (just like it is theoretically possible to write a web application in assembly code; but really, who would want to?). I&#8217;m moderately proficient in Java, and I can&#8217;t think of any way to do these without resorting to hacks like making pseudo-closures by hiding methods inside objects - and even that isn&#8217;t anywhere near as general and flexible as the code presented here.</p>
<p>I plan on discussing three techniques today: sorting (<a href="http://en.wikipedia.org/wiki/Quicksort" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/Quicksort');">quicksort</a>) on an arbitrary comparison (i.e., <a href="http://en.wikipedia.org/wiki/First_Class_Functions" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/First_Class_Functions');">first class functions</a>), automatic <a href="http://en.wikipedia.org/wiki/Memoization" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/Memoization');">memoization</a>, and <a href="http://en.wikipedia.org/wiki/Lazy_evaluation" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/Lazy_evaluation');">lazy evalution</a>. I wish I could say I invented some of these techniques, but all the code presented here is at least inspired by <a href="http://www.amazon.com/Structure-Interpretation-Computer-Programs-Engineering/dp/0262011530" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.amazon.com/Structure-Interpretation-Computer-Programs-Engineering/dp/0262011530');">The Structure and Interpretation of Computer Programming</a> and <a href="http://www.amazon.com/Paradigms-Artificial-Intelligence-Programming-Studies/dp/1558601910" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.amazon.com/Paradigms-Artificial-Intelligence-Programming-Studies/dp/1558601910');">Paradigms of Artificial Intelligence Programming</a> (both highly recommended). If you&#8217;ve read both, you probably won&#8217;t learn anything new from this post - but if you haven&#8217;t I can at least give you a taste of the great masters&#8217; works.</p>
<p>I&#8217;ve chosen to write all my code in Common Lisp, just because that&#8217;s what I like. It should be relatively straightforward to translate my code to Python, Ruby, or any other language with first class functions. I&#8217;ll also make a point to explain my code so non-lispers should be able to follow along.</p>
<p><strong>Quicksort</strong></p>
<p>I&#8217;m assuming if you&#8217;re reading this you&#8217;re a programmer and you are, or at least were, familiar with Quicksort. As a quick refresher, if you were sorting a list in ascending order you would randomly choose a pivot element and place all the elements less than or equal to the pivot on its left and all the items greater than the pivot to its right. You then recursively call the quicksort function on the left and right sublists. On average, it runs in <img src="http://arantaday.com/blog/wp-content/cache/tex_8f1bbb5f5c8a76d0d83f0cdab5eb3bf8.gif" align="absmiddle" class="tex" alt="O(n\log(n))" /> time, but if you get unlucky choosing your pivots it can be as poor as <img src="http://arantaday.com/blog/wp-content/cache/tex_9f84a66d88d24c3b1bc91df5b5346a13.gif" align="absmiddle" class="tex" alt="O(n^2)" />.</p>
<p>My implementation of a quicksort is extremely naive (always choosing the first element as the pivot, doing a lot of unnecessary memory allocation (consing), iterating over the list twice to split it on the pivot, etc.) but it will do for pedagogical purposes.</p>
<p>So, first the code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="lisp lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> quicksort <span style="color: #66cc66;">&#40;</span>data comparison<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> data<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      data
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span><span style="color: #66cc66;">*</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>pivot <span style="color: #66cc66;">&#40;</span>first data<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span>predicate <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: #b1b100;">funcall</span> comparison pivot x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">append</span> <span style="color: #66cc66;">&#40;</span>quicksort <span style="color: #66cc66;">&#40;</span>remove<span style="color: #66cc66;">-</span><span style="color: #b1b100;">if</span> predicate <span style="color: #66cc66;">&#40;</span>rest data<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> comparison<span style="color: #66cc66;">&#41;</span>
	         <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> pivot<span style="color: #66cc66;">&#41;</span>
		 <span style="color: #66cc66;">&#40;</span>quicksort <span style="color: #66cc66;">&#40;</span>remove<span style="color: #66cc66;">-</span>if<span style="color: #66cc66;">-</span><span style="color: #b1b100;">not</span> predicate <span style="color: #66cc66;">&#40;</span>rest data<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> comparison<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></pre></td></tr></table></div>

<p>For you non-lispers, that defines a function (named quicksort) that takes two parameters, &#8220;data&#8221; and &#8220;comparison,&#8221; and sorts the data so that the comparison function holds true for any two adjacent elements in &#8220;data.&#8221; For example, if data = (1 2 3 4) and comparison = &#8216;&lt;&#8217;, then the data is already sorted since <img src="http://arantaday.com/blog/wp-content/cache/tex_29188149d264241b9efe67cef7c036a0.gif" align="absmiddle" class="tex" alt="1&lt;2" />,<img src="http://arantaday.com/blog/wp-content/cache/tex_cefdac7331cc9e8e4f2f9b54e030ab9b.gif" align="absmiddle" class="tex" alt="2&lt;3" />, and <img src="http://arantaday.com/blog/wp-content/cache/tex_1e43b7a930577fba9c591b649edd86e3.gif" align="absmiddle" class="tex" alt="3&lt;4" />.</p>
<p>The nice part about this function is that it can sort the data by any arbitrary criteria. For example, it can sort strings alphabetically, numbers in ascending or descending order, or numbers by absolute value. Here&#8217;s a few quick examples:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp lisp" style="font-family:monospace;">CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>quicksort <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> #'<span style="color: #66cc66;">&gt;</span><span style="color: #808080; font-style: italic;">;)</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>quicksort <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> #'<span style="color: #66cc66;">&lt;</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: #cc66cc;">8</span> <span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#41;</span>
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>quicksort <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #ff0000;">&quot;Tiger&quot;</span> <span style="color: #ff0000;">&quot;Grape&quot;</span> <span style="color: #ff0000;">&quot;Red&quot;</span> <span style="color: #ff0000;">&quot;Apple&quot;</span> <span style="color: #ff0000;">&quot;Zebra&quot;</span> <span style="color: #ff0000;">&quot;Cat&quot;</span> <span style="color: #ff0000;">&quot;Bear&quot;</span> <span style="color: #66cc66;">&#41;</span> #'string<span style="color: #66cc66;">&lt;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Apple&quot;</span> <span style="color: #ff0000;">&quot;Bear&quot;</span> <span style="color: #ff0000;">&quot;Cat&quot;</span> <span style="color: #ff0000;">&quot;Grape&quot;</span> <span style="color: #ff0000;">&quot;Red&quot;</span> <span style="color: #ff0000;">&quot;Tiger&quot;</span> <span style="color: #ff0000;">&quot;Zebra&quot;</span><span style="color: #66cc66;">&#41;</span>
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>quicksort <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">32</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">21</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">12</span> <span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> #'<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>a b<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">abs</span> a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">abs</span> b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">12</span> <span style="color: #cc66cc;">21</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">32</span> <span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>In a non-functional language it&#8217;s easy to write a sort function that works on any one of those criteria, but it&#8217;s far more difficult to write one general enough to sort in any way (even ways you don&#8217;t originally anticipate).</p>
<p><strong>Memoization</strong></p>
<p>Memoization isn&#8217;t as generally well known as quicksort, but the idea is simple: cache the results of previous function calls so that you don&#8217;t have to recompute values you have already computed. The canonical poster child for memoization is the <a href="http://en.wikipedia.org/wiki/Fibonacci_sequence" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/Fibonacci_sequence');">Fibonacci sequence</a>.</p>
<p>The Fibonacci sequence is defined by the recurrence relation:<br />
<center><img src="http://arantaday.com/blog/wp-content/cache/tex_ceeb5b5a387ba1aea6084c7abe74cef8.gif" align="absmiddle" class="tex" alt=" F(0) = 0\\<br />
F(1) = 1\\<br />
F(n) = F(n-1) + F(n-2)" /></center> </p>
<p>As you might guess, it&#8217;s heavily recursive and often calls itself with the same arguments. For example, in order to computer the 40th Fibonacci number naively you have to computer the 2nd Fibonacci number 63,245,986 times, and the 5th Fibonacci number 14,930,352 times. A naive lisp function to compute the nth Fibonacci number might be:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="lisp lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> fibonacci <span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;</span> n <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
      n
      <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</span> <span style="color: #66cc66;">&#40;</span>fibonacci <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">-</span> n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>fibonacci <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">-</span> n <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: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>To see it in use, we can use the Common Lisp debugging function <a href="http://www.lispworks.com/documentation/HyperSpec/Body/m_tracec.htm" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.lispworks.com/documentation/HyperSpec/Body/m_tracec.htm');">trace</a> to show all the recursive function calls and the benchmarking function <a href="http://www.lispworks.com/documentation/HyperSpec/Body/m_time.htm" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.lispworks.com/documentation/HyperSpec/Body/m_time.htm');">time</a> as a gross performance measure.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp lisp" style="font-family:monospace;">CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>time <span style="color: #66cc66;">&#40;</span>fibonacci <span style="color: #cc66cc;">40</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
Evaluation took:
  <span style="color: #cc66cc;">6.385</span> seconds of real time
  <span style="color: #cc66cc;">5.000313</span> seconds of total run time <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4.968311</span> user, <span style="color: #cc66cc;">0.032002</span> system<span style="color: #66cc66;">&#41;</span>
  <span style="color: #cc66cc;">78.31</span><span style="color: #66cc66;">%</span> CPU
  <span style="color: #cc66cc;">11</span>,<span style="color: #cc66cc;">673</span>,<span style="color: #cc66cc;">458</span>,046 processor cycles
  0 bytes consed
<span style="color: #cc66cc;">102334155</span>
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>trace fibonacci<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>FIBONACCI<span style="color: #66cc66;">&#41;</span>
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>fibonacci <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>
  0: <span style="color: #66cc66;">&#40;</span>FIBONACCI <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #cc66cc;">1</span>: <span style="color: #66cc66;">&#40;</span>FIBONACCI <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #cc66cc;">2</span>: <span style="color: #66cc66;">&#40;</span>FIBONACCI <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #cc66cc;">3</span>: <span style="color: #66cc66;">&#40;</span>FIBONACCI <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #cc66cc;">4</span>: <span style="color: #66cc66;">&#40;</span>FIBONACCI <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #cc66cc;">4</span>: <span style="color: #555;">FIBONACCI</span> returned <span style="color: #cc66cc;">1</span>
          <span style="color: #cc66cc;">4</span>: <span style="color: #66cc66;">&#40;</span>FIBONACCI 0<span style="color: #66cc66;">&#41;</span>
          <span style="color: #cc66cc;">4</span>: <span style="color: #555;">FIBONACCI</span> returned 0
        <span style="color: #cc66cc;">3</span>: <span style="color: #555;">FIBONACCI</span> returned <span style="color: #cc66cc;">1</span>
        <span style="color: #cc66cc;">3</span>: <span style="color: #66cc66;">&#40;</span>FIBONACCI <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #cc66cc;">3</span>: <span style="color: #555;">FIBONACCI</span> returned <span style="color: #cc66cc;">1</span>
      <span style="color: #cc66cc;">2</span>: <span style="color: #555;">FIBONACCI</span> returned <span style="color: #cc66cc;">2</span>
      <span style="color: #cc66cc;">2</span>: <span style="color: #66cc66;">&#40;</span>FIBONACCI <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #cc66cc;">3</span>: <span style="color: #66cc66;">&#40;</span>FIBONACCI <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #cc66cc;">3</span>: <span style="color: #555;">FIBONACCI</span> returned <span style="color: #cc66cc;">1</span>
        <span style="color: #cc66cc;">3</span>: <span style="color: #66cc66;">&#40;</span>FIBONACCI 0<span style="color: #66cc66;">&#41;</span>
        <span style="color: #cc66cc;">3</span>: <span style="color: #555;">FIBONACCI</span> returned 0
      <span style="color: #cc66cc;">2</span>: <span style="color: #555;">FIBONACCI</span> returned <span style="color: #cc66cc;">1</span>
    <span style="color: #cc66cc;">1</span>: <span style="color: #555;">FIBONACCI</span> returned <span style="color: #cc66cc;">3</span>
    <span style="color: #cc66cc;">1</span>: <span style="color: #66cc66;">&#40;</span>FIBONACCI <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #cc66cc;">2</span>: <span style="color: #66cc66;">&#40;</span>FIBONACCI <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #cc66cc;">3</span>: <span style="color: #66cc66;">&#40;</span>FIBONACCI <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #cc66cc;">3</span>: <span style="color: #555;">FIBONACCI</span> returned <span style="color: #cc66cc;">1</span>
        <span style="color: #cc66cc;">3</span>: <span style="color: #66cc66;">&#40;</span>FIBONACCI 0<span style="color: #66cc66;">&#41;</span>
        <span style="color: #cc66cc;">3</span>: <span style="color: #555;">FIBONACCI</span> returned 0
      <span style="color: #cc66cc;">2</span>: <span style="color: #555;">FIBONACCI</span> returned <span style="color: #cc66cc;">1</span>
      <span style="color: #cc66cc;">2</span>: <span style="color: #66cc66;">&#40;</span>FIBONACCI <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #cc66cc;">2</span>: <span style="color: #555;">FIBONACCI</span> returned <span style="color: #cc66cc;">1</span>
    <span style="color: #cc66cc;">1</span>: <span style="color: #555;">FIBONACCI</span> returned <span style="color: #cc66cc;">2</span>
  0: <span style="color: #555;">FIBONACCI</span> returned <span style="color: #cc66cc;">5</span>
<span style="color: #cc66cc;">5</span></pre></div></div>

<p>Not too bad &#8230; but even computing the 5th Fibonacci number required a lot of repeated function calls (and the 40th required approximately 102,334,114 repeated calls). </p>
<p>Fortunately, we can use the power of functional languages to write a meta-function (a function that operates on functions) that will automatically and transparently transform any function into a memoized-function. Therefore, for example, every time (after the first) that we try to look up the nth Fibonacci number, the result is just looked up in a hash table instead of being recomputed. In most real world applications (web-apps, business logic, etc) we would also add some logic to &#8216;expire&#8217; answers in the cache after a certain amount of time has elapsed (or certain triggers have occurred).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="lisp lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> make<span style="color: #66cc66;">-</span>memoize<span style="color: #66cc66;">-</span>fn <span style="color: #66cc66;">&#40;</span>fn<span style="color: #66cc66;">&#41;</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>cache <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>hash<span style="color: #66cc66;">-</span>table<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;">lambda</span> <span style="color: #66cc66;">&#40;</span>input<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>multiple<span style="color: #66cc66;">-</span>value<span style="color: #66cc66;">-</span>bind <span style="color: #66cc66;">&#40;</span>output found<span style="color: #66cc66;">-</span>p<span style="color: #66cc66;">&#41;</span>
	  <span style="color: #66cc66;">&#40;</span>gethash input cache<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> found<span style="color: #66cc66;">-</span>p
	    output
	    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">&#40;</span>gethash input cache<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> fn input<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><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> memoize <span style="color: #66cc66;">&#40;</span>fn<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">&#40;</span>symbol<span style="color: #66cc66;">-</span><span style="color: #b1b100;">function</span> fn<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>memoize<span style="color: #66cc66;">-</span>fn <span style="color: #66cc66;">&#40;</span>symbol<span style="color: #66cc66;">-</span><span style="color: #b1b100;">function</span> fn<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></td></tr></table></div>

<p>Then, after just running:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp lisp" style="font-family:monospace;">CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>memoize 'fibonacci<span style="color: #66cc66;">&#41;</span>
#<span style="color: #66cc66;">&lt;</span>CLOSURE <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">LAMBDA</span> <span style="color: #66cc66;">&#40;</span>INPUT<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>AE6A65D<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&gt;</span></pre></div></div>

<p>Our Fibonacci function is automatically memoizing - no rewriting. From now on, each call to the Fibonacci function is just looked up in the hash table. If the result is there, it just returns it without ever doing any work. If the result isn&#8217;t cached in the hash table, it is computed and added to the hash table for future reference. All completely transparently, without ever rewriting Fibonacci.</p>
<p>The performance difference is significant:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp lisp" style="font-family:monospace;">CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>fibonacci <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>
  0: <span style="color: #66cc66;">&#40;</span>FIBONACCI <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #cc66cc;">1</span>: <span style="color: #66cc66;">&#40;</span>FIBONACCI <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #cc66cc;">2</span>: <span style="color: #66cc66;">&#40;</span>FIBONACCI <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #cc66cc;">3</span>: <span style="color: #66cc66;">&#40;</span>FIBONACCI <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #cc66cc;">4</span>: <span style="color: #66cc66;">&#40;</span>FIBONACCI <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #cc66cc;">4</span>: <span style="color: #555;">FIBONACCI</span> returned <span style="color: #cc66cc;">1</span>
          <span style="color: #cc66cc;">4</span>: <span style="color: #66cc66;">&#40;</span>FIBONACCI 0<span style="color: #66cc66;">&#41;</span>
          <span style="color: #cc66cc;">4</span>: <span style="color: #555;">FIBONACCI</span> returned 0
        <span style="color: #cc66cc;">3</span>: <span style="color: #555;">FIBONACCI</span> returned <span style="color: #cc66cc;">1</span>
      <span style="color: #cc66cc;">2</span>: <span style="color: #555;">FIBONACCI</span> returned <span style="color: #cc66cc;">2</span>
    <span style="color: #cc66cc;">1</span>: <span style="color: #555;">FIBONACCI</span> returned <span style="color: #cc66cc;">3</span>
  0: <span style="color: #555;">FIBONACCI</span> returned <span style="color: #cc66cc;">5</span>
<span style="color: #cc66cc;">5</span>
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>untrace fibonacci<span style="color: #66cc66;">&#41;</span>
T
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>time <span style="color: #66cc66;">&#40;</span>fibonacci <span style="color: #cc66cc;">40</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
Evaluation took:
  <span style="color: #cc66cc;">0.000</span> seconds of real time
  <span style="color: #cc66cc;">0.000000</span> seconds of total run time <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.000000</span> user, <span style="color: #cc66cc;">0.000000</span> system<span style="color: #66cc66;">&#41;</span>
  <span style="color: #cc66cc;">100.00</span><span style="color: #66cc66;">%</span> CPU
  <span style="color: #cc66cc;">19</span>,<span style="color: #cc66cc;">174</span> processor cycles
  0 bytes consed
<span style="color: #cc66cc;">102334155</span>
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>time <span style="color: #66cc66;">&#40;</span>fibonacci <span style="color: #cc66cc;">40</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
Evaluation took:
  <span style="color: #cc66cc;">0.000</span> seconds of real time
  <span style="color: #cc66cc;">0.000000</span> seconds of total run time <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.000000</span> user, <span style="color: #cc66cc;">0.000000</span> system<span style="color: #66cc66;">&#41;</span>
  <span style="color: #cc66cc;">100.00</span><span style="color: #66cc66;">%</span> CPU
  <span style="color: #cc66cc;">2</span> processor cycles
  0 bytes consed
<span style="color: #cc66cc;">102334155</span></pre></div></div>

<p>Even the first time it computed the 40th Fibonacci number, it was over 600,000 times faster. And the second time it only took 2 processor cycles (far less than I, personally, expected). And the truly amazing part is that we can make any other function memoizing just by calling the meta-function memoize on it. No having to rewrite any code to implement caching!</p>
<p><strong>Lazy Evaluation</strong></p>
<p>With Lazy Evaluation, an expression isn&#8217;t evaluated as soon as it is created; instead it is delayed until that expression&#8217;s value is needed. One of the neat tricks provided by lazy evaluation is the ability to work with infinite sets - for example, you can literally have a variable which holds all the natural numbers, all the primes, or all the Fibonacci numbers. This sometimes provides rather surprising results - such as the ability to <a href="http://math.andrej.com/2007/09/28/seemingly-impossible-functional-programs/" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://math.andrej.com/2007/09/28/seemingly-impossible-functional-programs/');">exhaustively search over infinite spaces</a>.</p>
<p>I&#8217;ll settle for just writing a bare bones lazy evaluation system. I won&#8217;t provide many of the utility functions (map, iterators, etc.) that could prove useful in the real world, but I will build enough so that we can, for example, have a variable which holds all the primes (lazily evaluated, of course).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="lisp lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> make<span style="color: #66cc66;">-</span>pipe <span style="color: #66cc66;">&#40;</span>head tail<span style="color: #66cc66;">&#41;</span>
  `<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> ,head <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> ,tail<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;">;;the lambda is delaying the evaluation</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> head <span style="color: #66cc66;">&#40;</span>pipe<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> pipe<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> tail <span style="color: #66cc66;">&#40;</span>pipe<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>functionp <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> pipe<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> pipe<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> pipe<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;">cdr</span> pipe<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> pipe<span style="color: #66cc66;">-</span>elt <span style="color: #66cc66;">&#40;</span>pipe n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">zerop</span> n<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>head pipe<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>pipe<span style="color: #66cc66;">-</span>elt <span style="color: #66cc66;">&#40;</span>tail pipe<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">-</span> n<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> integers <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&amp;</span>key <span style="color: #66cc66;">&#40;</span>start 0<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>end <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">or</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null</span> end<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;=</span> start end<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>pipe start <span style="color: #66cc66;">&#40;</span>integers :<span style="color: #555;">start</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">+</span> start<span style="color: #66cc66;">&#41;</span> :<span style="color: #555;">end</span> end<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>That should be enough to get us started. The make-pipe macro creates a lazily evaluated list (a &#8216;pipe&#8217;, as per Norvig) whose last element is a closure over a function that can be evaluated to get another actual element (we use the closure to delay evaluation, up until we call the lambda). We then define two convenience functions: pipe-elt, which will evaluate a pipe up to a particular element (e.g., compute the first N integers) and integers which can create a list of (possibly infinite) integers.</p>
<p>A few simple examples of our basic system at use:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp lisp" style="font-family:monospace;">CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>defvar <span style="color: #66cc66;">*</span>short<span style="color: #66cc66;">-</span>lazy<span style="color: #66cc66;">*</span> <span style="color: #66cc66;">&#40;</span>integers :<span style="color: #555;">start</span> 0 :<span style="color: #555;">end</span> <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">*</span>SHORT<span style="color: #66cc66;">-</span>LAZY<span style="color: #66cc66;">*</span>
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">*</span>short<span style="color: #66cc66;">-</span>lazy<span style="color: #66cc66;">*</span>
<span style="color: #66cc66;">&#40;</span>0 . #<span style="color: #66cc66;">&lt;</span>CLOSURE <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">LAMBDA</span> #<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>B1D0D05<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span>
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>pipe<span style="color: #66cc66;">-</span>elt <span style="color: #66cc66;">*</span>short<span style="color: #66cc66;">-</span>lazy<span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #cc66cc;">3</span>
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">*</span>short<span style="color: #66cc66;">-</span>lazy<span style="color: #66cc66;">*</span>
<span style="color: #66cc66;">&#40;</span>0 <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> . #<span style="color: #66cc66;">&lt;</span>CLOSURE <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">LAMBDA</span> #<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>B42B795<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span>
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>pipe<span style="color: #66cc66;">-</span>elt <span style="color: #66cc66;">*</span>short<span style="color: #66cc66;">-</span>lazy<span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">NIL</span>
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">*</span>short<span style="color: #66cc66;">-</span>lazy<span style="color: #66cc66;">*</span>
<span style="color: #66cc66;">&#40;</span>0 <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>defvar <span style="color: #66cc66;">*</span>infinite<span style="color: #66cc66;">-</span>lazy<span style="color: #66cc66;">*</span> <span style="color: #66cc66;">&#40;</span>integers :<span style="color: #555;">start</span> 0<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">*</span>INFINITE<span style="color: #66cc66;">-</span>LAZY<span style="color: #66cc66;">*</span>
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>pipe<span style="color: #66cc66;">-</span>elt <span style="color: #66cc66;">*</span>infinite<span style="color: #66cc66;">-</span>lazy<span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #cc66cc;">5</span>
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">*</span>infinite<span style="color: #66cc66;">-</span>lazy<span style="color: #66cc66;">*</span>
<span style="color: #66cc66;">&#40;</span>0 <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">5</span> . #<span style="color: #66cc66;">&lt;</span>CLOSURE <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">LAMBDA</span> #<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>B71B7E5<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span>
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>pipe<span style="color: #66cc66;">-</span>elt <span style="color: #66cc66;">*</span>infinite<span style="color: #66cc66;">-</span>lazy<span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #cc66cc;">20</span>
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">*</span>infinite<span style="color: #66cc66;">-</span>lazy<span style="color: #66cc66;">*</span>
<span style="color: #66cc66;">&#40;</span>0 <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">10</span> <span style="color: #cc66cc;">11</span> <span style="color: #cc66cc;">12</span> <span style="color: #cc66cc;">13</span> <span style="color: #cc66cc;">14</span> <span style="color: #cc66cc;">15</span> <span style="color: #cc66cc;">16</span> <span style="color: #cc66cc;">17</span> <span style="color: #cc66cc;">18</span> <span style="color: #cc66cc;">19</span> <span style="color: #cc66cc;">20</span>
 . #<span style="color: #66cc66;">&lt;</span>CLOSURE <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">LAMBDA</span> #<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>B7C8DD5<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>We could get the hundredth, thousandth, or trillionth entry from *infinite-lazy*, and they would all be there.</p>
<p>Well, so far this is interesting - but not really useful. But we&#8217;ve laid the foundation for the really cool stuff. My goal for this exercise will be to create an infinite list of all the primes using the <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes');">Sieve of Eratosthenes</a>. Basically, by applying filters to the closure on the end of the lazy list (i.e. function composition) that &#8216;cross off&#8217; multiples of numbers already evaluated, we can generate an infinite list of primes (I used this very trick on several <a href="http://projecteuler.net/index.php?section=problems" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://projecteuler.net/index.php?section=problems');">Project Euler</a> problems).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="lisp lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> filter <span style="color: #66cc66;">&#40;</span>predicate pipe<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> predicate <span style="color: #66cc66;">&#40;</span>head pipe<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>pipe <span style="color: #66cc66;">&#40;</span>head pipe<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>filter predicate <span style="color: #66cc66;">&#40;</span>tail pipe<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>filter predicate <span style="color: #66cc66;">&#40;</span>tail pipe<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> lazy<span style="color: #66cc66;">-</span>eratosthenes <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&amp;</span>optional <span style="color: #66cc66;">&#40;</span>pipe <span style="color: #66cc66;">&#40;</span>integers :<span style="color: #555;">start</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: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>pipe <span style="color: #66cc66;">&#40;</span>head pipe<span style="color: #66cc66;">&#41;</span> 
	     <span style="color: #66cc66;">&#40;</span>filter #'<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>p<span style="color: #66cc66;">&#41;</span> 
			 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">not</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">zerop</span> <span style="color: #66cc66;">&#40;</span>mod p <span style="color: #66cc66;">&#40;</span>head pipe<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>eratosthenes <span style="color: #66cc66;">&#40;</span>tail pipe<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></pre></td></tr></table></div>

<p>Now, we can simply create, and play with, an infinite list of primes:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp lisp" style="font-family:monospace;">CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>defvar <span style="color: #66cc66;">*</span>lazy<span style="color: #66cc66;">-</span>primes<span style="color: #66cc66;">*</span> <span style="color: #66cc66;">&#40;</span>lazy<span style="color: #66cc66;">-</span>eratosthenes<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">*</span>LAZY<span style="color: #66cc66;">-</span>PRIMES<span style="color: #66cc66;">*</span>
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">*</span>lazy<span style="color: #66cc66;">-</span>primes<span style="color: #66cc66;">*</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span> . #<span style="color: #66cc66;">&lt;</span>CLOSURE <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">LAMBDA</span> #<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>B60E975<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span>
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>pipe<span style="color: #66cc66;">-</span>elt <span style="color: #66cc66;">*</span>lazy<span style="color: #66cc66;">-</span>primes<span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #cc66cc;">13</span>
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">*</span>lazy<span style="color: #66cc66;">-</span>primes<span style="color: #66cc66;">*</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">11</span> <span style="color: #cc66cc;">13</span> . #<span style="color: #66cc66;">&lt;</span>CLOSURE <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">LAMBDA</span> #<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>B07FB45<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span>
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>pipe<span style="color: #66cc66;">-</span>elt <span style="color: #66cc66;">*</span>lazy<span style="color: #66cc66;">-</span>primes<span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #cc66cc;">547</span>
CL<span style="color: #66cc66;">-</span>USER<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">*</span>lazy<span style="color: #66cc66;">-</span>primes<span style="color: #66cc66;">*</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">11</span> <span style="color: #cc66cc;">13</span> <span style="color: #cc66cc;">17</span> <span style="color: #cc66cc;">19</span> <span style="color: #cc66cc;">23</span> <span style="color: #cc66cc;">29</span> <span style="color: #cc66cc;">31</span> <span style="color: #cc66cc;">37</span> <span style="color: #cc66cc;">41</span> <span style="color: #cc66cc;">43</span> <span style="color: #cc66cc;">47</span> <span style="color: #cc66cc;">53</span> <span style="color: #cc66cc;">59</span> <span style="color: #cc66cc;">61</span> <span style="color: #cc66cc;">67</span> <span style="color: #cc66cc;">71</span> <span style="color: #cc66cc;">73</span> <span style="color: #cc66cc;">79</span> <span style="color: #cc66cc;">83</span> <span style="color: #cc66cc;">89</span> <span style="color: #cc66cc;">97</span> <span style="color: #cc66cc;">101</span> <span style="color: #cc66cc;">103</span>
 <span style="color: #cc66cc;">107</span> <span style="color: #cc66cc;">109</span> <span style="color: #cc66cc;">113</span> <span style="color: #cc66cc;">127</span> <span style="color: #cc66cc;">131</span> <span style="color: #cc66cc;">137</span> <span style="color: #cc66cc;">139</span> <span style="color: #cc66cc;">149</span> <span style="color: #cc66cc;">151</span> <span style="color: #cc66cc;">157</span> <span style="color: #cc66cc;">163</span> <span style="color: #cc66cc;">167</span> <span style="color: #cc66cc;">173</span> <span style="color: #cc66cc;">179</span> <span style="color: #cc66cc;">181</span> <span style="color: #cc66cc;">191</span> <span style="color: #cc66cc;">193</span> <span style="color: #cc66cc;">197</span> <span style="color: #cc66cc;">199</span>
 <span style="color: #cc66cc;">211</span> <span style="color: #cc66cc;">223</span> <span style="color: #cc66cc;">227</span> <span style="color: #cc66cc;">229</span> <span style="color: #cc66cc;">233</span> <span style="color: #cc66cc;">239</span> <span style="color: #cc66cc;">241</span> <span style="color: #cc66cc;">251</span> <span style="color: #cc66cc;">257</span> <span style="color: #cc66cc;">263</span> <span style="color: #cc66cc;">269</span> <span style="color: #cc66cc;">271</span> <span style="color: #cc66cc;">277</span> <span style="color: #cc66cc;">281</span> <span style="color: #cc66cc;">283</span> <span style="color: #cc66cc;">293</span> <span style="color: #cc66cc;">307</span> <span style="color: #cc66cc;">311</span> <span style="color: #cc66cc;">313</span>
 <span style="color: #cc66cc;">317</span> <span style="color: #cc66cc;">331</span> <span style="color: #cc66cc;">337</span> <span style="color: #cc66cc;">347</span> <span style="color: #cc66cc;">349</span> <span style="color: #cc66cc;">353</span> <span style="color: #cc66cc;">359</span> <span style="color: #cc66cc;">367</span> <span style="color: #cc66cc;">373</span> <span style="color: #cc66cc;">379</span> <span style="color: #cc66cc;">383</span> <span style="color: #cc66cc;">389</span> <span style="color: #cc66cc;">397</span> <span style="color: #cc66cc;">401</span> <span style="color: #cc66cc;">409</span> <span style="color: #cc66cc;">419</span> <span style="color: #cc66cc;">421</span> <span style="color: #cc66cc;">431</span> <span style="color: #cc66cc;">433</span>
 <span style="color: #cc66cc;">439</span> <span style="color: #cc66cc;">443</span> <span style="color: #cc66cc;">449</span> <span style="color: #cc66cc;">457</span> <span style="color: #cc66cc;">461</span> <span style="color: #cc66cc;">463</span> <span style="color: #cc66cc;">467</span> <span style="color: #cc66cc;">479</span> <span style="color: #cc66cc;">487</span> <span style="color: #cc66cc;">491</span> <span style="color: #cc66cc;">499</span> <span style="color: #cc66cc;">503</span> <span style="color: #cc66cc;">509</span> <span style="color: #cc66cc;">521</span> <span style="color: #cc66cc;">523</span> <span style="color: #cc66cc;">541</span> <span style="color: #cc66cc;">547</span>
 . #<span style="color: #66cc66;">&lt;</span>CLOSURE <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">LAMBDA</span> #<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>B3A785D<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>So, there you have it folks. Next time some <a href="http://www.paulgraham.com/avg.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.paulgraham.com/avg.html');">blub</a> programmer asks you what you need first class functions and closures for, pull out some of these. While I didn&#8217;t develop any of these three ideas beyond the level of parlor-tricks, it doesn&#8217;t take much imagination to extend these simple examples into the real world.</p>
]]></content:encoded>
			<wfw:commentRss>http://arantaday.com/blog/the-power-of-the-functional/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Dropbox Invites for Clever Code</title>
		<link>http://arantaday.com/blog/dropbox-invites-for-clever-code/</link>
		<comments>http://arantaday.com/blog/dropbox-invites-for-clever-code/#comments</comments>
		<pubDate>Sun, 07 Sep 2008 23:12:17 +0000</pubDate>
		<dc:creator>smanek</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[clever]]></category>

		<category><![CDATA[contest]]></category>

		<category><![CDATA[dropbox]]></category>

		<guid isPermaLink="false">http://arantaday.com/blog/?p=57</guid>
		<description><![CDATA[Well, in honor of Dropbox&#8217;s new Linux Client (note: you must have an account to be able to see the Linux client page), I&#8217;ve decided to give out (at least) 3 invitations to Dropbox&#8217;s private beta for (what I deem to be) the cleverest sub-ten-line code snippets posted. If there are many outstanding entries, I&#8217;ll [...]]]></description>
			<content:encoded><![CDATA[<p>Well, in honor of <a href="http://www.getdropbox.com/" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.getdropbox.com/');">Dropbox</a>&#8217;s new <a href="http://www.getdropbox.com/beta?os=linux" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.getdropbox.com/beta?os=linux');">Linux Client</a> (note: you must have an account to be able to see the Linux client page), I&#8217;ve decided to give out (at least) 3 invitations to Dropbox&#8217;s private beta for (what I deem to be) the cleverest sub-ten-line code snippets posted. If there are many outstanding entries, I&#8217;ll be happy to give out more than 3 though.</p>
<p><a href="/files/dropbox-screen.png" target ="_blank"><img src="/files/dropbox-screen.png" alt="Dropbox Linux Client Screenshot"/></a></p>
<p>To give you some idea of what I&#8217;m talking about take a look at these examples:</p>
<p>Schwartzian Transform in Perl:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="perl perl" style="font-family:monospace;"><span style="color: #0000ff;">@sorted</span> <span style="color: #339933;">=</span> <span style="color: #000066;">map</span>  <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">$_</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#125;</span>
          <span style="color: #000066;">sort</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">$a</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">cmp</span> <span style="color: #0000ff;">$b</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#125;</span>
          <span style="color: #000066;">map</span>  <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">,</span> foo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#125;</span>
               <span style="color: #0000ff;">@unsorted</span>;</pre></td></tr></table></div>

<p>A Common Lisp <a href="http://en.wikipedia.org/wiki/Quine_(computing)" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/Quine_(computing)');">Quine</a> (extra whitespace for readability):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="lisp lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</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: #b1b100;">list</span> x <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> '<span style="color: #b1b100;">quote</span> x<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;">lambda</span> <span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> x <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> '<span style="color: #b1b100;">quote</span> x<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></td></tr></table></div>

<p>I have a few better examples in mind too (clever regexes, pointer magic, etc), but I don&#8217;t want to steal anyone&#8217;s real entry <img src='http://arantaday.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> The code you submit doesn&#8217;t have to be of your own invention, but bonus points if it is. Any language and any topic (<a href="http://en.wikipedia.org/wiki/Obfuscated_code#Recreational_obfuscation" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/Obfuscated_code#Recreational_obfuscation');">obfuscated code</a>, <a href="http://en.wikipedia.org/wiki/Polyglot_(computing)" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/Polyglot_(computing)');">polyglots</a>, <a href="http://en.wikipedia.org/wiki/Numerical_methods" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/Numerical_methods');">numerical methods</a>, etc.) are fair game - if I don&#8217;t understand it, I&#8217;ll ask someone smarter than me for help. </p>
<p>I&#8217;ll only consider comments submitted by midnight Wednesday, and I&#8217;ll announce winners within a day of that.</p>
<p>If you feel like entering, please be sure to leave some way for me to contact you so I can get your invite to you if you win.</p>
]]></content:encoded>
			<wfw:commentRss>http://arantaday.com/blog/dropbox-invites-for-clever-code/feed/</wfw:commentRss>
		</item>
		<item>
		<title>My Stupid Site Killing Mistake</title>
		<link>http://arantaday.com/blog/my-stupid-site-killing-mistake/</link>
		<comments>http://arantaday.com/blog/my-stupid-site-killing-mistake/#comments</comments>
		<pubDate>Sun, 07 Sep 2008 20:27:19 +0000</pubDate>
		<dc:creator>smanek</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[mistake]]></category>

		<category><![CDATA[stupid]]></category>

		<category><![CDATA[sysadmin]]></category>

		<guid isPermaLink="false">http://arantaday.com/blog/?p=52</guid>
		<description><![CDATA[Sorry for the slowness for the last few hours.
My webserver was standing up to the load just fine, I just did something stupid.
As some background, I almost always leave a terminal multiplexer (screen) running on my vps. It eats a few megs of memory, but I have enough to spare that it isn&#8217;t an issue.
And [...]]]></description>
			<content:encoded><![CDATA[<p>Sorry for the slowness for the last few hours.</p>
<p>My webserver was standing up to the load just fine, I just did something stupid.</p>
<p>As some background, I almost always leave a terminal multiplexer (<a href="http://www.gnu.org/software/screen/" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.gnu.org/software/screen/');">screen</a>) running on my vps. It eats a few megs of memory, but I have enough to spare that it isn&#8217;t an issue.</p>
<p>And sometimes, when I&#8217;m bored, I&#8217;ll run a command like:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #007800;">$tail</span> <span style="color: #660033;">-f</span> apache_access_log.log| <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">'GET article-url'</span></pre></div></div>

<p>It basically shows me real time access to a particular url - every time someone reads that article I see their <a href="http://en.wikipedia.org/wiki/User_agent" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/User_agent');">user-agent</a> string, referrer, and a bit of other useful information. </p>
<p>Of course I have real analytics software too, but sometimes it&#8217;s just fun to watch the hits scroll by - it&#8217;s like a pretty matrix-esque screen saver.</p>
<p>Well, I was doing that this morning, and then I left my computer. I disconnected my screen session and ssh, and went about life in the real world. When I returned ~6 hours later, I noticed my brand new blog was down. Uh-oh. </p>
<p>After connecting to a console to investigate (ssh was timing out), I was greeted by this beautiful sight:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#uptime</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>...<span style="color: #7a0874; font-weight: bold;">&#93;</span>load average: <span style="color: #000000;">20.84</span>, <span style="color: #000000;">22.16</span>, <span style="color: #000000;">23.84</span></pre></div></div>

<p>For the record, that&#8217;s bad. I don&#8217;t think I&#8217;ve ever seen a load that high on a single cpu machine &#8230;</p>
<p>I was particularly surprised since I had spent a few hours the previous day setting up the site (caching, php compiling, tweaking apache/mysql, etc) to handle slashdot level loads. I had simulated well over a 1000 concurrent users without a hiccup.</p>
<p>After looking at ps, I realized that my little log trick was the culprit. I killed the job, and load dropped right down to 0. </p>
<p>I&#8217;m still looking into why it pegged the CPU so hard though - I&#8217;ve left that same command running for hours on a terminal I&#8217;m connected to with no discernible effect. My best guess is having that much information going to STDOUT in a disconnected screen session triggered some sort of pathological edge case &#8230; I&#8217;ll investigate more thoroughly tonight.</p>
<p>Anyways, my apologies to anyone who tried to access the site when it&#8217;s down. I&#8217;m always the first person to criticize twitter when I hear it&#8217;s down (again) - maybe there&#8217;s something is to be said about glass houses &#8230;</p>
<p>Has anyone else ever made a stupid mistake like this that brought a server to its knees? If so, I&#8217;d love to hear about it. I think I need a little pick me up <img src='http://arantaday.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://arantaday.com/blog/my-stupid-site-killing-mistake/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The New Classics of Computer Science</title>
		<link>http://arantaday.com/blog/the-new-classics/</link>
		<comments>http://arantaday.com/blog/the-new-classics/#comments</comments>
		<pubDate>Sun, 07 Sep 2008 04:36:36 +0000</pubDate>
		<dc:creator>smanek</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[book]]></category>

		<category><![CDATA[review]]></category>

		<guid isPermaLink="false">http://arantaday.com/blog/?p=30</guid>
		<description><![CDATA[Hello. Welcome to the second installment of A Rant a Day.
We all know about the classic texts of computer science (although, we may be a bit behind on our reading). We&#8217;ve heard a thousand times how important Knuth&#8217;s Art of Computer Programming, Abelson and Sussman&#8217;s Structure and Interpretation of Computer Programming, and the Gang of [...]]]></description>
			<content:encoded><![CDATA[<p>Hello. Welcome to the second installment of A Rant a Day.</p>
<p>We all know about the classic texts of computer science (although, we may be a bit behind on our reading). We&#8217;ve heard a thousand times how important Knuth&#8217;s <a href="http://en.wikipedia.org/wiki/The_art_of_computer_programming" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/The_art_of_computer_programming');">Art of Computer Programming</a>, Abelson and Sussman&#8217;s <a href="http://en.wikipedia.org/wiki/Structure_and_Interpretation_of_Computer_Programs" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/Structure_and_Interpretation_of_Computer_Programs');">Structure and Interpretation of Computer Programming</a>, and the Gang of Four&#8217;s book on <a href="http://en.wikipedia.org/wiki/Design_Patterns" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/Design_Patterns');">Design Patterns</a> are.</p>
<p>But, I think these are sometime overemphasized - often to the neglect of other great books that have been written in the last few decades. Recently, I moved from Chicago to Cambridge and could only bring a few dozen books with me. I was forced to evaluate which are truly timeless classics, and which are just tutorials. </p>
<p>Now, don&#8217;t get me wrong - tutorials are great and do serve their purpose. I often reference <a href="http://www.amazon.com/Programming-Perl-3rd-Larry-Wall/dp/0596000278/" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.amazon.com/Programming-Perl-3rd-Larry-Wall/dp/0596000278/');">Programming Perl</a> or <a href="http://www.gigamonkeys.com/book/" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.gigamonkeys.com/book/');">Practical Common Lisp</a>. But they don&#8217;t change the way you think - they don&#8217;t have an impact which spans disciplines and languages and technologies.</p>
<p>When I sat down and thought about it, many of the books that have had the greatest impact on me as a programmer (and as a thinker in general) aren&#8217;t even Computer Science books. These books below often transcend their stated domain and impart a new way of looking at all problems.</p>
<ul>
<li><strong><a href="http://www.amazon.com/Godel-Escher-Bach-Eternal-Golden/dp/0465026567" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.amazon.com/Godel-Escher-Bach-Eternal-Golden/dp/0465026567');">Godel, Escher, Bach: An Eternal Golden Braid</a></strong></li>
<p>&nbsp;&nbsp;&nbsp;GEB is ostensibly about the hidden threads that hold together Mathematics, Art, and Music. But Hofstadter goes so much deeper than that. Through a series of Socratic Dialogues, the nature of thought and computation are revealed to us. And, along the way, this book touches on recursion, formal systems, lambda calculus, undecidability, complexity theory, and far more. If you only read one book on this list, it should be this one.</p>
<li><strong><a href="http://www.amazon.com/New-Turing-Omnibus-Sixty-Six-Excursions/dp/0805071660" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.amazon.com/New-Turing-Omnibus-Sixty-Six-Excursions/dp/0805071660');">The New Turing Omnibus</a></strong></li>
<p>&nbsp;&nbsp;&nbsp;This book is extraordinary for the sheer breadth of topics it covers. I&#8217;ve noticed that many programmers stagnate and grow complacent with their existing tool set. They master a few tricks, and then just plateau - whenever any problem arises, they reach for a technique from their limited repertoire and think it&#8217;s the best solution. As the old cliche goes, when the only tool you have is a hammer, every problem looks like a nail.</p>
<p>&nbsp;&nbsp;&nbsp;The new Turing Omnibus opens your eyes to new worlds. Everything from genetic algorithms, to pushdown automata, to computer vision techniques are fair game. This book doesn&#8217;t go into enough depth in any topic to actually teach you how to do anything - but it will teach what is possible, and will help you recognize the best tool for the job. You won&#8217;t understand the details of simulated annealing or the simplex algorithm, but when you encounter a new problem you will know the right approach to take, and will having a jumping off point for your research. </p>
<li><strong><a href="http://www.amazon.com/Introduction-Algorithms-Thomas-H-Cormen/dp/0262032937" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.amazon.com/Introduction-Algorithms-Thomas-H-Cormen/dp/0262032937');">Introduction to Algorithms</a></strong></li>
<p>&nbsp;&nbsp;&nbsp;This, to me, is the new Art of Computer Programming. Although it is nice to learn efficient arithmetic algorithms using assembly on a MIX machine, it really isn&#8217;t what I&#8217;d like to focus on. I&#8217;d rather learn, for example, about an extremely clever disjoint set data structure which provides <img src="http://arantaday.com/blog/wp-content/cache/tex_e986233711d3cb8ca5720d95837ce82f.gif" align="absmiddle" class="tex" alt="O(\alpha(n))" /> unions (where <img src="http://arantaday.com/blog/wp-content/cache/tex_29e22701a5d5dfb8208f271ff59d5bdf.gif" align="absmiddle" class="tex" alt="\alpha(n)=A^{-1}(n,n)" /> and <img src="http://arantaday.com/blog/wp-content/cache/tex_7fc56270e7a70fa81a5935b72eacbe29.gif" align="absmiddle" class="tex" alt="A" /> is the <a href="http://en.wikipedia.org/wiki/Ackermann_function" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/Ackermann_function');"> Ackermann function</a>) in high level pseudo-code.  This book showed me a lot of novel techniques for proving an algorithm&#8217;s runtime and it has become my bible for elementary algorithms.</p>
<li><strong><a href="http://www.amazon.com/Algebra-Geometry-Alan-F-Beardon/dp/052181362X" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.amazon.com/Algebra-Geometry-Alan-F-Beardon/dp/052181362X');">Algebra and Geometry</a></strong></li>
<p>&nbsp;&nbsp;&nbsp;First, this isn&#8217;t your high school algebra/geometry book (well, I suppose <i>you</i> could have done this in high school, but I didn&#8217;t at least). It is focused, primarily, on abstract algebra, linear algebra, and non-euclidean geometry. By the time I read this book, I had already taken courses in all those topics individually. But this book tied up a lot of loose ends and showed me many interconnections between seemingly disparate fields that I hadn&#8217;t picked up on on my own. It may be a little too intense for a first foray into higher math thought (at least I would have struggled a lot more, if I wasn&#8217;t familiar with so many of the topics going in), but it&#8217;s definitely a worthwhile read.</p>
<li><strong><a href="http://www.amazon.com/Road-Reality-Complete-Guide-Universe/dp/0679776311" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.amazon.com/Road-Reality-Complete-Guide-Universe/dp/0679776311');">The Road to Reality</a></strong></li>
<p>&nbsp;&nbsp;&nbsp;The first half of this book is, hands down, the most incredible math tutorial I&#8217;ve ever seen. In the span of a few hundred pages, the author takes a reader up from elementary algebra and arithmetic all the way through calculus and large swathes of analysis, abstract algebra, number theory, geometry, and basically the better part of an undergraduate math education. After that, Penrose uses your newly minted math skills to teach physics from simple Newtonian mechanics, up through quantum mechanics and relativity, and even touching on some cutting edge work currently taking place on a &#8220;theory of everything.&#8221; Because of the tremendous amount of ground this book covers, it is extremely difficult to get through. Over the course of about a year, I often found myself giving up on exercises, and just glossing over some of the more intense sections. Reading this book can best be described as attempting to drink from a firehose: if even 20% of what Penrose throws at you sticks, you&#8217;ll be a better man for it. The math skills that this book imparts are alone worth the price of admission. Throw in the newfound understanding of all things physical, and this book is bargain for what you put in.
</ul>
<p>Those are a few of my favorite books, that are off the beaten track. If you have any to add, please leave a note. I&#8217;m already behind on my reading, but nothing like a bit of guilt to motivate me <img src='http://arantaday.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://arantaday.com/blog/the-new-classics/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The Problem with Python</title>
		<link>http://arantaday.com/blog/the-problem-with-python/</link>
		<comments>http://arantaday.com/blog/the-problem-with-python/#comments</comments>
		<pubDate>Sat, 06 Sep 2008 07:18:45 +0000</pubDate>
		<dc:creator>smanek</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[critique]]></category>

		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://arantaday.com/blog/?p=9</guid>
		<description><![CDATA[To get started, I&#8217;d just like to say &#8220;Hello&#8221;. This is my first real blog post (excluding the geocities and xanga sites I started back in high school).
I&#8217;ve been playing with Python recently, and a few things have been bugging me. Before I get started, I do have to admit Python does a lot of [...]]]></description>
			<content:encoded><![CDATA[<p>To get started, I&#8217;d just like to say &#8220;Hello&#8221;. This is my first real blog post (excluding the geocities and xanga sites I started back in high school).</p>
<p>I&#8217;ve been playing with <a href="http://www.python.org/" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.python.org/');">Python</a> recently, and a few things have been bugging me. Before I get started, I do have to admit Python does a lot of things right; for all its flaws, I&#8217;d much rather work in Python than Java or C++.</p>
<p>That being said, there are a few things I&#8217;ve encountered that seem very inconsistent, and are very inconvenient. I had hoped that they were just legacy artifacts and would be fixed in <a href="http://docs.python.org/dev/3.0/" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://docs.python.org/dev/3.0/');">Python 3000</a> but, for the most part, they seem to be sticking around.</p>
<p><strong>Scoping</strong><br />
First, to show you the expected behavior of <a href="http://en.wikipedia.org/wiki/Static_scope#Static_scoping_.28also_known_as_lexical_scoping.29" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/Static_scope#Static_scoping_.28also_known_as_lexical_scoping.29');">lexically scoped</a> variables in Python:</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;">a = <span style="color: #483d8b;">&quot;foo&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> f<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> a <span style="color: #808080; font-style: italic;">#a is free, so it refers to the global binding &quot;foo&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> g<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    a = <span style="color: #483d8b;">&quot;bar&quot;</span>
    f<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#Python creates a closure around the function f with a reference to the global a</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> a <span style="color: #808080; font-style: italic;">#Here the global a is shadowed by the local a, which has a value of &quot;bar&quot;</span>
&nbsp;
<span style="color: #66cc66;">&gt;&gt;&gt;</span>g<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
foo
bar</pre></div></div>

<p>As expected, calling the function <em>g</em> prints &#8220;foo&#8221; and then &#8220;bar&#8221;.</p>
<p>Now, when we try a slightly more interesting example:</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;">a = <span style="color: #483d8b;">&quot;foo&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> f<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> a <span style="color: #808080; font-style: italic;">#Should refer to the global a, declared above</span>
    a = <span style="color: #483d8b;">&quot;bar&quot;</span> <span style="color: #808080; font-style: italic;">#Should shadow the global (statically scoped) a</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> a <span style="color: #808080; font-style: italic;">#Should print bar</span>
&nbsp;
<span style="color: #66cc66;">&gt;&gt;&gt;</span>f<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
UnboundLocalError: local variable <span style="color: #483d8b;">'a'</span> referenced before assignment</pre></div></div>

<p>There is no good reason for a language to exhibit this behavior: it&#8217;s completely inconsistent with what was demonstrated in the first example. Since &#8216;a&#8217; is free in the function <em>f</em> the first time we try to print it, it should refer to the global &#8216;a&#8217; with the value of &#8220;foo&#8221;.</p>
<p><strong>Rounding errors</strong><br />
Now, maybe I&#8217;m just spoiled coming from doing <a href="http://en.wikipedia.org/wiki/Common_lisp" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://en.wikipedia.org/wiki/Common_lisp');">Common Lisp</a> work, but I&#8217;m used to programming languages handling numbers right. In Lisp, I don&#8217;t have to tell the compiler how big or small my numbers are going to be (although, I optionally can for better performance). They just work. Lisp transparently uses fractions or floating points or complex numbers  or integers automatically - and it never makes a rounding error. To me <img src="http://arantaday.com/blog/wp-content/cache/tex_93d91d9cc65c1ff278d78468676e9ab8.gif" align="absmiddle" class="tex" alt="0.1 = \frac{1}{10} = \frac{1.0}{10.0}" />, etc.</p>
<p>Technically, I understand that it is <a href="http://docs.sun.com/source/806-3568/ncg_goldberg.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://docs.sun.com/source/806-3568/ncg_goldberg.html');">rather difficult</a> to perfectly represent decimal numbers in binary machines, but Lisp (and many other languages) solved these problems 30 years ago. There&#8217;s no reason that we should still be struggling with them today - especially in a very high level language like Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span>a = <span style="color: #ff4500;">0.1</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span>a
<span style="color: #ff4500;">0.10000000000000001</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span>a - <span style="color: #ff4500;">1</span>/<span style="color: #ff4500;">10</span>
<span style="color: #ff4500;">0.10000000000000001</span></pre></div></div>

<p><strong><img src="http://arantaday.com/blog/wp-content/cache/tex_cd1b775f1955be54b07f140fc303eede.gif" align="absmiddle" class="tex" alt="\Huge\lambda" />&#8217;s</strong><br />
Python&#8217;s lambdas have been emasculated. I know that lambas are never strictly necessary - anything I can do with a lambda I can do with a named function. And I also know that python provides powerful list comprehensions which serve the purpose of map/filter and lambda more often than not. But, there are often times when I just want a lambda.</p>
<p>The problem is, python&#8217;s lambda doesn&#8217;t work. You can&#8217;t &#8216;print&#8217; inside a lambda (but you can call another function that can?!)  and they can&#8217;t span multiple lines. I don&#8217;t know if these were purposeful design decisions meant to discourage the use of lambdas (maybe, especially in light of the <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=98196" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.artima.com/weblogs/viewpost.jsp?thread=98196');">talk</a> I&#8217;ve seen of eventually deprecating reduce, filter, and map) but, in any case, they are annoying.</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt;&amp;gt;&amp;gt; a = <span style="color: #ff7700;font-weight:bold;">lambda</span> x: <span style="color: #ff7700;font-weight:bold;">print</span> x
<span style="color: #008000;">SyntaxError</span>: invalid syntax
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> myPrint<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> x
&nbsp;
a = <span style="color: #ff7700;font-weight:bold;">lambda</span> x: myPrint<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span>a<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Hello World&quot;</span><span style="color: black;">&#41;</span>
Hello World</pre></div></div>

<p>And here ends my first, relatively mild, rant.</p>
]]></content:encoded>
			<wfw:commentRss>http://arantaday.com/blog/the-problem-with-python/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 2.330 seconds -->
<!-- Cached page generated by WP-Super-Cache on 2009-01-07 07:39:54 -->
