<?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>nklein software &#187; Programming</title>
	<atom:link href="http://nklein.com/topics/articles/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://nklein.com</link>
	<description>software development and consulting</description>
	<lastBuildDate>Tue, 22 May 2012 03:48:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Visualizing Galois Fields (Follow-up)</title>
		<link>http://nklein.com/2012/05/visualizing-galois-fields-follow-up/</link>
		<comments>http://nklein.com/2012/05/visualizing-galois-fields-follow-up/#comments</comments>
		<pubDate>Tue, 22 May 2012 03:48:05 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Galois fields]]></category>
		<category><![CDATA[Let Over Lambda]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[zpng]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1872</guid>
		<description><![CDATA[In my previous article, I should have finished by remapping the multiplication and addition tables so that the multiplication table was in cyclic order. In cyclic order, the zeroth column (or row) represents zero and the -th column (or row) (for]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://nklein.com/2012/05/visualizing-galois-fields/">previous article</a>, I should have finished by remapping the multiplication and addition tables so that the multiplication table was in cyclic order.  In cyclic order, the zeroth column (or row) represents zero and the <img src="http://l.wordpress.com/latex.php?latex=%28i%2B1%29&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="(i+1)" style="vertical-align:-20%;" class="tex" alt="(i+1)" />-th column (or row) (for <img src="http://l.wordpress.com/latex.php?latex=i%20%3E%200&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="i > 0&#8243; style=&#8221;vertical-align:-20%;&#8221; class=&#8221;tex&#8221; alt=&#8221;i > 0&#8243; />) represents <img src="http://l.wordpress.com/latex.php?latex=a%5Ei&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="a^i" style="vertical-align:-20%;" class="tex" alt="a^i" /> for some generator <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_3cad95c81df4676e1b14e93cbb0b18ae.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" />.  As such, the multiplication table is simply <img src="http://l.wordpress.com/latex.php?latex=0&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="0" style="vertical-align:-20%;" class="tex" alt="0" /> in the zeroth row and column and <img src="http://l.wordpress.com/latex.php?latex=1%20%2B%20%28%28i%2Bj%29%20%5Cmod%20255%29&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="1 + ((i+j) \mod 255)" style="vertical-align:-20%;" class="tex" alt="1 + ((i+j) \mod 255)" /> for the spot <img src="http://l.wordpress.com/latex.php?latex=%28i%2B1%2Cj%2B1%29&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="(i+1,j+1)" style="vertical-align:-20%;" class="tex" alt="(i+1,j+1)" />.</p>
<p><center><a href="http://nklein.com/wp-content/uploads/2012/05/gf256m.png"><img src="http://nklein.com/wp-content/uploads/2012/05/gf256m.png" alt="" title="gf256m" width="256" height="256" class="aligncenter size-full wp-image-1873" /></a></center></p>
<p>Once resorted by the order of the powers of a generator <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_3cad95c81df4676e1b14e93cbb0b18ae.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" />, the multiplication table look the same regardless of the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_3cad95c81df4676e1b14e93cbb0b18ae.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" />.  The addition, however, looks different for different generators.  Below are the addition tables for two of them: <img src="http://l.wordpress.com/latex.php?latex=a%20%3D%20%281%2Bx%29&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="a = (1+x)" style="vertical-align:-20%;" class="tex" alt="a = (1+x)" /> on the right and <img src="http://l.wordpress.com/latex.php?latex=a%20%3D%20x%5E3%20%2B%20x%5E6%20%2B%20x%5E7&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="a = x^3 + x^6 + x^7" style="vertical-align:-20%;" class="tex" alt="a = x^3 + x^6 + x^7" /> on the left.  They make as decent a stereo as any of the other pairs so far.</p>
<p><center><a href="http://nklein.com/wp-content/uploads/2012/05/gf256a.png"><img src="http://nklein.com/wp-content/uploads/2012/05/gf256a.png" alt="" title="gf256a-00000011" width="256" height="256" class="aligncenter size-full wp-image-1874" /></a> &nbsp; <a href="http://nklein.com/wp-content/uploads/2012/05/gf256a-200.png"><img src="http://nklein.com/wp-content/uploads/2012/05/gf256a-200.png" alt="" title="gf256a-11001000" width="256" height="256" class="aligncenter size-full wp-image-1875" /></a></center></p>
<p>Here&#8217;s the code that I used to generate the remapping for a given generator.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> make-cyclic-remapping <span style="color: #66cc66;">&#40;</span>fn generator <span style="color: #66cc66;">&amp;</span>optional <span style="color: #66cc66;">&#40;</span>limit <span style="color: #cc66cc;">256</span><span style="color: #66cc66;">&#41;</span><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>to <span style="color: #66cc66;">&#40;</span>make-<span style="color: #b1b100;">array</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> limit<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">initial-element</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>from <span style="color: #66cc66;">&#40;</span>make-<span style="color: #b1b100;">array</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> limit<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">initial-element</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>used <span style="color: #66cc66;">&#40;</span>make-hash-table <span style="color: #66cc66;">:</span><span style="color: #555;">test</span> #'<span style="color: #b1b100;">equal</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;">;; fill up the lookup tables</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">&#40;</span>gethash <span style="color: #cc66cc;">0</span> used<span style="color: #66cc66;">&#41;</span> t<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>nlet fill-tables <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">exp</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>acc <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;</span> <span style="color: #b1b100;">exp</span> limit<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;">aref</span> to <span style="color: #b1b100;">exp</span><span style="color: #66cc66;">&#41;</span> acc
              <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">aref</span> from acc<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">exp</span>
              <span style="color: #66cc66;">&#40;</span>gethash acc used<span style="color: #66cc66;">&#41;</span> t<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>fill-tables <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>+ <span style="color: #b1b100;">exp</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> fn acc generator<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: #808080; font-style: italic;">;; return a closure around the lookup tables</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>hash-table-count used<span style="color: #66cc66;">&#41;</span> limit<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>direction n<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>ecase direction
          <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">to</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">aref</span> to n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">from</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">aref</span> from 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><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>If you&#8217;ve read it, you can probably tell by my code that I&#8217;m still under the influence of <a href="http://letoverlambda.com/">Let Over Lambda</a>.  If you haven&#8217;t read it, it is quite worth the read.</p>
<p>Then, I used a modified version of the <code class="codecolorer lisp default"><span class="lisp">draw-heightmap</span></code> function which also takes in the remapping function.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> draw-mapped-heightmap <span style="color: #66cc66;">&#40;</span>op map filename <span style="color: #66cc66;">&amp;</span>optional <span style="color: #66cc66;">&#40;</span>limit <span style="color: #cc66cc;">256</span><span style="color: #66cc66;">&#41;</span><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>png <span style="color: #66cc66;">&#40;</span>make-instance 'zpng<span style="color: #66cc66;">:</span><span style="color: #555;">pixel-streamed-png</span>
                            <span style="color: #66cc66;">:</span><span style="color: #555;">color-type</span> <span style="color: #66cc66;">:</span><span style="color: #555;">grayscale</span>
                            <span style="color: #66cc66;">:</span><span style="color: #555;">width</span> limit
                            <span style="color: #66cc66;">:</span><span style="color: #555;">height</span> limit<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>with-open-file <span style="color: #66cc66;">&#40;</span>stream filename
                            <span style="color: #66cc66;">:</span><span style="color: #555;">direction</span> <span style="color: #66cc66;">:</span><span style="color: #555;">output</span>
                            <span style="color: #66cc66;">:</span><span style="color: #555;">if-does-not-exist</span> <span style="color: #66cc66;">:</span><span style="color: #555;">create</span>
                            <span style="color: #66cc66;">:</span><span style="color: #555;">element-type</span> '<span style="color: #66cc66;">&#40;</span>unsigned-byte <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>zpng<span style="color: #66cc66;">:</span><span style="color: #555;">start-png</span> png stream<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">dotimes</span> <span style="color: #66cc66;">&#40;</span>xx limit<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">dotimes</span> <span style="color: #66cc66;">&#40;</span>yy limit<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>a <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> map <span style="color: #66cc66;">:</span><span style="color: #555;">to</span> xx<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                 <span style="color: #66cc66;">&#40;</span>b <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> map <span style="color: #66cc66;">:</span><span style="color: #555;">to</span> yy<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                 <span style="color: #66cc66;">&#40;</span>c <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> map <span style="color: #66cc66;">:</span><span style="color: #555;">from</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> op a 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>zpng<span style="color: #66cc66;">:</span><span style="color: #555;">write-pixel</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> c<span style="color: #66cc66;">&#41;</span> png<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>zpng<span style="color: #66cc66;">:</span><span style="color: #555;">finish-png</span> png<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>values<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2012/05/visualizing-galois-fields-follow-up/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Anti-Cons</title>
		<link>http://nklein.com/2012/02/the-anti-cons/</link>
		<comments>http://nklein.com/2012/02/the-anti-cons/#comments</comments>
		<pubDate>Tue, 28 Feb 2012 03:51:44 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[anti-cons]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[polynomials]]></category>
		<category><![CDATA[recursion]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1801</guid>
		<description><![CDATA[Motivation I no longer remember what led me to this page of synthetic division implemented in various languages. The author provides a common lisp implementation for taking a list representing the coefficients of a polynomial in one variable and a number and returning the result of dividing the polynomial by . The author states: I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<h3>Motivation</h3>
<p>I no longer remember what led me to this page of <a href="http://kourge.net/node/98">synthetic division</a> implemented in various languages.  The author provides a common lisp implementation for taking a list representing the coefficients of a polynomial in one variable <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_1a507c5494969dc6de305770cadc6630.png" title="x" style="vertical-align:-20%;" class="tex" alt="x" /> and a number <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_a7175a60e504c64ad51e7119b44ee9aa.png" title="\alpha" style="vertical-align:-20%;" class="tex" alt="\alpha" /> and returning the result of dividing the polynomial by <img src="http://l.wordpress.com/latex.php?latex=%28x-%5Calpha%29&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="(x-\alpha)" style="vertical-align:-20%;" class="tex" alt="(x-\alpha)" />. </p>
<p>The author states: <q>I&#8217;m very sure this isn&#8217;t considered <em>Lispy</em> and would surely seem like an awkward port from an extremely Algol-like mindset in the eyes of a seasoned Lisper.</q>  In the mood for the exercise, I reworked his code snippet into slightly more canonical lisp while leaving the basic structure the same:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> synthetic-division <span style="color: #66cc66;">&#40;</span>polynomial divisor<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>result <span style="color: #66cc66;">&#40;</span>first polynomial<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>                                            
         <span style="color: #66cc66;">&#40;</span>quotient <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> result<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;">dolist</span> <span style="color: #66cc66;">&#40;</span>coefficient <span style="color: #66cc66;">&#40;</span>rest polynomial<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>                                     
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> result <span style="color: #66cc66;">&#40;</span>* result divisor<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>                                          
      <span style="color: #66cc66;">&#40;</span>incf result coefficient<span style="color: #66cc66;">&#41;</span>                                                 
      <span style="color: #66cc66;">&#40;</span>push result quotient<span style="color: #66cc66;">&#41;</span><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>remainder <span style="color: #66cc66;">&#40;</span>pop quotient<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;">list</span> <span style="color: #66cc66;">:</span><span style="color: #555;">quotient</span> <span style="color: #66cc66;">&#40;</span>nreverse quotient<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">remainder</span> remainder<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>
<p>From there, I went on to implement it using tail recursion to get rid of the <code class="codecolorer lisp default"><span class="lisp">#'<span style="color: #b1b100;">setf</span></span></code> and <code class="codecolorer lisp default"><span class="lisp">#'incf</span></code> and <code class="codecolorer lisp default"><span class="lisp">#'push</span></code>:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> synthetic-division-<span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">&#40;</span>polynomial divisor<span style="color: #66cc66;">&#41;</span>                                
  <span style="color: #66cc66;">&#40;</span>labels <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>divide <span style="color: #66cc66;">&#40;</span>coefficients remainder quotient<span style="color: #66cc66;">&#41;</span>                            
             <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> coefficients                                                   
                 <span style="color: #66cc66;">&#40;</span>divide <span style="color: #66cc66;">&#40;</span>rest coefficients<span style="color: #66cc66;">&#41;</span>                                    
                         <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>* divisor remainder<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>first coefficients<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>         
                         <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span>* remainder quotient<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>                            
               <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">:</span><span style="color: #555;">quotient</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">reverse</span> quotient<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">remainder</span> remainder<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>divide <span style="color: #66cc66;">&#40;</span>rest polynomial<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>first polynomial<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>What I didn&#8217;t like about this was the complexity of calling the tail-recursive portion.  If I just called it like I wished to  <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&#40;</span>divide polynomial <span style="color: #cc66cc;">0</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span></span></code> then I ended up with one extra coefficient in the answer.  This wouldn&#8217;t do.</p>
<h3>The Joke</h3>
<p>There&#8217;s an old joke about a physicist, a biologist, and a mathematician who were having lunch at an outdoor café.  Just as their food was served, they noticed a couple walk into the house across the street.  As they were finishing up their meal, they saw three people walk out of that very same house.</p>
<p>The physicist said, <q>We must have miscounted.  Three must have entered before.</q></p>
<p>The biologist said, <q>They must have pro-created.</q></p>
<p>The mathematician said, <q>If one more person enters that house, it will again be empty.</q></p>
<h3>The Anti-Cons</h3>
<p>What I needed was a more-than-empty list.  I needed a negative cons-cell.  I needed something to put in place of the <code class="codecolorer lisp default"><span class="lisp"><span style="color: #b1b100;">nil</span></span></code> in <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&#40;</span>divide polynomial <span style="color: #cc66cc;">0</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span></span></code> that would annihilate the first thing it was cons-ed to.</p>
<p>I haven&#8217;t come up with the right notation to make this clear.  It is somewhat like a <a href="http://en.wikipedia.org/wiki/Quasigroup">quasigroup</a> except that there is only one inverse element for all other elements.  Let&#8217;s denote this annihilator: <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_0824a1fdf135170467671326d5780e2b.png" title="\omega" style="vertical-align:-20%;" class="tex" alt="\omega" />.  Let&#8217;s denote list concatenation with <img src="http://l.wordpress.com/latex.php?latex=%5Coplus&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\oplus" style="vertical-align:-20%;" class="tex" alt="\oplus" />.</p>
<p>Only having one inverse-ish element means we have to give up associativity.  For lists <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_3cad95c81df4676e1b14e93cbb0b18ae.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" /> and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_97f4a1ba736119e3cd4de6fca35efd6b.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" />, evaluating <img src="http://l.wordpress.com/latex.php?latex=%28a%20%5Coplus%20%5Comega%29%20%5Coplus%20b&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="(a \oplus \omega) \oplus b" style="vertical-align:-20%;" class="tex" alt="(a \oplus \omega) \oplus b" /> equals <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_97f4a1ba736119e3cd4de6fca35efd6b.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" />, but <img src="http://l.wordpress.com/latex.php?latex=a%20%5Coplus%20%28%5Comega%20%5Coplus%20b%29&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="a \oplus (\omega \oplus b)" style="vertical-align:-20%;" class="tex" alt="a \oplus (\omega \oplus b)" /> equals <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_3cad95c81df4676e1b14e93cbb0b18ae.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" />.</p>
<p>Associativity is a small price to pay though for a prettier call to my tail-recursive function, right?</p>
<h3>The Basic Operations</h3>
<p>For basic operations, I&#8217;m going to need the anti-cons itself and a lazy list of an arbitrary number of anti-conses.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>defconstant anti-<span style="color: #b1b100;">cons</span> 'anti-<span style="color: #b1b100;">cons</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defclass anti-cons-<span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">length</span> <span style="color: #66cc66;">:</span><span style="color: #555;">initarg</span> <span style="color: #66cc66;">:</span><span style="color: #b1b100;">length</span> <span style="color: #66cc66;">:</span><span style="color: #555;">reader</span> anti-cons-list-<span style="color: #b1b100;">length</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">default-initargs</span> <span style="color: #66cc66;">:</span><span style="color: #b1b100;">length</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defmethod print-object <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>obj anti-cons-<span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> stream<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>print-unreadable-object <span style="color: #66cc66;">&#40;</span>obj stream<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>prin1 <span style="color: #66cc66;">&#40;</span>loop <span style="color: #66cc66;">:</span><span style="color: #555;">for</span> ii <span style="color: #66cc66;">:</span><span style="color: #555;">from</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">:</span><span style="color: #555;">to</span> <span style="color: #66cc66;">&#40;</span>anti-cons-list-<span style="color: #b1b100;">length</span> obj<span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">:</span><span style="color: #555;">collecting</span> 'anti-<span style="color: #b1b100;">cons</span><span style="color: #66cc66;">&#41;</span>
           stream<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Then, I&#8217;m going to make some macros to define generic functions named by adding a minus-sign to the end of a Common Lisp function.  The default implementation will simply be the common-lisp function.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> defun- <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">name</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&amp;</span>rest args<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;</span>body methods<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>name- <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">intern</span> <span style="color: #66cc66;">&#40;</span>concatenate 'string <span style="color: #66cc66;">&#40;</span>symbol-<span style="color: #b1b100;">name</span> <span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;-&quot;</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>defgeneric <span style="color: #66cc66;">,</span>name- <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">,</span>@args<span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">method</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">,</span>@args<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">,</span><span style="color: #b1b100;">name</span> <span style="color: #66cc66;">,</span>@args<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">,</span>@methods<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>I&#8217;m even going to go one step further for single-argument functions where I want to override the body for my lazy list of anti-conses using a single form for the body:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> defun1- <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">name</span> <span style="color: #66cc66;">&#40;</span>arg<span style="color: #66cc66;">&#41;</span> a-list-form <span style="color: #66cc66;">&amp;</span>body body<span style="color: #66cc66;">&#41;</span>
  `<span style="color: #66cc66;">&#40;</span>defun- <span style="color: #66cc66;">,</span><span style="color: #b1b100;">name</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">,</span>arg<span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">method</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">,</span>arg anti-cons-<span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">,</span>a-list-form<span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">,</span>@body<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>I need <code class="codecolorer lisp default"><span class="lisp">#'cons-</span></code> to set the stage.  I need to be able to cons an anti-cons with a normal list.  I need to be able to cons an anti-cons with a list of anti-conses.  And, I need to be able to cons something other than an anti-cons with a list of anti-conses.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>defun- <span style="color: #b1b100;">cons</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;">:</span><span style="color: #555;">method</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>a <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eql</span> 'anti-<span style="color: #b1b100;">cons</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>b <span style="color: #b1b100;">list</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;">null</span> b<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>make-instance 'anti-cons-<span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> b<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: #66cc66;">:</span><span style="color: #555;">method</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>a <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eql</span> 'anti-<span style="color: #b1b100;">cons</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>b anti-cons-<span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>make-instance 'anti-cons-<span style="color: #b1b100;">list</span> <span style="color: #66cc66;">:</span><span style="color: #b1b100;">length</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>+ <span style="color: #66cc66;">&#40;</span>anti-cons-list-<span style="color: #b1b100;">length</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>
&nbsp;
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">method</span> <span style="color: #66cc66;">&#40;</span>a <span style="color: #66cc66;">&#40;</span>b anti-cons-<span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><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>b-len <span style="color: #66cc66;">&#40;</span>anti-cons-list-<span style="color: #b1b100;">length</span> b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&gt;</span> b-len <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>make-instance 'anti-cons-<span style="color: #b1b100;">list</span> <span style="color: #66cc66;">:</span><span style="color: #b1b100;">length</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>- b-len<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></pre></div>
<p>Now, I can go on to define some simple functions that can take either anti-cons lists or regular Common Lisp lists.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>defun1- <span style="color: #b1b100;">length</span> <span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>- <span style="color: #66cc66;">&#40;</span>anti-cons-list-<span style="color: #b1b100;">length</span> a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defun1- <span style="color: #b1b100;">car</span> <span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">anti-</span><span style="color: #b1b100;">cons</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defun1- <span style="color: #b1b100;">cdr</span> <span style="color: #66cc66;">&#40;</span>a<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>a-len <span style="color: #66cc66;">&#40;</span>anti-cons-list-<span style="color: #b1b100;">length</span> a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&gt;</span> a-len <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>make-instance 'anti-cons-<span style="color: #b1b100;">list</span> <span style="color: #66cc66;">:</span><span style="color: #b1b100;">length</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>- a-len<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>defun1- <span style="color: #b1b100;">cadr</span> <span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>anti-cons-list-<span style="color: #b1b100;">length</span> a<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">anti-</span><span style="color: #b1b100;">cons</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>defun1- <span style="color: #b1b100;">caddr</span> <span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>anti-cons-list-<span style="color: #b1b100;">length</span> a<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">anti-</span><span style="color: #b1b100;">cons</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>To give a feel for how this all fits together, here&#8217;s a little interactive session:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ANTI-CONS<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>cons- anti-<span style="color: #b1b100;">cons</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span>
#<span style="color: #66cc66;">&lt;</span><span style="color: #66cc66;">&#40;</span>ANTI-<span style="color: #b1b100;">CONS</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&gt;</span>
&nbsp;
ANTI-CONS<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>cons- anti-<span style="color: #b1b100;">cons</span> *<span style="color: #66cc66;">&#41;</span>
#<span style="color: #66cc66;">&lt;</span><span style="color: #66cc66;">&#40;</span>ANTI-<span style="color: #b1b100;">CONS</span> ANTI-<span style="color: #b1b100;">CONS</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&gt;</span>
&nbsp;
ANTI-CONS<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>cons- <span style="color: #66cc66;">:</span><span style="color: #555;">a</span> *<span style="color: #66cc66;">&#41;</span>
#<span style="color: #66cc66;">&lt;</span><span style="color: #66cc66;">&#40;</span>ANTI-<span style="color: #b1b100;">CONS</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&gt;</span>
&nbsp;
ANTI-CONS<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>length- *<span style="color: #66cc66;">&#41;</span>
-<span style="color: #cc66cc;">1</span></pre></div>
<h3>Denouement</h3>
<p>Only forty or fifty lines of code to go from:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>divide <span style="color: #66cc66;">&#40;</span>rest polynomial<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>first polynomial<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>To this:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>divide polynomial <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> anti-<span style="color: #b1b100;">cons</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Definitely worth it.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2012/02/the-anti-cons/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>My Favorite Macro Patterns</title>
		<link>http://nklein.com/2012/02/my-favorite-macro-patterns</link>
		<comments>http://nklein.com/2012/02/my-favorite-macro-patterns#comments</comments>
		<pubDate>Sat, 18 Feb 2012 02:00:00 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[cl-who]]></category>
		<category><![CDATA[css-lite]]></category>
		<category><![CDATA[hunchentoot]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[macros]]></category>
		<category><![CDATA[parenscript]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1789</guid>
		<description><![CDATA[I read Jorge Tavares&#8217;s article on Macro Patterns a few days ago.  I was thinking about replying to mention a few of my favorites: The with- pattern which makes sure a special variable is bound for the body and makes sure the tied resources are released at the end of the block. Macros which collect [...]]]></description>
			<content:encoded><![CDATA[<p>I read <a href="http://jorgetavares.com/2012/02/13/macros-design-patterns/">Jorge Tavares&#8217;s article on Macro Patterns</a> a few days ago.  I was thinking about replying to mention a few of my favorites:</p>
<ul>
<li>The <code class="codecolorer lisp default"><span class="lisp">with-</span></code> pattern which makes sure a special variable is bound for the body and makes sure the tied resources are released at the end of the block.</li>
<li>Macros which collect content (usually into a special variable) so they can do something with the content at the end of the close of the macro.</li>
</ul>
<p>Then, I was working on something tonight when I re-discovered a favorite pattern that I&#8217;d forgotten about:  Putting multiple wrappers on the same body.</p>
<p>I am working on an HTML+JavaScript+CSS project.  In the end, I need static files.  But, I thought I would use the opportunity to really experience <a href="http://weitz.de/cl-who/">CL-Who</a>, <a href="http://common-lisp.net/project/parenscript/">Parenscript</a>, and <a href="https://github.com/paddymul/css-lite">CSS-Lite</a>.</p>
<p>I have now made a macro called <code class="codecolorer lisp default"><span class="lisp">define-web-file</span></code> which takes a CL-Who or Parenscript body and wraps it up as both a <a href="http://weitz.de/hunchentoot/">Hunchentoot</a> handler and a <code class="codecolorer text default"><span class="text">write-to-file</span></code> wrapper.  Now, I can test interactively with Hunchentoot and generate the whole web application when I&#8217;m ready.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2012/02/my-favorite-macro-patterns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Keeping Server and Client Separate</title>
		<link>http://nklein.com/keeping-server-and-client-separate</link>
		<comments>http://nklein.com/keeping-server-and-client-separate#comments</comments>
		<pubDate>Thu, 08 Sep 2011 19:51:33 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[client-server]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[literate programming]]></category>
		<category><![CDATA[userial]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1758</guid>
		<description><![CDATA[The problem Whenever I write client-server applications, I run into the same problem trying to separate the code. To send a message from the server to the client, the server has to serialize that message and the client has to unserialize that message. The server doesn&#8217;t need to unserialize that message. The client doesn&#8217;t need [...]]]></description>
			<content:encoded><![CDATA[<h3>The problem</h3>
<p>Whenever I write client-server applications, I run into the same problem trying to separate the code.  To send a message from the server to the client, the server has to serialize that message and the client has to unserialize that message.  The server doesn&#8217;t need to unserialize that message.  The client doesn&#8217;t need to serialize that message.</p>
<p>It seems wrong to include both the serialization code and the unserialization code in both client and server when each side will only be using 1/2 of that code.  On the other hand, it seems bad to keep the serialization and unserialization code in separate places.  You don&#8217;t want one side serializing A+B+C and the other side trying to unserialize A+C+D+B.</p>
<h3>One approach: data classes</h3>
<p>Some projects deal with this situation by making every message have its own data class.  You take all of the information that you want to be in the message and plop it into a data class.  You then serialize the data class and send the resulting bytes.  The other side unserializes the bytes into a data class and plucks the data out of the data class.</p>
<p>The advantage here is that you can have some metaprogram read the data class definition and generate a serializer or unserializer as needed.  You&#8217;re only out-of-sync if one side hasn&#8217;t regenerated since the data class definition changed.</p>
<p>The disadvantage here is that I loathe data classes.  If my top-level interface is going to be <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&#40;</span>send-login username password<span style="color: #66cc66;">&#41;</span></span></code>, then why can&#8217;t I just serialize straight from there without having to create a dippy data structure to hold my opcode and two strings?</p>
<h3>Another approach: suck it up</h3>
<p>Who cares if the client contains both the serialization and unserialization code?  Heck, if you&#8217;re really all that concerned, then <code class="codecolorer lisp default"><span class="lisp">fmakunbound</span></code> half the universe before you <code class="codecolorer lisp default"><span class="lisp">save-lisp-and-die</span></code>.</p>
<p>Of course, unless you&#8217;re using data classes, you&#8217;re either going to have code in your client that references a bunch of functions and variables that only exist in your server or your client and server will be identical except for:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> main <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  #+server <span style="color: #66cc66;">&#40;</span>server-main<span style="color: #66cc66;">&#41;</span>
  #-server <span style="color: #66cc66;">&#40;</span>client-main<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Now, of course, your server is going to accidentally depend on OpenGL and OpenAL and SDL and a whole bunch of other -L&#8217;s it never actually calls.  Meanwhile, your client is going to accidentally depend on Postmodern and Portable-Threads and a whole bunch of other Po-&#8217;s it never actually calls.</p>
<h3>Another approach: tangle and weave, baby</h3>
<p>Another way that I&#8217;ve got around this is to use literate programming tools to let me write the serialiization and unserialization right next to each other in my document.  Then, anyone going to change the serialize code would be immediately confronted with the unserialize code that goes with it.</p>
<p>The advantage here is that you can tangle the client code through an entirely separate path than the server code keeping only what you need in each.</p>
<p>The disadvantage here is that now both your client code and your server code have to be in the same document or both include the same sizable chunk of document.  And, while there aren&#8217;t precisely name-capturing problems, trying to include the &#8220;serialize-and-send&#8221; chunk in your function in the client code still requires that you use the same variable names that were in that chunk.</p>
<h3>How can Lisp make this better?</h3>
<p>In Lisp, we can get the benefits of a data-definition language and data classes without needing the data classes.  Here&#8217;s a snippet of the data definition for a simple client-server protocol.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;;; protocol.lisp</span>
<span style="color: #66cc66;">&#40;</span>userial<span style="color: #66cc66;">:</span><span style="color: #555;">make-enum-serializer</span> <span style="color: #66cc66;">:</span><span style="color: #555;">opcode</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">ping</span> <span style="color: #66cc66;">:</span><span style="color: #555;">ping-ack</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>defmessage <span style="color: #66cc66;">:</span><span style="color: #555;">ping</span>     <span style="color: #66cc66;">:</span><span style="color: #555;">uint32</span> ping-payload<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>defmessage <span style="color: #66cc66;">:</span><span style="color: #555;">ping-ack</span> <span style="color: #66cc66;">:</span><span style="color: #555;">uint32</span> ping-payload<span style="color: #66cc66;">&#41;</span></pre></div>
<p>I&#8217;ve declared there are two different types of messages, each with their own opcode.  Now, I have macros for <code class="codecolorer lisp default"><span class="lisp">define-sender</span></code> and <code class="codecolorer lisp default"><span class="lisp">define-handler</span></code> that allow me to create functions which have no control over the actual serialization and unserialization.  My functions can only manipulate the named message parameters (the value of <code class="codecolorer lisp default"><span class="lisp">ping-payload</span></code> in this case) before serialization or after unserialization but cannot change the serialization or unserialization itself.</p>
<p>With this protocol, the client side has to handle ping messages by sending ping-ack messages.  The <code class="codecolorer lisp default"><span class="lisp">define-sender</span></code> macro takes the opcode of the message (used to identify the message fields), the name of the function to create, the argument list for the function (which may include declarations for some or all of the fields in the message), the form to use for the address to send the resulting message to, and any body needed to set fields in the packet based on the function arguments before the serialization.  The <code class="codecolorer lisp default"><span class="lisp">define-handler</span></code> macro takes the opcode of the message (again, used to identify the message fields), the name of the function to create, the argument list for the function, the form to use for the buffer to unserialize, and any body needed to act on the unserialized message fields.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;;; client.lisp</span>
<span style="color: #66cc66;">&#40;</span>define-sender  <span style="color: #66cc66;">:</span><span style="color: #555;">ping-ack</span> send-ping-ack <span style="color: #66cc66;">&#40;</span>ping-payload<span style="color: #66cc66;">&#41;</span> *server-address*<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define-handler <span style="color: #66cc66;">:</span><span style="color: #555;">ping</span>     handle-ping   <span style="color: #66cc66;">&#40;</span>buffer<span style="color: #66cc66;">&#41;</span> buffer
  <span style="color: #66cc66;">&#40;</span>send-ping-ack ping-payload<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>The server side has a bit more work to do because it&#8217;s going to generate the sequence numbers and track the round-trip ping times.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;;; server.lisp</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defvar *last-ping-payload* <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>defvar *last-ping-time*    <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>define-sender <span style="color: #66cc66;">:</span><span style="color: #555;">ping</span> send-ping <span style="color: #66cc66;">&#40;</span>who<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>get-address-of who<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>incf *last-ping-payload*<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> *last-ping-time*    <span style="color: #66cc66;">&#40;</span>get-internal-real-time<span style="color: #66cc66;">&#41;</span>
        ping-payload        *last-ping-payload*<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>define-handler <span style="color: #66cc66;">:</span><span style="color: #555;">ping-ack</span> handle-ping-ack <span style="color: #66cc66;">&#40;</span>who buffer<span style="color: #66cc66;">&#41;</span> buffer
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> ping-payload *last-ping-payload*<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>update-ping-time who <span style="color: #66cc66;">&#40;</span>- <span style="color: #66cc66;">&#40;</span>get-internal-real-time<span style="color: #66cc66;">&#41;</span> *last-ping-time*<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>
<h3>Problems with the above</h3>
<p>It feels strange to leave compile-time artifacts like the names and types of the message fields in the code after I&#8217;ve generated the functions that I&#8217;m actually going to use.  But, I guess that&#8217;s just part of Lisp development.  You can&#8217;t (easily) unload a package.  I can <code class="codecolorer lisp default"><span class="lisp">makunbound</span></code> a bunch of stuff after I&#8217;m loaded if I don&#8217;t want it to be convenient to modify senders or handlers at run-time.</p>
<p>There is intentional name-capture going on.  The names of the message fields become names in the handlers.  The biggest problem with this is that the <code class="codecolorer lisp default"><span class="lisp">defmessage</span></code> calls really have to be in the same namespace as the <code class="codecolorer lisp default"><span class="lisp">define-sender</span></code> and <code class="codecolorer lisp default"><span class="lisp">define-handler</span></code> calls.</p>
<p>I still have some work to do on my macros to support <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&amp;</span>key</span></code> and <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&amp;</span>optional</span></code> and <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&amp;</span>aux</span></code> and <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&amp;</span>rest</span></code> arguments properly.  I will post those macros once I&#8217;ve worked out those kinks.</p>
<p>Anyone care to share how they&#8217;ve tackled client-server separation before?</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/keeping-server-and-client-separate/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>C- in MacOSX&#8217;s Terminal.app</title>
		<link>http://nklein.com/2011/06/c-in-macosxs-terminal-app/</link>
		<comments>http://nklein.com/2011/06/c-in-macosxs-terminal-app/#comments</comments>
		<pubDate>Tue, 14 Jun 2011 06:25:24 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[emacs]]></category>
		<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1747</guid>
		<description><![CDATA[After tonight&#8217;s TC Lispers Meeting, I had a renewed interest in figuring out why C-&#60;right arrow&#62; didn&#8217;t work for me in Org-Mode or Paredit. After a whole bunch of running in circles, I have discovered a combination that works (with these clues). I have my TERM variable set to xterm-color. I configured the Terminal.app using [...]]]></description>
			<content:encoded><![CDATA[<p>After tonight&#8217;s <a href="http://tclispers.org/events/june-meeting-emacs-theme">TC Lispers Meeting</a>, I had a renewed interest in figuring out why C-&lt;right arrow&gt; didn&#8217;t work for me in Org-Mode or Paredit.</p>
<p>After a whole bunch of running in circles, I have discovered a combination that works (with <a href="http://marc-abramowitz.com/archives/2006/10/05/ctrl-left-and-ctrl-right-in-bash-and-emacs/">these clues</a>).  I have my <code class="codecolorer text default"><span class="text">TERM</span></code> variable set to <code class="codecolorer text default"><span class="text">xterm-color</span></code>.  I configured the Terminal.app using its Keyboard settings to have it send the string &#8220;\033[1;5C&#8221; for C-&lt;right arrow&gt; and &#8220;\033[1;5D&#8221; for C-&lt;left arrow&gt;.  (The &#8220;\033&#8243; is the escape key.)</p>
<p>This works for me even through <code class="codecolorer text default"><span class="text">screen</span></code>.</p>
<p>Bonus.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2011/06/c-in-macosxs-terminal-app/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Delayed Evaluation Across Packages</title>
		<link>http://nklein.com/2011/04/delayed-evaluation-across-packages/</link>
		<comments>http://nklein.com/2011/04/delayed-evaluation-across-packages/#comments</comments>
		<pubDate>Mon, 25 Apr 2011 02:17:08 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[unet]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1666</guid>
		<description><![CDATA[For my networking layer library, I wanted to provide ubiquitous logging. At the same time, I did not want to tie the application to my choice of logging library. I wanted the user to be able to pass me a function where I could give them a logging category and something to log. &#40;in-package :unet&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>For my <a href="http://nklein.com/software/unet/">networking layer library</a>, I wanted to provide ubiquitous logging.  At the same time, I did not want to tie the application to my choice of logging library.  I wanted the user to be able to pass me a function where I could give them a logging category and something to log.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>in-package <span style="color: #66cc66;">:</span><span style="color: #555;">unet</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defvar *logger-<span style="color: #b1b100;">function</span>* <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> log-it <span style="color: #66cc66;">&#40;</span>category thing-to-log<span style="color: #66cc66;">&#41;</span>
  `<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> *logger-<span style="color: #b1b100;">function</span>*
     <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> *logger-<span style="color: #b1b100;">function</span>* <span style="color: #66cc66;">,</span>category <span style="color: #66cc66;">,</span>thing-to-log<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>This seems simple enough, right?  Now, throughout my code, I can do things like:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>log-it <span style="color: #66cc66;">:</span><span style="color: #555;">incoming-packet</span> packet<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>log-it <span style="color: #66cc66;">:</span><span style="color: #555;">list-of-unacked-packets</span> <span style="color: #66cc66;">&#40;</span>get-list-of-unacked-packets<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>etc<span style="color: #66cc66;">&#41;</span></pre></div>
<p>The application can register a <code class="codecolorer lisp default"><span class="lisp">*logger-<span style="color: #b1b100;">function</span>*</span></code> something like this:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> app-log-network-msgs <span style="color: #66cc66;">&#40;</span>category thing-to-log<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>cl-log<span style="color: #66cc66;">:</span><span style="color: #555;">log-message</span> category thing-to-log<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Here&#8217;s the problem though:  most (all?) logging libraries are good about not evaluating any arguments beyond the <code class="codecolorer text default"><span class="text">category</span></code> unless something is actually listening for messages of that category.  This makes it reasonable to do stuff like this and only take the speed hit when it&#8217;s actually important to do so:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>log-it <span style="color: #66cc66;">:</span><span style="color: #555;">excruciating-detail</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">mapcar</span> #'get-excrutiating-detail <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">append</span> everyone everything<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>With my macro above, I have no way of knowing whether something is listening on a particular category or not.  Further, most logging libraries don&#8217;t offer a way to query that sort of information.</p>
<h3>What to do?</h3>
<p>What I wanted to do was to pass a macro from the application package into my networking library instead of passing a function.  I spent way too long trying to find a way to make this work (especially considering I ran into the same trouble in <a href="http://nklein.com/2009/10/lisp-troubles-fabricating-a-closure/">October, 2009</a> trying to use some combination of <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&#40;</span>macroexpand <span style="color: #66cc66;">...</span><span style="color: #66cc66;">&#41;</span></span></code> and <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eval</span> <span style="color: #66cc66;">...</span><span style="color: #66cc66;">&#41;</span></span></code> to let the caller decide which forms to execute).</p>
<p>All it took was posting to <a href="http://groups.google.com/group/comp.lang.lisp/browse_thread/thread/a63a10394350cc7e/c06e26bb6dc1a20e#c06e26bb6dc1a20e">comp.lang.lisp</a> to answer my own question:  <b>Closures</b>.  I changed my macro to create a closure:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> log-it <span style="color: #66cc66;">&#40;</span>category thing-to-log<span style="color: #66cc66;">&#41;</span>
  `<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> *logger-<span style="color: #b1b100;">function</span>*
     <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> *logger-<span style="color: #b1b100;">function</span>* <span style="color: #66cc66;">,</span>category #'<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;">,</span>thing-to-log<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>
<p>Now, the application&#8217;s logger function changes slightly and my forms are only evaluated when the logging library evaluates them:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> app-log-network-msgs <span style="color: #66cc66;">&#40;</span>category thing-to-log-generator<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>cl-log<span style="color: #66cc66;">:</span><span style="color: #555;">log-message</span> category <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> thing-to-log-generator<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Hopefully, I will remember next time I run into this.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2011/04/delayed-evaluation-across-packages/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Binary Logging with CL-Log</title>
		<link>http://nklein.com/2011/04/binary-logging-with-cl-log/</link>
		<comments>http://nklein.com/2011/04/binary-logging-with-cl-log/#comments</comments>
		<pubDate>Thu, 07 Apr 2011 03:47:30 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[quicklisp]]></category>
		<category><![CDATA[unet]]></category>
		<category><![CDATA[userial]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1640</guid>
		<description><![CDATA[One of the things that my current work does better than anywhere I&#8217;ve worked before is logging. When something goes wrong, there is a log file that you can dig through to find all kinds of information about what you were doing and how things were going. As I move forward programming a game with [...]]]></description>
			<content:encoded><![CDATA[<p>One of the things that my current work does better than anywhere I&#8217;ve worked before is logging.  When something goes wrong, there is a log file that you can dig through to find all kinds of information about what you were doing and how things were going.</p>
<p>As I move forward programming a game with my <a href="http://nklein.com/software/unet/">UNet</a> library, I want to make sure that I can easily log all the network traffic during testing runs at least.</p>
<p>In looking through the various <a href="http://cliki.net/logging">Lisp logging</a> packages out there, I decided on Nick Levine&#8217;s <a href="http://www.nicklevine.org/cl-log/">cl-log</a> library.</p>
<p>I installed it in no time with <a href="http://quicklisp.org/">quicklisp</a>.</p>
<p>Then, I set to work trying to figure out how I could use it to log binary data.</p>
<p>Here&#8217;s what I ended up with.  If you want to do something similar, this should give you a good starting point.</p>
<h3>Serializing, unserializing, and categorizing</h3>
<p>With my <a href="http://nklein.com/software/unet/userial/">USerial</a> library, I defined a serializer to keep track of the different categories of log messages.  And, I made corresponding categories in cl-log.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>make-enum-serializer <span style="color: #66cc66;">:</span><span style="color: #555;">log-category</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">packet</span> <span style="color: #66cc66;">:</span><span style="color: #b1b100;">error</span> <span style="color: #66cc66;">:</span><span style="color: #555;">warning</span> <span style="color: #66cc66;">:</span><span style="color: #555;">info</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defcategory <span style="color: #66cc66;">:</span><span style="color: #555;">packet</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>defcategory <span style="color: #66cc66;">:</span><span style="color: #b1b100;">error</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>defcategory <span style="color: #66cc66;">:</span><span style="color: #555;">warning</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">or</span> <span style="color: #66cc66;">:</span><span style="color: #b1b100;">error</span> <span style="color: #66cc66;">:</span><span style="color: #555;">warning</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>defcategory <span style="color: #66cc66;">:</span><span style="color: #555;">info</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">or</span> <span style="color: #66cc66;">:</span><span style="color: #555;">warning</span> <span style="color: #66cc66;">:</span><span style="color: #555;">info</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<h3>Specializing the classes</h3>
<p>There are two major classes that I specialized: <b>base-message</b> and <b>base-messenger</b>.  For my toying around, I didn&#8217;t end up adding any functionality to the <b>base-message</b> class.  I will show it here though so that you know you can do it.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>defclass serialized-message <span style="color: #66cc66;">&#40;</span>base-message<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defclass serialized-messenger <span style="color: #66cc66;">&#40;</span>base-messenger<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>filename <span style="color: #66cc66;">:</span><span style="color: #555;">initarg</span> <span style="color: #66cc66;">:</span><span style="color: #555;">filename</span> <span style="color: #66cc66;">:</span><span style="color: #555;">reader</span> serialized-messenger-filename<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Then, I overrode the <b>messenger-send-message</b> generic function to create a binary header with my <a href="http://nklein.com/software/unet/userial/">USerial</a> library and then write the header and the message out.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>defmethod messenger-send-message <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>messenger serialized-messenger<span style="color: #66cc66;">&#41;</span>
                                   <span style="color: #66cc66;">&#40;</span>message serialized-message<span style="color: #66cc66;">&#41;</span><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>header <span style="color: #66cc66;">&#40;</span>make-buffer <span style="color: #cc66cc;">16</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>serialize* <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">uint64</span> <span style="color: #66cc66;">&#40;</span>timestamp-universal-time
                              <span style="color: #66cc66;">&#40;</span>message-timestamp message<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                 <span style="color: #66cc66;">:</span><span style="color: #555;">log-category</span> <span style="color: #66cc66;">&#40;</span>message-category message<span style="color: #66cc66;">&#41;</span>
                 <span style="color: #66cc66;">:</span><span style="color: #555;">uint64</span> <span style="color: #66cc66;">&#40;</span>buffer-<span style="color: #b1b100;">length</span> <span style="color: #66cc66;">:</span><span style="color: #555;">buffer</span> <span style="color: #66cc66;">&#40;</span>message-description message<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">:</span><span style="color: #555;">buffer</span> header<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>with-open-file <span style="color: #66cc66;">&#40;</span>stream <span style="color: #66cc66;">&#40;</span>serialized-messenger-filename messenger<span style="color: #66cc66;">&#41;</span>
                            <span style="color: #66cc66;">:</span><span style="color: #555;">direction</span> <span style="color: #66cc66;">:</span><span style="color: #555;">output</span>
                            <span style="color: #66cc66;">:</span><span style="color: #555;">if-does-not-exist</span> <span style="color: #66cc66;">:</span><span style="color: #555;">create</span>
                            <span style="color: #66cc66;">:</span><span style="color: #555;">if-exists</span> <span style="color: #66cc66;">:</span><span style="color: #b1b100;">append</span>
                            <span style="color: #66cc66;">:</span><span style="color: #555;">element-type</span> '<span style="color: #66cc66;">&#40;</span>unsigned-byte <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>write-sequence header stream<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>write-sequence <span style="color: #66cc66;">&#40;</span>message-description message<span style="color: #66cc66;">&#41;</span> stream<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>
<h3>Using it</h3>
<p>To get things going, I then made a log manager that accepts my <b>serialized-message</b> type and started one of my <b>serialized-messenger</b> instances.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">&#40;</span>log-manager<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>make-instance 'log-manager
                     <span style="color: #66cc66;">:</span><span style="color: #555;">message-class</span> 'serialized-message<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>start-messenger 'serialized-messenger <span style="color: #66cc66;">:</span><span style="color: #b1b100;">name</span> <span style="color: #ff0000;">&quot;binary-logger&quot;</span>
                                       <span style="color: #66cc66;">:</span><span style="color: #555;">filename</span> <span style="color: #ff0000;">&quot;/tmp/binary-log.dat&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Once these were started, I made a little utility function to make it easy for me to make test messages and then invoked <b>log-message</b> a few times.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> make-info <span style="color: #66cc66;">&#40;</span>string<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>serialize <span style="color: #66cc66;">:</span><span style="color: #555;">string</span> string <span style="color: #66cc66;">:</span><span style="color: #555;">buffer</span> <span style="color: #66cc66;">&#40;</span>make-buffer<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>log-message <span style="color: #66cc66;">:</span><span style="color: #555;">warning</span> <span style="color: #66cc66;">&#40;</span>make-info <span style="color: #ff0000;">&quot;Warning&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>log-message <span style="color: #66cc66;">:</span><span style="color: #555;">info</span> <span style="color: #66cc66;">&#40;</span>make-info <span style="color: #ff0000;">&quot;This is info&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<h3>Conclusions</h3>
<p>In all, it has taken me about four times as long to write blog post as it did to install cl-log with quicklisp, peek through the cl-log documentation and source code enough to figure out how to do this, and write all of the code.</p>
<p>To really use this, I will probably separate out the category of a message from the serialized type of the message.  This will probably involve adding a field to the <b>serialized-message</b> class to track the message type, adding an <b>initialize-instance :before</b> method for that class to look through the <b>arguments</b> to pull out the type, and then adding the type as an extra argument to <b>log-message</b>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2011/04/binary-logging-with-cl-log/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Tutorial: Introduction to Conditions and Restarts</title>
		<link>http://nklein.com/2011/03/tutorial-introduction-to-conditions-and-restarts/</link>
		<comments>http://nklein.com/2011/03/tutorial-introduction-to-conditions-and-restarts/#comments</comments>
		<pubDate>Sat, 19 Mar 2011 04:08:53 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1621</guid>
		<description><![CDATA[Today was the first time that I really kicked the tires on Common Lisp&#8217;s conditions and restarts. I thought that I&#8217;d share some of the experience as a sort of mini-tutorial. This tutorial assumes some minimal experience hitting the debugger from the REPL and some comfort with CLOS. Lisp: Conditions and Restarts from Patrick Stein [...]]]></description>
			<content:encoded><![CDATA[<p>Today was the first time that I really kicked the tires on Common Lisp&#8217;s conditions and restarts.  I thought that I&#8217;d share some of the experience as a sort of mini-tutorial.  This tutorial assumes some minimal experience hitting the debugger from the REPL and some comfort with CLOS.</p>
<p><iframe src="http://player.vimeo.com/video/21221157?byline=0&amp;portrait=0" width="640" height="400" frameborder="0"></iframe>
<p><a href="http://vimeo.com/21221157">Lisp: Conditions and Restarts</a> from <a href="http://vimeo.com/nklein">Patrick Stein</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p>Screencast tutorial on basic conditions and restarts in Common Lisp.</p>
<p>Here is the <a href="http://nklein.com/wp-content/uploads/2011/03/restarts.lisp" title="restarts.lisp" alt="restarts.lisp">source code generated during this tutorial</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2011/03/tutorial-introduction-to-conditions-and-restarts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Calculating the mean and variance with one pass</title>
		<link>http://nklein.com/2011/02/calculating-the-mean-and-variance-with-one-pass/</link>
		<comments>http://nklein.com/2011/02/calculating-the-mean-and-variance-with-one-pass/#comments</comments>
		<pubDate>Tue, 15 Feb 2011 22:17:07 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[mean]]></category>
		<category><![CDATA[variance]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1563</guid>
		<description><![CDATA[A friend showed me this about 15 years ago. I use it every time I need to calculate the variance of some data set. I always forget the exact details and have to derive it again. But, it&#8217;s easy enough to derive that it&#8217;s never a problem. I had to derive it again on Friday [...]]]></description>
			<content:encoded><![CDATA[<p>A friend showed me this about 15 years ago.  I use it every time I need to calculate the variance of some data set.  I always forget the exact details and have to derive it again.  But, it&#8217;s easy enough to derive that it&#8217;s never a problem.</p>
<p>I had to derive it again on Friday and thought, <q>I should make sure more people get this tool into their utility belts</q>.</p>
<p>First, a quick refresher on what we&#8217;re talking about here.  The mean <img src="http://l.wordpress.com/latex.php?latex=%5Cmu&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\mu" style="vertical-align:-20%;" class="tex" alt="\mu" /> of a data set <img src="http://l.wordpress.com/latex.php?latex=%7B%20x_1%2C%20x_2%2C%20%5Cldots%2C%20x_n%20%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="{ x_1, x_2, \ldots, x_n }" style="vertical-align:-20%;" class="tex" alt="{ x_1, x_2, \ldots, x_n }" /> is defined to be <img src="http://l.wordpress.com/latex.php?latex=%5Cfrac%7B1%7D%7Bn%7D%20%5Csum_%7Bi%3D1%7D%5En%20x_i&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\frac{1}{n} \sum_{i=1}^n x_i" style="vertical-align:-20%;" class="tex" alt="\frac{1}{n} \sum_{i=1}^n x_i" />.  The variance <img src="http://l.wordpress.com/latex.php?latex=%5Csigma%5E2&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\sigma^2" style="vertical-align:-20%;" class="tex" alt="\sigma^2" /> is defined to be <img src="http://l.wordpress.com/latex.php?latex=%5Cfrac%7B1%7D%7Bn%7D%20%5Csum_%7Bi%3D1%7D%5En%20%28x_i%20-%20%5Cmu%29%5E2&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\frac{1}{n} \sum_{i=1}^n (x_i - \mu)^2" style="vertical-align:-20%;" class="tex" alt="\frac{1}{n} \sum_{i=1}^n (x_i - \mu)^2" />.</p>
<p>A naïve approach to calculating the variance then goes something like this:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> mean-variance <span style="color: #66cc66;">&#40;</span>data<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>flet <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>square <span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>* x 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;">let</span>* <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>n <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">length</span> data<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
           <span style="color: #66cc66;">&#40;</span>sum <span style="color: #66cc66;">&#40;</span>reduce #'+ data <span style="color: #66cc66;">:</span><span style="color: #555;">initial-</span><span style="color: #b1b100;">value</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
           <span style="color: #66cc66;">&#40;</span>mu <span style="color: #66cc66;">&#40;</span>/ sum n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
           <span style="color: #66cc66;">&#40;</span>vv <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>accum xi<span style="color: #66cc66;">&#41;</span>
                           <span style="color: #66cc66;">&#40;</span>+ accum <span style="color: #66cc66;">&#40;</span>square <span style="color: #66cc66;">&#40;</span>- xi mu<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                       data <span style="color: #66cc66;">:</span><span style="color: #555;">initial-</span><span style="color: #b1b100;">value</span> <span style="color: #cc66cc;">0</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>values mu <span style="color: #66cc66;">&#40;</span>/ vv 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><span style="color: #66cc66;">&#41;</span></pre></div>
<p>This code runs through the data list once to count the items, once to calculate the mean, and once to calculate the variance.  It is easy to see how we could count the items at the same time we are summing them.  It is not as obvious how we can calculate the sum of squared terms involving the mean until we&#8217;ve calculated the mean.</p>
<p>If we expand the squared term and pull the constant <img src="http://l.wordpress.com/latex.php?latex=%5Cmu&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\mu" style="vertical-align:-20%;" class="tex" alt="\mu" /> outside of the summations it ends up in, we find that:<br />
<center><img src="http://l.wordpress.com/latex.php?latex=%5Cfrac%7B%5Csum%20%28x_i%20-%20%5Cmu%29%5E2%7D%7Bn%7D%20%3D%20%5Cfrac%7B%5Csum%20x_i%5E2%7D%7Bn%7D%20-%202%20%5Cmu%20%5Cfrac%7B%5Csum%20x_i%7D%7Bn%7D%20%2B%20%5Cmu%5E2%20%5Cfrac%7B%5Csum%201%7D%7Bn%7D%20&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\frac{\sum (x_i - \mu)^2}{n} = \frac{\sum x_i^2}{n} - 2 \mu \frac{\sum x_i}{n} + \mu^2 \frac{\sum 1}{n} " style="vertical-align:-20%;" class="tex" alt="\frac{\sum (x_i - \mu)^2}{n} = \frac{\sum x_i^2}{n} - 2 \mu \frac{\sum x_i}{n} + \mu^2 \frac{\sum 1}{n} " /></center></p>
<p>When we recognize that <img src="http://l.wordpress.com/latex.php?latex=%5Cfrac%7B%5Csum%20x_i%7D%7Bn%7D%20%3D%20%5Cmu&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\frac{\sum x_i}{n} = \mu" style="vertical-align:-20%;" class="tex" alt="\frac{\sum x_i}{n} = \mu" /> and <img src="http://l.wordpress.com/latex.php?latex=%5Csum_%7Bi%3D1%7D%5En%201%20%3D%20n&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\sum_{i=1}^n 1 = n" style="vertical-align:-20%;" class="tex" alt="\sum_{i=1}^n 1 = n" />, we get:<br />
<center><img src="http://l.wordpress.com/latex.php?latex=%5Csigma%5E2%20%3D%20%5Cfrac%7B%5Csum%20x_i%5E2%7D%7Bn%7D%20-%20%5Cmu%5E2%20%3D%20%5Cfrac%7B%5Csum%20x_i%5E2%7D%7Bn%7D%20-%20%5Cleft%28%20%5Cfrac%7B%5Csum%20x_i%7D%7Bn%7D%20%5Cright%29%5E2&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\sigma^2 = \frac{\sum x_i^2}{n} - \mu^2 = \frac{\sum x_i^2}{n} - \left( \frac{\sum x_i}{n} \right)^2" style="vertical-align:-20%;" class="tex" alt="\sigma^2 = \frac{\sum x_i^2}{n} - \mu^2 = \frac{\sum x_i^2}{n} - \left( \frac{\sum x_i}{n} \right)^2" /></center>.</p>
<p>This leads to the following code:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> mean-variance <span style="color: #66cc66;">&#40;</span>data<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>flet <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>square <span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>* x 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>destructuring-bind <span style="color: #66cc66;">&#40;</span>n xs x2s<span style="color: #66cc66;">&#41;</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>accum xi<span style="color: #66cc66;">&#41;</span>
                    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>+ <span style="color: #66cc66;">&#40;</span>first accum<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                          <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>second accum<span style="color: #66cc66;">&#41;</span> xi<span style="color: #66cc66;">&#41;</span>
                          <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>third accum<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>square xi<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                data <span style="color: #66cc66;">:</span><span style="color: #555;">initial-</span><span style="color: #b1b100;">value</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><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>mu <span style="color: #66cc66;">&#40;</span>/ xs n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>values mu <span style="color: #66cc66;">&#40;</span>- <span style="color: #66cc66;">&#40;</span>/ x2s n<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>square mu<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></pre></div>
<p>The code is not as simple, but you gain a great deal of flexibility.  You can easily convert the above concept to continuously track the mean and variance as you iterate through an input stream.  You do not have to keep data around to iterate through later.  You can deal with things one sample at a time.</p>
<p>The same concept extends to higher-order moments, too.</p>
<p>Happy counting.</p>
<p><strong>Edit:</strong> As many have pointed out, this isn&#8217;t the most numerically stable way to do this calculation.  For my part, I was doing it with Lisp integers, so I&#8217;ve got all of the stability I could ever want.  <img src='http://nklein.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />    But, yes&#8230;. if you are intending to use these numbers for big-time decision making, you probably want to look up a really stable algorithm.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2011/02/calculating-the-mean-and-variance-with-one-pass/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A Currying Pipeline</title>
		<link>http://nklein.com/2010/11/a-currying-pipeline/</link>
		<comments>http://nklein.com/2010/11/a-currying-pipeline/#comments</comments>
		<pubDate>Sat, 20 Nov 2010 15:14:18 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1538</guid>
		<description><![CDATA[I have been reading a great deal about Haskell and thinking a great deal about a networked Lisp game that I intend to work on soon. For the Lisp project, I will need to serialize and unserialize packets to send them over the network. I re-read the chapter on parsing binary files in Practical Common [...]]]></description>
			<content:encoded><![CDATA[<p>I have been reading a great deal about Haskell and thinking a great deal about a networked Lisp game that I intend to work on soon.  For the Lisp project, I will need to serialize and unserialize packets to send them over the network.  I re-read the chapter on parsing binary files in Practical Common Lisp and started to think about how I could make readers and writers that worked on buffers.  Thanks to the Haskell influence, I was also trying to do this serialization without side effects.</p>
<h3>The Goal</h3>
<p>I wanted to accomplish something like this without all of the <b>SETF</b> action and verbiage:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span>vv<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> vv <span style="color: #66cc66;">&#40;</span>serialize 'real xx vv<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> vv <span style="color: #66cc66;">&#40;</span>serialize 'real yy vv<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> vv <span style="color: #66cc66;">&#40;</span>serialize 'real zz vv<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  vv<span style="color: #66cc66;">&#41;</span></pre></div>
<h3>The First Attempt</h3>
<p>Well, also thanks to Haskell, my instinct was to make a <b>CURRY-PIPELINE</b> macro that gets called something like this:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>curry-pipeline <span style="color: #b1b100;">nil</span>
  <span style="color: #66cc66;">&#40;</span>serialize 'real xx<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>serialize 'real yy<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>serialize 'real zz<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>and expands into something like this:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>serialize 'real zz <span style="color: #66cc66;">&#40;</span>serialize 'real yy <span style="color: #66cc66;">&#40;</span>serialize 'real xx <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Unfortunately, this changes the order of evaluation of <b>xx</b>, <b>yy</b>, and <b>zz</b> entirely.  So, this was suboptimal.  It also involved a fifteen-line macro with macro recursion and two conditionals.</p>
<h3>Slightly Cleaner</h3>
<p>My next attempt was about a ten-line macro with one conditional that turned it into a bunch of nested <b>LET</b> statements.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>#<span style="color: #66cc66;">:</span><span style="color: #555;">V-</span><span style="color: #cc66cc;">1</span> <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;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>#<span style="color: #66cc66;">:</span><span style="color: #555;">V-</span><span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#40;</span>serialize 'real xx #<span style="color: #66cc66;">:</span><span style="color: #555;">V-</span><span style="color: #cc66cc;">1</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;">let</span> <span style="color: #66cc66;">&#40;</span>#<span style="color: #66cc66;">:</span><span style="color: #555;">V-</span><span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#40;</span>serialize 'real yy #<span style="color: #66cc66;">:</span><span style="color: #555;">V-</span><span style="color: #cc66cc;">1</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;">let</span> <span style="color: #66cc66;">&#40;</span>#<span style="color: #66cc66;">:</span><span style="color: #555;">V-</span><span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#40;</span>serialize 'real zz #<span style="color: #66cc66;">:</span><span style="color: #555;">V-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        #<span style="color: #66cc66;">:</span><span style="color: #555;">V-</span><span style="color: #cc66cc;">1</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></div>
<h3>Cleaner Still</h3>
<p>Then, I realized that most of my simple examples would be simplest if I curried into the first argument instead of the last.  (In fact, it would have even fixed my order of evaluation problem in the initial version.)  And, I realized that I could abandon the nested <b>LET</b> if I used a <b>LET*</b>.  Now, I have a six-line macro that I really like.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> curry-pipeline <span style="color: #66cc66;">&#40;</span>initial <span style="color: #66cc66;">&amp;</span>body body<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>vv <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">gensym</span> <span style="color: #ff0000;">&quot;VARIABLE-&quot;</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>labels <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>curry-and-store <span style="color: #66cc66;">&#40;</span>line<span style="color: #66cc66;">&#41;</span>
               `<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">,</span>vv <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span>first line<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">,</span>vv <span style="color: #66cc66;">,</span>@<span style="color: #66cc66;">&#40;</span>rest line<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      `<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span>* <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">,</span>vv <span style="color: #66cc66;">,</span>initial<span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">,</span>@<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">mapcar</span> #'curry-and-store body<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">,</span>vv<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>
<p>So, here is an example that does one of those grade school magic tricks.  Pick a number, multiply by five, add six, multiply by four, add nine, and multiply by five.  Tell me the answer.  I subtract 165 and divide by one hundred to tell you your original number.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> magic-trick <span style="color: #66cc66;">&#40;</span>n<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>rube <span style="color: #66cc66;">&#40;</span>curry-pipeline n
                <span style="color: #66cc66;">&#40;</span>* <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>+ <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>* <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>+ <span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>* <span style="color: #cc66cc;">5</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>curry-pipeline rube
      <span style="color: #66cc66;">&#40;</span>- <span style="color: #cc66cc;">165</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>/ <span style="color: #cc66cc;">100</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></div>
<h3>Back to the Original Problem</h3>
<p>Now, my serialize functions can simply take a buffer and return a buffer which is the concatenation of the input buffer and the bytes required to encode the given value.  The unserialize is not as nice since I will have to return both a buffer and a value, but I am sure I can work something out using a <b>CONS</b> as an accumulator.  And, heck, it is going to kill my performance anyway if I really copy the buffer every time I want to add another item to it.  I am probably going to ditch the functional aspect anyway.  *shrug*</p>
<h3>A New Tack</h3>
<p>If you don&#8217;t like currying or need to have more control over where the accumulator goes in each step of the chain, you can still get the same kind of chaining if you require a declaration of the variable.  And, it simplifies the macro:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> let-pipeline <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>var initial<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;</span>body body<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><span style="color: #66cc66;">,</span>var <span style="color: #66cc66;">,</span>initial<span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">,</span>@<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">mapcar</span> #'<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>line<span style="color: #66cc66;">&#41;</span> `<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">,</span>var <span style="color: #66cc66;">,</span>line<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> body<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">,</span>var<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> magic-trick <span style="color: #66cc66;">&#40;</span>n<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>rube <span style="color: #66cc66;">&#40;</span>let-pipeline <span style="color: #66cc66;">&#40;</span>r n<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>* <span style="color: #cc66cc;">5</span> r<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>+ <span style="color: #cc66cc;">6</span> r<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>* <span style="color: #cc66cc;">4</span> r<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>+ <span style="color: #cc66cc;">9</span> r<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>* <span style="color: #cc66cc;">5</span> r<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>let-pipeline <span style="color: #66cc66;">&#40;</span>a rube<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>- a <span style="color: #cc66cc;">165</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>/ a <span style="color: #cc66cc;">100</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></div>
<p>From there, it is not too big of a leap to allow <b>MULTIPLE-VALUE-BIND</b> instead.  To accomplish the unserialize, as I mentioned, I will need to track multiple values.  I am now down to a four line macro:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> mv-pipeline <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>vars call <span style="color: #66cc66;">&amp;</span>rest rest-pipeline<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;</span>body body<span style="color: #66cc66;">&#41;</span>
  `<span style="color: #66cc66;">&#40;</span>multiple-value-bind <span style="color: #66cc66;">,</span>vars <span style="color: #66cc66;">,</span>call
     <span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> rest-pipeline
          `<span style="color: #66cc66;">&#40;</span>mv-pipeline <span style="color: #66cc66;">,</span>rest-pipeline <span style="color: #66cc66;">,</span>@body<span style="color: #66cc66;">&#41;</span>
          `<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">progn</span> <span style="color: #66cc66;">,</span>@body<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>
<p>Here is an easy to follow example that returns the first five Fibonacci numbers:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>mv-pipeline <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>f0 f1<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>values <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#40;</span>f2<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>+ f1 f0<span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#40;</span>f3<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>+ f2 f1<span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#40;</span>f4<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>+ f3 f2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> f0 f1 f2 f3 f4<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Now, my unserialize case might look something like this:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>defmethod unserialize <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>type <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eq</span> 'vector<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> buffer<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>mv-pipeline <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>xx buffer<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>unserialize 'real buffer<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>yy buffer<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>unserialize 'real buffer<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>zz buffer<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>unserialize 'real buffer<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>values <span style="color: #66cc66;">&#40;</span>vector xx yy zz<span style="color: #66cc66;">&#41;</span> buffer<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<h3>Conclusion</h3>
<p>Luckily, I am not paying myself based on lines of code per hour.  Almost every time I have done more work, I have reduced the number of lines of code.  I am reminded of this quote from Blaise Pascal: <q>I have made this letter longer, because I have not had the time to make it shorter.</q></p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2010/11/a-currying-pipeline/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

