<?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</title>
	<atom:link href="http://nklein.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://nklein.com</link>
	<description>software development and consulting</description>
	<lastBuildDate>Fri, 10 May 2013 14:41:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Track-Best Library Released</title>
		<link>http://nklein.com/2013/05/track-best-library-released/</link>
		<comments>http://nklein.com/2013/05/track-best-library-released/#comments</comments>
		<pubDate>Thu, 09 May 2013 23:27:00 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Initial Releases]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Releases]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[track-best]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1970</guid>
		<description><![CDATA[In doing a problem set from the Internet a few weeks ago, I found myself writing awkward constructions with REDUCE and LOOP to try to find the best one (or two or three) things in a big bag of things based on various criteria: similarity to English, hamming distance from their neighbor, etc. I wrote [...]]]></description>
				<content:encoded><![CDATA[<p>In doing a problem set from the Internet a few weeks ago, I found myself writing awkward constructions with <code class="codecolorer lisp default"><span class="lisp">REDUCE</span></code> and <code class="codecolorer lisp default"><span class="lisp">LOOP</span></code> to try to find the best one (or two or three) things in a big bag of things based on various criteria: similarity to English, hamming distance from their neighbor, etc.</p>
<p>I wrote a macro that encapsulated the pattern.  I&#8217;ve reworked that macro into a library for public use.</p>
<h3>Acquiring</h3>
<ul>
<li>Home page: <a href="http://nklein.com/software/track-best-library/">http://nklein.com/software/track-best-library/</a></li>
<li>Main git repository: <a href="http://git.nklein.com/lisp/libs/track-best.git">http://git.nklein.com/lisp/libs/track-best.git</a></li>
<li>Browsable repository: <a href="https://github.com/nklein/track-best">https://github.com/nklein/track-best</a></li>
</ul>
<h3>Using</h3>
<p>There are a variety of examples in the <a href="https://github.com/nklein/track-best/blob/master/README.md">README</a> and the <a href="https://github.com/nklein/track-best/tree/master/tests">tests directory</a>.</p>
<p>Here is one example to pique your interest.  Suppose you have some data about the elevations of various cities in various states and you (being a Moxy Fruvous fan) want to know <a href="http://www.lyricsmania.com/the_lowest_highest_point_improv_lyrics_moxy_fruvous.html"><q>What Is the Lowest Highest Point?</q></a>  Here&#8217;s how you might tackle that with the <code class="codecolorer lisp default"><span class="lisp">TRACK-BEST</span></code> library:</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>data '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Alabama&quot;</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Birmingham&quot;</span> <span style="color: #cc66cc;">664</span><span style="color: #66cc66;">&#41;</span>
                          <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Mobile&quot;</span> <span style="color: #cc66cc;">218</span><span style="color: #66cc66;">&#41;</span>
                          <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Montegomery&quot;</span> <span style="color: #cc66cc;">221</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Alaska&quot;</span>   <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Anchorage&quot;</span> <span style="color: #cc66cc;">144</span><span style="color: #66cc66;">&#41;</span>
                          <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Fairbanks&quot;</span> <span style="color: #cc66cc;">531</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Arizona&quot;</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Grand Canyon&quot;</span> <span style="color: #cc66cc;">6606</span><span style="color: #66cc66;">&#41;</span>
                          <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Phoenix&quot;</span> <span style="color: #cc66cc;">1132</span><span style="color: #66cc66;">&#41;</span>
                          <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Tuscon&quot;</span>  <span style="color: #cc66cc;">2641</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>with-track-best <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">order-by-fn</span> #'<span style="color: #66cc66;">&lt;</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>state-info data<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>multiple-value-bind <span style="color: #66cc66;">&#40;</span>city altitude<span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span>with-track-best <span style="color: #66cc66;">&#40;</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>city-info <span style="color: #66cc66;">&#40;</span>rest state-info<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#40;</span>track <span style="color: #66cc66;">&#40;</span>first city-info<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>second city-info<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>track <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>first state-info<span style="color: #66cc66;">&#41;</span> city<span style="color: #66cc66;">&#41;</span> altitude<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>With this limited dataset, the end result would be <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&#40;</span>VALUES '<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Alaska&quot;</span> <span style="color: #ff0000;">&quot;Fairbanks&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">531</span><span style="color: #66cc66;">&#41;</span></span></code>.  The inner <code class="codecolorer lisp default"><span class="lisp">WITH-TRACK-BEST</span></code> finds the highest city in each state.  The outer <code class="codecolorer lisp default"><span class="lisp">WITH-TRACK-BEST</span></code> finds the lowest of these.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2013/05/track-best-library-released/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Building the language up to you</title>
		<link>http://nklein.com/2013/05/building-the-language-up-to-you/</link>
		<comments>http://nklein.com/2013/05/building-the-language-up-to-you/#comments</comments>
		<pubDate>Sun, 05 May 2013 06:05:55 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1956</guid>
		<description><![CDATA[One of the things that&#8217;s often said about Lisp is that it allows you to build up the language to you. Here are two examples that really drive home that point, for me. The Problem Domain I read an article about Treaps on reddit yesterday. The article used pretty direct pylisp to present Treaps. I [...]]]></description>
				<content:encoded><![CDATA[<p>One of the things that&#8217;s often said about Lisp is that it allows you to <a href="http://dunsmor.com/lisp/onlisp/onlisp_5.html">build up the language</a> to you.  Here are two examples that really drive home that point, for me.</p>
<h2>The Problem Domain</h2>
<p>I read <a href="http://pavpanchekha.com/blog/treap.html">an article about Treaps</a> on reddit yesterday.  The article used pretty direct pylisp to present Treaps.</p>
<p>I thought it would be fun to go through the exercise in Common Lisp instead.  Pylisp made a number of things awkward that would melt away in Common Lisp.</p>
<p>I began by defining a node structure to encapsulate the information at a given node:</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>defstruct node
  <span style="color: #66cc66;">&#40;</span>priority <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> 1<span style="color: #66cc66;">.</span>0d0<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">type</span> real <span style="color: #66cc66;">:</span><span style="color: #555;">read-only</span> t<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>key <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">:</span><span style="color: #555;">read-only</span> t<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">value</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">:</span><span style="color: #555;">read-only</span> t<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>left <span style="color: #b1b100;">nil</span> <span style="color: #66cc66;">:</span><span style="color: #555;">type</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">or</span> <span style="color: #b1b100;">null</span> node<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">read-only</span> t<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>right <span style="color: #b1b100;">nil</span> <span style="color: #66cc66;">:</span><span style="color: #555;">type</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">or</span> <span style="color: #b1b100;">null</span> node<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">read-only</span> t<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Then, I defined a top-level structure to hold the root node, track the function used to sort the tree, and track the function used to create a key from a value.  I used the convention that two keys are equivalent if neither is less than the other.</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>defstruct treap
  <span style="color: #66cc66;">&#40;</span>root <span style="color: #b1b100;">nil</span> <span style="color: #66cc66;">:</span><span style="color: #555;">type</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">or</span> <span style="color: #b1b100;">null</span> node<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">read-only</span> t<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>less-than #'<span style="color: #66cc66;">&lt;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">read-only</span> t<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>key #'<span style="color: #b1b100;">identity</span> <span style="color: #66cc66;">:</span><span style="color: #555;">read-only</span> t<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<h2>The First Build Up</h2>
<p>The article was using a functional style.  As you may have guessed by the liberal use of <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">:</span><span style="color: #555;">read-only</span> t</span></code> in those <code class="codecolorer lisp default"><span class="lisp">DEFSTRUCT</span></code> clauses, I also used a functional style.</p>
<p>When working with functional data structures, one often needs to copy a whole structure  with only slight modifications.  Here, Lisp&#8217;s keyword arguments made everything simple and clean.  I made this functions:</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> copy-node <span style="color: #66cc66;">&#40;</span>node <span style="color: #66cc66;">&amp;</span>key <span style="color: #66cc66;">&#40;</span>priority <span style="color: #66cc66;">&#40;</span>node-priority node<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                            <span style="color: #66cc66;">&#40;</span>key <span style="color: #66cc66;">&#40;</span>node-key node<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                            <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">value</span> <span style="color: #66cc66;">&#40;</span>node-<span style="color: #b1b100;">value</span> node<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                            <span style="color: #66cc66;">&#40;</span>left <span style="color: #66cc66;">&#40;</span>node-left node<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                            <span style="color: #66cc66;">&#40;</span>right <span style="color: #66cc66;">&#40;</span>node-right node<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>make-node <span style="color: #66cc66;">:</span><span style="color: #555;">priority</span> priority
             <span style="color: #66cc66;">:</span><span style="color: #555;">key</span> key
             <span style="color: #66cc66;">:</span><span style="color: #b1b100;">value</span> <span style="color: #b1b100;">value</span>
             <span style="color: #66cc66;">:</span><span style="color: #555;">left</span> left
             <span style="color: #66cc66;">:</span><span style="color: #555;">right</span> right<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Now, for any node, I could copy it and only have to specify the fields that I wanted to change.  Here is a left-rotation using 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> left-rotate <span style="color: #66cc66;">&#40;</span>node<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>left <span style="color: #66cc66;">&#40;</span>node-left node<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>copy-node left
               <span style="color: #66cc66;">:</span><span style="color: #555;">right</span> <span style="color: #66cc66;">&#40;</span>copy-node node
                                 <span style="color: #66cc66;">:</span><span style="color: #555;">left</span> <span style="color: #66cc66;">&#40;</span>node-right left<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>What other languages let you do anything like that so simply?  You could pull it off in Perl if you were willing to have your node be a hash (which, admittedly, you probably are if you&#8217;re writing Perl).  What other language lets you do anything like that?  Even other languages with named arguments don&#8217;t let you have the defaults based on other arguments.</p>
<h2>The Second Build Up</h2>
<p>As with any binary tree, you find yourself having to deal with four specific cases over and over again with Treaps.</p>
<ul>
<li>You ran out of tree</li>
<li>Your key is less than the current node&#8217;s key</li>
<li>Your key is greater than the current node&#8217;s key</li>
<li>Your key is equivalent to the current node&#8217;s key</li>
</ul>
<p>I wrote myself a <code class="codecolorer lisp default"><span class="lisp">TREAP-CASES</span></code> macro that streamlines all of these checks.  It also validates to make sure you don&#8217;t have duplicate cases or cases other than these four.  It makes sure that no matter which order you write the cases (or even if you leave some out), they end up organized in the order listed above.  The four cases are mutually exclusive so the order doesn&#8217;t matter except that you want to make sure you haven&#8217;t run out of tree before doing comparisons and that once you&#8217;re beyond the less-than and greater-than cases you already know you&#8217;re in the equivalence case.</p>
<p>With this macro, my <code class="codecolorer lisp default"><span class="lisp">TREAP-FIND</span></code> function looks 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> treap-find <span style="color: #66cc66;">&#40;</span>key treap<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>check-type treap treap<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>treap-node-find <span style="color: #66cc66;">&#40;</span>root<span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>treap-cases <span style="color: #66cc66;">&#40;</span>key root treap<span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null</span> <span style="color: #66cc66;">&#40;</span>values <span style="color: #b1b100;">nil</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: #66cc66;">&lt;</span> <span style="color: #66cc66;">&#40;</span>treap-node-find <span style="color: #66cc66;">&#40;</span>node-left root<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: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>treap-node-find <span style="color: #66cc66;">&#40;</span>node-right root<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: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>values <span style="color: #66cc66;">&#40;</span>node-<span style="color: #b1b100;">value</span> root<span style="color: #66cc66;">&#41;</span> t<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>treap-node-find <span style="color: #66cc66;">&#40;</span>treap-root treap<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 little DSL makes writing and reading the code so much easier.  All of the mess of comparing the key to the node&#8217;s key is hidden away.</p>
<p>With years of practice and days of debugging, you might be able to pull off some quasi-readable control construct like this using C++ templates.  With enough therapy, you could convince yourself you can get a close-enough effect with C-preprocessor macros.  In Lisp, it&#8217;s the work of minutes (without lying to yourself).</p>
<p>Here is the <code class="codecolorer lisp default"><span class="lisp">TREAP-CASES</span></code> macro for reference/completeness.</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> treap-cases <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>key root treap<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;</span>rest clauses<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>validate-treap-case-clauses clauses<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>k <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">gensym</span> <span style="color: #ff0000;">&quot;KEY-&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>tr <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">gensym</span> <span style="color: #ff0000;">&quot;TREAP-&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>r <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">gensym</span> <span style="color: #ff0000;">&quot;ROOT-&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>t<span style="color: #66cc66;">&lt;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">gensym</span> <span style="color: #ff0000;">&quot;&lt;-&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><span style="color: #b1b100;">let</span>* <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">,</span>k <span style="color: #66cc66;">,</span>key<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">,</span>tr <span style="color: #66cc66;">,</span>treap<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">,</span>r <span style="color: #66cc66;">,</span>root<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">,</span>t<span style="color: #66cc66;">&lt;</span> <span style="color: #66cc66;">&#40;</span>treap-less-than <span style="color: #66cc66;">,</span>tr<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;">cond</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null</span> <span style="color: #66cc66;">,</span>r<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">,</span>@<span style="color: #66cc66;">&#40;</span>rest <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">assoc</span> '<span style="color: #b1b100;">null</span> clauses<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: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> <span style="color: #66cc66;">,</span>t<span style="color: #66cc66;">&lt;</span> <span style="color: #66cc66;">,</span>k <span style="color: #66cc66;">&#40;</span>node-key <span style="color: #66cc66;">,</span>r<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">,</span>@<span style="color: #66cc66;">&#40;</span>rest <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">assoc</span> '<span style="color: #66cc66;">&lt;</span> clauses<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: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> <span style="color: #66cc66;">,</span>t<span style="color: #66cc66;">&lt;</span> <span style="color: #66cc66;">&#40;</span>node-key <span style="color: #66cc66;">,</span>r<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">,</span>k<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">,</span>@<span style="color: #66cc66;">&#40;</span>rest <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">assoc</span> '<span style="color: #66cc66;">&gt;</span> clauses<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>t <span style="color: #66cc66;">,</span>@<span style="color: #66cc66;">&#40;</span>rest <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">assoc</span> '<span style="color: #66cc66;">=</span> clauses<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>I suppose I should also include <code class="codecolorer lisp default"><span class="lisp">#'VALIDATE-TREAP-CASE-CLAUSES</span></code>, too, but it&#8217;s what you&#8217;d expect:</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> validate-treap-case-clauses <span style="color: #66cc66;">&#40;</span>clauses<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>all-choices '<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">=</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>flet <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>assert-all-choices-valid <span style="color: #66cc66;">&#40;</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>c clauses<span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#40;</span>unless <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">member</span> <span style="color: #66cc66;">&#40;</span>first c<span style="color: #66cc66;">&#41;</span> all-choices<span style="color: #66cc66;">&#41;</span>
                 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">error</span> <span style="color: #ff0000;">&quot;Unrecognized clause type: ~S&quot;</span> <span style="color: #66cc66;">&#40;</span>first c<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>assert-no-duplicates <span style="color: #66cc66;">&#40;</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>c all-choices<span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#40;</span>unless <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;=</span> <span style="color: #66cc66;">&#40;</span>count c clauses <span style="color: #66cc66;">:</span><span style="color: #555;">key</span> #'first<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
                 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">error</span> <span style="color: #ff0000;">&quot;Duplicate ~S clause not allowed.&quot;</span> c<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>assert-all-choices-valid<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>assert-no-duplicates<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>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2013/05/building-the-language-up-to-you/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Two snippets that I rewrite every three or four months&#8230;</title>
		<link>http://nklein.com/two-snippets-I-rewrite-too-often</link>
		<comments>http://nklein.com/two-snippets-I-rewrite-too-often#comments</comments>
		<pubDate>Tue, 23 Apr 2013 05:09:41 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[group theory]]></category>
		<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1948</guid>
		<description><![CDATA[A friend of mine is taking a cryptography course this semester. One of his current homework problems involves creating , , and values for RSA Encryption. Always one to play the home game when math is involved, I found myself rewriting two snippets of code that I always end up writing any time I do [...]]]></description>
				<content:encoded><![CDATA[<p>A friend of mine is taking a cryptography course this semester.  One of his current homework problems involves creating <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" />, <img src="http://l.wordpress.com/latex.php?latex=e&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="e" style="vertical-align:-20%;" class="tex" alt="e" />, and <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_fa25a1c273232af978e96f1ea4331d79.png" title="d" style="vertical-align:-20%;" class="tex" alt="d" /> values for <a href="http://en.wikipedia.org/wiki/RSA_(algorithm)">RSA Encryption</a>.  Always one to play the home game when math is involved, I found myself rewriting two snippets of code that I always end up writing any time I do any number-theory related coding.</p>
<p>One of them is simple <code class="codecolorer lisp default"><span class="lisp">EXPONENT-MODULO</span></code> that takes a base <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_97f4a1ba736119e3cd4de6fca35efd6b.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" />, an exponent <img src="http://l.wordpress.com/latex.php?latex=e&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="e" style="vertical-align:-20%;" class="tex" alt="e" />, and a modulus <img src="http://l.wordpress.com/latex.php?latex=m&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="m" style="vertical-align:-20%;" class="tex" alt="m" /> and returns <img src="http://l.wordpress.com/latex.php?latex=b%5Ee%20%5Cpmod%20m&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="b^e \pmod m" style="vertical-align:-20%;" class="tex" alt="b^e \pmod m" />.  There are more efficient ways to do this, but this one is much faster than the naïve method and quite easy to rewrite every time that I need it.  The idea is that when <img src="http://l.wordpress.com/latex.php?latex=e&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="e" style="vertical-align:-20%;" class="tex" alt="e" /> is odd, you recursively calculate <img src="http://l.wordpress.com/latex.php?latex=b%5E%7Be-1%7D%20%5Cpmod%20m&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="b^{e-1} \pmod m" style="vertical-align:-20%;" class="tex" alt="b^{e-1} \pmod m" /> and return <img src="http://l.wordpress.com/latex.php?latex=b%20%5Ccdot%20b%5E%7Be-1%7D%20%5Cpmod%20m&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="b \cdot b^{e-1} \pmod m" style="vertical-align:-20%;" class="tex" alt="b \cdot b^{e-1} \pmod m" />.  When <img src="http://l.wordpress.com/latex.php?latex=e&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="e" style="vertical-align:-20%;" class="tex" alt="e" /> is even, you recursively calculate <img src="http://l.wordpress.com/latex.php?latex=a%20%3D%20b%5E%7Be%2F2%7D%20%5Cpmod%20m&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="a = b^{e/2} \pmod m" style="vertical-align:-20%;" class="tex" alt="a = b^{e/2} \pmod m" /> and return <img src="http://l.wordpress.com/latex.php?latex=a%5E2%20%5Cpmod%20m&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="a^2 \pmod m" style="vertical-align:-20%;" class="tex" alt="a^2 \pmod m" />.</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> expt-mod <span style="color: #66cc66;">&#40;</span>b e m<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">zerop</span> e<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">evenp</span> e<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>expt-mod b <span style="color: #66cc66;">&#40;</span>/ e <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                 <span style="color: #66cc66;">&#40;</span>mod <span style="color: #66cc66;">&#40;</span>* a a<span style="color: #66cc66;">&#41;</span> m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>t <span style="color: #66cc66;">&#40;</span>mod <span style="color: #66cc66;">&#40;</span>* b <span style="color: #66cc66;">&#40;</span>expt-mod b <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>- e<span style="color: #66cc66;">&#41;</span> m<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> m<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 other snippet of code is a good deal trickier.  Every time I need it, I spend way more time than I want to futzing with examples, getting lost in subscripts, etc.  So, while I could rewrite the above in a moment, what&#8217;s below, I hope I have the search-fu to find this post next time I need it.</p>
<p>If you have a number <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> and some number <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_3cad95c81df4676e1b14e93cbb0b18ae.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" /> which is relatively prime to (shares no factors except for one with) <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" />, then there exists some number <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_97f4a1ba736119e3cd4de6fca35efd6b.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" /> such that <img src="http://l.wordpress.com/latex.php?latex=a%20%5Ccdot%20b%20%5Cequiv%201%20%5Cpmod%20n&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="a \cdot b \equiv 1 \pmod n" style="vertical-align:-20%;" class="tex" alt="a \cdot b \equiv 1 \pmod n" />.  And, in fact, all such numbers <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_97f4a1ba736119e3cd4de6fca35efd6b.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" /> for a given <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_3cad95c81df4676e1b14e93cbb0b18ae.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" /> differ by a multiple of <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" />, so there is a unique such <img src="http://l.wordpress.com/latex.php?latex=b%20%5Cin%20%5B1%2Cn%29&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="b \in [1,n)" style="vertical-align:-20%;" class="tex" alt="b \in [1,n)" />.</p>
<p>Another way of saying <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_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> are relatively prime is to say that their greatest common divisor <img src="http://l.wordpress.com/latex.php?latex=%5Cgcd%28a%2Cn%29&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\gcd(a,n)" style="vertical-align:-20%;" class="tex" alt="\gcd(a,n)" /> is one.  Besides being the biggest number that divides into both <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> and <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 <img src="http://l.wordpress.com/latex.php?latex=%5Cgcd%28a%2Cn%29&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\gcd(a,n)" style="vertical-align:-20%;" class="tex" alt="\gcd(a,n)" /> is also the smallest positive number expressible as <img src="http://l.wordpress.com/latex.php?latex=x%20%5Ccdot%20a%20%2B%20y%20%5Ccdot%20n&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x \cdot a + y \cdot n" style="vertical-align:-20%;" class="tex" alt="x \cdot a + y \cdot n" /> for integers <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 <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e561f1e00880abb7e58816455af00a48.png" title="y" style="vertical-align:-20%;" class="tex" alt="y" />.  If you keep track of the steps you use when calculating the greatest common divisor (which in this case is <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e04a0c4d52e7f8537abf80bb9beb4a8a.png" title="1" style="vertical-align:-20%;" class="tex" alt="1" />) while using the <a href="http://en.wikipedia.org/wiki/Euclidean_algorithm">Euclidean algorithm</a> you can (with enough care) backtrack through the steps and determine <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 <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e561f1e00880abb7e58816455af00a48.png" title="y" style="vertical-align:-20%;" class="tex" alt="y" /> for your given <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_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" />.</p>
<p>For example, suppose you had <img src="http://l.wordpress.com/latex.php?latex=n%20%3D%2040&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="n = 40" style="vertical-align:-20%;" class="tex" alt="n = 40" /> and <img src="http://l.wordpress.com/latex.php?latex=a%20%3D%2031&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="a = 31" style="vertical-align:-20%;" class="tex" alt="a = 31" />.  You want to know what <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_97f4a1ba736119e3cd4de6fca35efd6b.png" title="b" style="vertical-align:-20%;" class="tex" alt="b" /> you&#8217;d need for <img src="http://l.wordpress.com/latex.php?latex=31b%20%5Cequiv%201%20%5Cpmod%7B40%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="31b \equiv 1 \pmod{40}" style="vertical-align:-20%;" class="tex" alt="31b \equiv 1 \pmod{40}" />.<br />
The steps in the Euclidean algorithm show:</p>
<p><center><img src="http://l.wordpress.com/latex.php?latex=%5Cbegin%7Barray%7D%7Brcl%7D40%20%26%20%3D%20%26%2031%20%5Ccdot%201%20%2B%209%20%5C%5C31%20%26%20%3D%20%26%209%20%5Ccdot%203%20%2B%204%20%5C%5C9%20%26%20%3D%20%26%204%20%5Ccdot%202%20%2B%201%5Cend%7Barray%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\begin{array}{rcl}40 &#038; = &#038; 31 \cdot 1 + 9 \\31 &#038; = &#038; 9 \cdot 3 + 4 \\9 &#038; = &#038; 4 \cdot 2 + 1\end{array}" style="vertical-align:-20%;" class="tex" alt="\begin{array}{rcl}40 &#038; = &#038; 31 \cdot 1 + 9 \\31 &#038; = &#038; 9 \cdot 3 + 4 \\9 &#038; = &#038; 4 \cdot 2 + 1\end{array}" /></center></p>
<p>Working backwards, you can express <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e04a0c4d52e7f8537abf80bb9beb4a8a.png" title="1" style="vertical-align:-20%;" class="tex" alt="1" /> as a combination of <img src="http://l.wordpress.com/latex.php?latex=x_0%20%5Ccdot%204%20%2B%20y_0%20%5Ccdot%209&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x_0 \cdot 4 + y_0 \cdot 9" style="vertical-align:-20%;" class="tex" alt="x_0 \cdot 4 + y_0 \cdot 9" />.  Then, you can express <img src="http://l.wordpress.com/latex.php?latex=4&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="4" style="vertical-align:-20%;" class="tex" alt="4" /> as a combination of <img src="http://l.wordpress.com/latex.php?latex=x_1%20%5Ccdot%209%20%2B%20y_1%20%5Ccdot%2031&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x_1 \cdot 9 + y_1 \cdot 31" style="vertical-align:-20%;" class="tex" alt="x_1 \cdot 9 + y_1 \cdot 31" /> and so on:</p>
<p><center><img src="http://l.wordpress.com/latex.php?latex=%5Cbegin%7Barray%7D%7Brcl%7D1%20%26%20%3D%20%26%20%28-2%29%5Ccdot4%20%2B%209%5Ccdot%7B1%7D%20%5C%5C%20%20%26%20%3D%20%26%20%28-2%29%5Ccdot%5Cleft%28%28-3%29%5Ccdot9%20%2B%2031%5Cright%29%20%2B%209%5Ccdot%7B1%7D%20%5C%5C%20%20%26%20%3D%20%26%20%28-2%29%5Ccdot31%20%2B%207%5Ccdot9%20%5C%5C%20%20%26%20%3D%20%26%20%28-2%29%5Ccdot31%20%2B%207%5Ccdot%2840%20-%2031%5Ccdot1%29%20%5C%5C%20%20%26%20%3D%20%26%20%28-9%29%5Ccdot31%20%2B%207%5Ccdot%2840%29%20%5Cequiv%201%20%5Cpmod%7B40%7D%5Cend%7Barray%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\begin{array}{rcl}1 &#038; = &#038; (-2)\cdot4 + 9\cdot{1} \\  &#038; = &#038; (-2)\cdot\left((-3)\cdot9 + 31\right) + 9\cdot{1} \\  &#038; = &#038; (-2)\cdot31 + 7\cdot9 \\  &#038; = &#038; (-2)\cdot31 + 7\cdot(40 - 31\cdot1) \\  &#038; = &#038; (-9)\cdot31 + 7\cdot(40) \equiv 1 \pmod{40}\end{array}" style="vertical-align:-20%;" class="tex" alt="\begin{array}{rcl}1 &#038; = &#038; (-2)\cdot4 + 9\cdot{1} \\  &#038; = &#038; (-2)\cdot\left((-3)\cdot9 + 31\right) + 9\cdot{1} \\  &#038; = &#038; (-2)\cdot31 + 7\cdot9 \\  &#038; = &#038; (-2)\cdot31 + 7\cdot(40 - 31\cdot1) \\  &#038; = &#038; (-9)\cdot31 + 7\cdot(40) \equiv 1 \pmod{40}\end{array}" /></center></p>
<p>Wheee&#8230; in my example chosen to be easy to backtrack, the answer comes out to be <img src="http://l.wordpress.com/latex.php?latex=-9&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="-9" style="vertical-align:-20%;" class="tex" alt="-9" /> which is equal to <img src="http://l.wordpress.com/latex.php?latex=31&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="31" style="vertical-align:-20%;" class="tex" alt="31" /> modulo <img src="http://l.wordpress.com/latex.php?latex=40&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="40" style="vertical-align:-20%;" class="tex" alt="40" />.  So, <img src="http://l.wordpress.com/latex.php?latex=31%5E2%20%5Cequiv%201%20%5Cpmod%7B40%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="31^2 \equiv 1 \pmod{40}" style="vertical-align:-20%;" class="tex" alt="31^2 \equiv 1 \pmod{40}" />.  I picked a lucky number that is its own inverse modulo <img src="http://l.wordpress.com/latex.php?latex=40&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="40" style="vertical-align:-20%;" class="tex" alt="40" />.  It&#8217;s not usually like that.  For example, <img src="http://l.wordpress.com/latex.php?latex=3%20%5Ccdot%2027%20%5Cequiv%207%20%5Ccdot%2023%20%5Cequiv%201%20%5Cpmod%7B40%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="3 \cdot 27 \equiv 7 \cdot 23 \equiv 1 \pmod{40}" style="vertical-align:-20%;" class="tex" alt="3 \cdot 27 \equiv 7 \cdot 23 \equiv 1 \pmod{40}" />.</p>
<p>So, anyhow, here&#8217;s a function that takes <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_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> and returns two values <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 <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_e561f1e00880abb7e58816455af00a48.png" title="y" style="vertical-align:-20%;" class="tex" alt="y" /> so that <img src="http://l.wordpress.com/latex.php?latex=x%5Ccdot%7B%7Da%20%2B%20y%5Ccdot%7B%7Dn%20%5Cequiv%20%5Cgcd%28a%2Cn%29%20%5Cpmod%20n&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x\cdot{}a + y\cdot{}n \equiv \gcd(a,n) \pmod n" style="vertical-align:-20%;" class="tex" alt="x\cdot{}a + y\cdot{}n \equiv \gcd(a,n) \pmod n" />.</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> gcd-as-linear-combination <span style="color: #66cc66;">&#40;</span>a n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>multiple-value-bind <span style="color: #66cc66;">&#40;</span>q r<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">truncate</span> n a<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">zerop</span> r<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>values <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>multiple-value-bind <span style="color: #66cc66;">&#40;</span>x y<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>gcd-as-linear-combination r a<span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span>values <span style="color: #66cc66;">&#40;</span>- y <span style="color: #66cc66;">&#40;</span>* q x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Assuming that <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_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> are relatively prime with <img src="http://l.wordpress.com/latex.php?latex=a%20%3C%20n&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="a < n" style="vertical-align:-20%;" class="tex" alt="a < n" />, the first value returned is <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_3cad95c81df4676e1b14e93cbb0b18ae.png" title="a" style="vertical-align:-20%;" class="tex" alt="a" />&#8216;s inverse modulo <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" />.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/two-snippets-I-rewrite-too-often/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Tidbits from the past month or two&#8230;</title>
		<link>http://nklein.com/2013/03/tidbits-from-the-past-month-or-two/</link>
		<comments>http://nklein.com/2013/03/tidbits-from-the-past-month-or-two/#comments</comments>
		<pubDate>Sat, 16 Mar 2013 02:58:52 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[boston lisp]]></category>
		<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1945</guid>
		<description><![CDATA[It&#8217;s been some time since I have posted here. Life has been busy. Until last week, I was working one and a half jobs after I transitioned out of getting paid to do C++ into getting paid to do Common Lisp. I&#8217;m now working for Clozure Associates. It&#8217;s awesome. And, on top of the awesome [...]]]></description>
				<content:encoded><![CDATA[<p>It&#8217;s been some time since I have posted here.  Life has been busy.  Until last week, I was working one and a half jobs after I transitioned out of getting paid to do C++ into getting paid to do Common Lisp.</p>
<p>I&#8217;m now working for Clozure Associates.  It&#8217;s awesome.</p>
<p>And, on top of the awesome of getting paid to do Lisp, there was an added layer of awesome this week.  I had dinner with some Clozure folk on Wednesday night in Boston.  Cyrus Harmon happened to also be in Boston that day.  News of Cyrus&#8217;s proximity triggered an impromptu Boston Lisp User&#8217;s dinner meeting.  So, I met Zach Beane, Cyrus Harmon, Ben Hyde, François-René Rideau, Andrew Shalit, and Gail Zacharias all on the same day.</p>
<p>Zach is taller than I&#8217;d have thought.  Cyrus isn&#8217;t Eastern European as I&#8217;d somehow believed him to be.  Faré sounds just like I&#8217;d imagined in such an eerie way that if he had looked at all familiar I&#8217;d have believed that I&#8217;d seen him live or on video before.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2013/03/tidbits-from-the-past-month-or-two/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Faster Primes with Heaps</title>
		<link>http://nklein.com/2013/01/faster-primes-with-heaps/</link>
		<comments>http://nklein.com/2013/01/faster-primes-with-heaps/#comments</comments>
		<pubDate>Tue, 29 Jan 2013 04:59:32 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[primes]]></category>
		<category><![CDATA[sieve]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1932</guid>
		<description><![CDATA[I read a nice blog post this morning about implementing the Sieve of Eratosthenes in Lisp. It got me thinking about something that I did a few weeks back. A few weeks back, I needed to generate all of the primes up to a given number. The number was big enough that I didn&#8217;t want [...]]]></description>
				<content:encoded><![CDATA[<p>I read a nice blog post this morning about <a href="http://blog.lowsnr.net/2013/01/27/prime-number-searches-functional-and-imperative/">implementing the Sieve of Eratosthenes in Lisp.</a>  It got me thinking about something that I did a few weeks back.</p>
<p>A few weeks back, I needed to generate all of the primes up to a given number.  The number was big enough that I didn&#8217;t want to keep an array of bytes (or even bits) big enough to do the sieving.  I just had an aversion to keeping around O(<img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" />) memory when <a href="http://en.wikipedia.org/wiki/Prime_number_theorem">O(<img src="http://l.wordpress.com/latex.php?latex=n%20%2F%20%5Cln%20n&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="n / \ln n" style="vertical-align:-20%;" class="tex" alt="n / \ln n" />) would suffice</a>.  I also had a hankering (probably because of <a href="http://letoverlambda.com">Let Over Lambda</a>) to implement it as a generator&#8212;a function that each time you called it gave you the next prime.</p>
<p>My first version amounted to trial-division against the entire list of primes generated so far.  For the number of primes that I wanted to generate, this took more than nine minutes.  Unacceptable.</p>
<p>I made some tweaks to it to ensure that I was only checking <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> against the previously generated primes that were less than or equal to <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&#40;</span>isqrt n<span style="color: #66cc66;">&#41;</span></span></code>.  I made some other tweaks to keep my previously generated primes sorted so the most-likely to be a divisor of <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> were first.  I made another tweak to accumulate the first <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_98d86ac06986d02b6e40f483093b61e0.png" title="k" style="vertical-align:-20%;" class="tex" alt="k" /> primes into a product so that I could check all of those against <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_18a633fc025e2f1a862f7c8de3b31d6c.png" title="n" style="vertical-align:-20%;" class="tex" alt="n" /> with a <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&#40;</span>gcd n product-of-first-k<span style="color: #66cc66;">&#41;</span></span></code> where <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_98d86ac06986d02b6e40f483093b61e0.png" title="k" style="vertical-align:-20%;" class="tex" alt="k" /> was constrained so that the product stayed below <code class="codecolorer lisp default"><span class="lisp">most-positive-fixnum</span></code>.</p>
<p>Those tweaks got me down to around two minutes.  That was still more than twice as long as I wanted it to take.</p>
<p>Now, with the help of <a href="http://common-lisp.net/project/cl-heap/">CL-HEAP</a> and the Rosetta-Code-approved optimization mentioned in <a href="http://blog.lowsnr.net/2013/01/27/prime-number-searches-functional-and-imperative/">that blog post</a>, I have it down to thirty seconds with no trial division, no square roots, and only one multiplication per prime.</p>
<p>I keep a heap of <code class="codecolorer lisp default"><span class="lisp"><span style="color: #b1b100;">cons</span></span></code> cells.  The <code class="codecolorer lisp default"><span class="lisp"><span style="color: #b1b100;">car</span></span></code> is the next number that will be crossed out by this prime and the <code class="codecolorer lisp default"><span class="lisp"><span style="color: #b1b100;">cdr</span></span></code> is how much to increment the <code class="codecolorer lisp default"><span class="lisp"><span style="color: #b1b100;">car</span></span></code> once I&#8217;ve crossed out the <code class="codecolorer lisp default"><span class="lisp"><span style="color: #b1b100;">car</span></span></code>.</p>
<p>Ignoring the special-case at the beginning for two, my current algorithm looks 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> prime-generator <span style="color: #66cc66;">&#40;</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>nixed <span style="color: #66cc66;">&#40;</span>make-instance 'cl-heap<span style="color: #66cc66;">:</span><span style="color: #555;">fibonacci-heap</span>
                              <span style="color: #66cc66;">:</span><span style="color: #555;">key</span> #'first
                              <span style="color: #66cc66;">:</span><span style="color: #555;">sort-fun</span> #'<span style="color: #66cc66;">&lt;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>guess <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><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>loop <span style="color: #66cc66;">:</span><span style="color: #b1b100;">do</span> <span style="color: #66cc66;">&#40;</span>incf guess <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">until</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">%</span>prime-p guess nixed<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>cl-heap<span style="color: #66cc66;">:</span><span style="color: #555;">add-to-heap</span> nixed <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> <span style="color: #66cc66;">&#40;</span>* guess guess<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>* guess <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      guess<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>The <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">%</span>prime-p</span></code> function pulls <code class="codecolorer lisp default"><span class="lisp"><span style="color: #b1b100;">cons</span></span></code> cells off of the heap until it finds whose <code class="codecolorer lisp default"><span class="lisp"><span style="color: #b1b100;">car</span></span></code> isn&#8217;t equal to the current guess.  For each of them where the <code class="codecolorer lisp default"><span class="lisp"><span style="color: #b1b100;">car</span></span></code> was equal, it increments the <code class="codecolorer lisp default"><span class="lisp"><span style="color: #b1b100;">car</span></span></code> by its <code class="codecolorer lisp default"><span class="lisp"><span style="color: #b1b100;">cdr</span></span></code>.  Then, it puts all of the <code class="codecolorer lisp default"><span class="lisp"><span style="color: #b1b100;">cons</span></span></code> cells it removed back on the heap.  The guess was prime if it wasn&#8217;t equal to any of the numbers on the heap.</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> <span style="color: #66cc66;">%</span>prime-p <span style="color: #66cc66;">&#40;</span>guess nixed<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>prime t<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>n <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>loop <span style="color: #66cc66;">:</span><span style="color: #b1b100;">do</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> n <span style="color: #66cc66;">&#40;</span>cl-heap<span style="color: #66cc66;">:</span><span style="color: #555;">pop-heap</span> nixed<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">:</span><span style="color: #555;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">and</span> n <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> n<span style="color: #66cc66;">&#41;</span> guess<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">:</span><span style="color: #b1b100;">do</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">progn</span>
             <span style="color: #66cc66;">&#40;</span>incf <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> n<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>cl-heap<span style="color: #66cc66;">:</span><span style="color: #555;">add-to-heap</span> nixed n<span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> prime <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">:</span><span style="color: #555;">finally</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> n <span style="color: #66cc66;">&#40;</span>cl-heap<span style="color: #66cc66;">:</span><span style="color: #555;">add-to-heap</span> nixed n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    prime<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>I could probably save some time and <code class="codecolorer lisp default"><span class="lisp"><span style="color: #b1b100;">cons</span></span></code>-ing by peeking at the top of the heap rather than always gathering or countless other ways, but this more than exceeded my expectations for today.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2013/01/faster-primes-with-heaps/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Whittling Away with Repetition</title>
		<link>http://nklein.com/2013/01/whittling-away-with-repetition/</link>
		<comments>http://nklein.com/2013/01/whittling-away-with-repetition/#comments</comments>
		<pubDate>Tue, 15 Jan 2013 05:50:27 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[code kata]]></category>
		<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1919</guid>
		<description><![CDATA[I&#8217;ve now done the Bowling Game Kata from the previous video seven more times imperatively as I did in the video and two more times with a functional style. Each time I did it imperatively, I decreased the overall size of the code. The functional code is smaller still and got smaller on my second [...]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve now done the Bowling Game Kata from the <a href="http://nklein.com/2013/01/bowling-game-kata-in-common-lisp">previous video</a> seven more times imperatively as I did in the video and two more times with a functional style.</p>
<p>Each time I did it imperatively, I decreased the overall size of the code.  The functional code is smaller still and got smaller on my second try.  The previous video was <code class="codecolorer bash default"><span class="bash">bowling-<span style="color: #000000;">20130102</span>.lisp</span></code> and the functional versions end in <code class="codecolorer bash default"><span class="bash">-f.lisp</span></code>.</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">%</span> lisp_count <span style="color: #000000; font-weight: bold;">*</span>.lisp
<span style="color: #000000;">70</span> bowling-<span style="color: #000000;">20121230</span>.lisp
<span style="color: #000000;">70</span> bowling-<span style="color: #000000;">20130102</span>.lisp
<span style="color: #000000;">64</span> bowling-<span style="color: #000000;">20130106</span>.lisp
<span style="color: #000000;">62</span> bowling-<span style="color: #000000;">20130107</span>.lisp
<span style="color: #000000;">64</span> bowling-<span style="color: #000000;">20130109</span>.lisp
<span style="color: #000000;">66</span> bowling-<span style="color: #000000;">20130110</span>.lisp
<span style="color: #000000;">62</span> bowling-<span style="color: #000000;">20130113</span>.lisp
<span style="color: #000000;">54</span> bowling-20130113b-f.lisp
<span style="color: #000000;">57</span> bowling-<span style="color: #000000;">20130114</span>.lisp
<span style="color: #000000;">57</span> bowling-20130114b.lisp
<span style="color: #000000;">51</span> bowling-20130114c-f.lisp
Total:
<span style="color: #000000;">677</span></pre></div>
<p>These SLOC numbers were generated using <a href="http://www.dwheeler.com/sloccount/">David A. Wheeler&#8217;s &#8216;SLOCCount&#8217;</a>.  Straight up byte, word, and line counts follow the same trend.</p>
<p>Soon, I will record another run through the imperative style and a run through the functional style.</p>
<p>Here are the most recent versions of each style:</p>
<ul>
<li><a href="http://nklein.com/wp-content/uploads/2013/01/bowling-20130114b.lisp">bowling-20130114b</a></li>
<li><a href="http://nklein.com/wp-content/uploads/2013/01/bowling-20130114c-f.lisp">bowling-20130114c-f</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2013/01/whittling-away-with-repetition/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Bowling Game Kata in Common Lisp</title>
		<link>http://nklein.com/2013/01/bowling-game-kata-in-common-lisp</link>
		<comments>http://nklein.com/2013/01/bowling-game-kata-in-common-lisp#comments</comments>
		<pubDate>Fri, 04 Jan 2013 02:43:41 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[code kata]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[test-driven development]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1914</guid>
		<description><![CDATA[The Bowling Game Kata in Common Lisp from Patrick Stein on Vimeo. Code Kata are repetitive coding tasks designed to help one internalize certain patterns, methodologies, or tools. In this video, I go through Uncle Bob Martin&#8217;s The Bowling Game Kata. The Kata exercises test-driven development. Uncle Bob&#8217;s presentation of the Kata is in Java. [...]]]></description>
				<content:encoded><![CDATA[<p><iframe src="http://player.vimeo.com/video/56730687?byline=0&amp;portrait=0" height="281" width="500" allowfullscreen="" frameborder="0"></iframe></p>
<p><a href="http://vimeo.com/56730687">The Bowling Game Kata in Common Lisp</a> from <a href="http://vimeo.com/nklein">Patrick Stein</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p>Code Kata are repetitive coding tasks designed to help one internalize certain patterns, methodologies, or tools. In this video, I go through <a href="http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata">Uncle Bob Martin&#8217;s The Bowling Game Kata</a>. The Kata exercises test-driven development. Uncle Bob&#8217;s presentation of the Kata is in Java. This is in Common Lisp.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2013/01/bowling-game-kata-in-common-lisp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Nick Levine&#8217;s cl-log rocks&#8230;</title>
		<link>http://nklein.com/2012/12/nick-levines-cl-log-rocks/</link>
		<comments>http://nklein.com/2012/12/nick-levines-cl-log-rocks/#comments</comments>
		<pubDate>Mon, 10 Dec 2012 19:43:39 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[cl-log]]></category>
		<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1910</guid>
		<description><![CDATA[Whenever I need logging in my Common Lisp applications, I use Nick Levine&#8217;s cl-log library. For many applications, spending the time to run CL:FORMAT during runtime is unreasonable. It is often much better to log something in a raw form and do any formatting/presentation offline rather than online. The cl-log library is the only one [...]]]></description>
				<content:encoded><![CDATA[<p>Whenever I need logging in my Common Lisp applications, I use Nick Levine&#8217;s <a href="http://www.nicklevine.org/cl-log/">cl-log</a> library.  For many applications, spending the time to run <code class="codecolorer lisp default"><span class="lisp">CL<span style="color: #66cc66;">:</span><span style="color: #555;">FORMAT</span></span></code> during runtime is unreasonable.  It is often much better to log something in a raw form and do any formatting/presentation offline rather than online.  The cl-log library is the only one that I know of which supports binary logging readily.</p>
<p>Additionally, Nick is incredibly responsive.  I found an oversight in a corner-case of cl-log last Friday at 1am.  I emailed him about it at 1:19am.  In under 5 hours, I had email back pointing me to a new release with that case corrected.</p>
<p>I hope to do a more complete comparison of the existing log libraries at some point.  At this point, know that I&#8217;ve tried all of the ones on the Cliki which had obvious documentation or examples, and I&#8217;ll be using cl-log.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2012/12/nick-levines-cl-log-rocks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Coder&#8217;s Asceticism</title>
		<link>http://nklein.com/2012/10/coders-asceticism/</link>
		<comments>http://nklein.com/2012/10/coders-asceticism/#comments</comments>
		<pubDate>Sun, 14 Oct 2012 06:08:36 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[minimalism]]></category>
		<category><![CDATA[packages]]></category>
		<category><![CDATA[style]]></category>
		<category><![CDATA[uncle bob]]></category>
		<category><![CDATA[unet]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1903</guid>
		<description><![CDATA[A month or two ago, I watched a bunch of videos of Uncle Bob speaking at various conferences. He&#8217;s thought a lot about what makes code testable and maintainable, and he&#8217;s an excellent speaker. I&#8217;ve even paid to watch some of his hour-ish videos at his Clean Coders site. In Clean Code, Episode 3 &#8211; [...]]]></description>
				<content:encoded><![CDATA[<p>A month or two ago, I watched a bunch of videos of <a href="http://en.wikipedia.org/wiki/Robert_Cecil_Martin">Uncle Bob</a> speaking at various conferences.  He&#8217;s thought a lot about what makes code testable and maintainable, and he&#8217;s an excellent speaker.  I&#8217;ve even paid to watch some of his hour-ish videos at his <a href="http://cleancoders.com">Clean Coders</a> site.</p>
<p>In <a href="http://http://www.cleancoders.com/codecast/clean-code-episode-3/show">Clean Code, Episode 3 &#8211; Functions</a>, he makes the case that we should be striving to keep each function under five lines long.  So, over the past month or so, I&#8217;ve been trying this out in all of my Scala and Lisp endeavors.</p>
<p>Once you get each function down to four lines or fewer, you start running into symbol-proliferation.  You pretty much throw out <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&#40;</span>LABELS <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>FLET <span style="color: #66cc66;">...</span><span style="color: #66cc66;">&#41;</span></span></code> unless you need the closure (and then try to say that when you can&#8217;t that it&#8217;s good enough to just keep the subfunctions under five lines and the body under five, too).  You create bunches of functions that should never be called anywhere except the one place it&#8217;s called now.</p>
<h3>Enter Package Proliferation!</h3>
<p>Then, I remembered some Lisp coding style guidelines that I read early this year sometime.  Those guidelines advocated having one package for each Lisp file and explicitly making peer files have to import or qualify symbols used from other peer files.  This will help me with the symbol-proliferation!  Let&#8217;s do it!</p>
<h3>Enter File Proliferation!</h3>
<p>After two weeks of that, I woke up yesterday morning realizing that I wasn&#8217;t getting the full <q>benefit</q> from this if I was exporting more than one symbol from each package.  If keeping each function tiny is Good and limiting a package to one file is Good, then it is certainly Bad to have one file&#8217;s package exporting eight different functions.</p>
<p>So, today, I reworked all of the <a href="http://nklein.com/software/unet/">UNet</a> that I wrote in the last couple of weeks to have one package per file and as few exported symbols per package as possible.</p>
<p>In the interest of testability and modularity, I had broken out a subpackage of UNet called UNet-Sockets that is to be the interface the UNet library uses to interact with sockets.  I had realized that I had a file called <i>methods.lisp</i> with four different publicly available <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&#40;</span>DEFMETHOD <span style="color: #66cc66;">...</span><span style="color: #66cc66;">&#41;</span></span></code> forms in it.  Now, each is in its own file and package.  I did the same for various other declarations.</p>
<p>Now, there is a top-level package which uses Tim Bradshaw&#8217;s <a href="http://www.cliki.net/conduit-packages">Conduit Packages</a> to collect all of the symbols meant to be used externally from these packages.  If there is an exported function in a package, that is the only exported symbol in that package.  If there is an exported generic function declaration in a package, that is the only exported symbol from that package.  If there is an exported method implementation in a package, there aren&#8217;t any exported symbols from that package.  If there is an exported condition in a package, that condition and its accessors are the only exported symbols from that package.  If there is an exported class in a package, that class and its accessors are the only exported symbols from that package.</p>
<h3>Example</h3>
<p>The top-level package of the <i>UNET-SOCKETS</i> system, that defines the interface that UNet will use to access its sockets, consists of just a <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&#40;</span>DEFPACKAGE <span style="color: #66cc66;">...</span><span style="color: #66cc66;">&#41;</span></span></code> form.  You can see the full package on <a href="https://github.com/nklein/unet/tree/trying-package-per-file">github</a> in the <i>unet-sockets.asd</i> and the <i>sockets/</i> directory.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><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>org<span style="color: #66cc66;">.</span>tfeb<span style="color: #66cc66;">.</span>conduit-packages<span style="color: #66cc66;">:</span><span style="color: #555;">defpackage</span> <span style="color: #66cc66;">:</span><span style="color: #555;">unet-sockets</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">use</span> <span style="color: #66cc66;">:</span><span style="color: #555;">cl</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">extends</span>/including <span style="color: #66cc66;">:</span><span style="color: #555;">unet-sockets-interface</span>
                      #<span style="color: #66cc66;">:</span><span style="color: #555;">sockets-interface</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">extends</span>/including <span style="color: #66cc66;">:</span><span style="color: #555;">unet-sockets-base-socket</span>
                      #<span style="color: #66cc66;">:</span><span style="color: #555;">base-socket</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">extends</span>/including <span style="color: #66cc66;">:</span><span style="color: #555;">unet-sockets-hostname-not-found-</span><span style="color: #b1b100;">error</span>
                      #<span style="color: #66cc66;">:</span><span style="color: #555;">hostname-not-found-</span><span style="color: #b1b100;">error</span>
                      #<span style="color: #66cc66;">:</span><span style="color: #555;">hostname-not-found-error-hostname</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">extends</span>/including <span style="color: #66cc66;">:</span><span style="color: #555;">unet-sockets-get-address</span>
                      #<span style="color: #66cc66;">:</span><span style="color: #555;">get-address</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">extends</span>/including <span style="color: #66cc66;">:</span><span style="color: #555;">unet-sockets-address</span>+port-not-available-<span style="color: #b1b100;">error</span>
                      #<span style="color: #66cc66;">:</span><span style="color: #555;">address</span>+port-not-available-<span style="color: #b1b100;">error</span>
                      #<span style="color: #66cc66;">:</span><span style="color: #555;">address</span>+port-not-available-error-address
                      #<span style="color: #66cc66;">:</span><span style="color: #555;">address</span>+port-not-available-error-port<span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">extends</span>/including <span style="color: #66cc66;">:</span><span style="color: #555;">unet-sockets-create-datagram-socket</span>
                      #<span style="color: #66cc66;">:</span><span style="color: #555;">create-datagram-socket</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">extends</span>/including <span style="color: #66cc66;">:</span><span style="color: #555;">unet-sockets-send-datagram</span>
                      #<span style="color: #66cc66;">:</span><span style="color: #555;">send-datagram</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">extends</span>/including <span style="color: #66cc66;">:</span><span style="color: #555;">unet-sockets-poll-datagram</span>
                      #<span style="color: #66cc66;">:</span><span style="color: #555;">poll-datagram</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<h3>Conclusion?</h3>
<p>The jury&#8217;s still out on this for me.  On the one hand, the self-flagellation is an interesting exercise.  On the other hand, if I&#8217;m going to have to export everything useful and import it in multiple places then I&#8217;m taking away some of the <a href="http://nklein.com/2009/02/sapir-whorf-wit-programming-languages/">fun</a>.  I feel like I&#8217;m maintaining C++ header files to some extent.</p>
<p>I think I&#8217;ll keep it up for the UNet project, at least.  It makes good documentation to have each exported symbol in a package and file named for that exported symbol.  You can open up any file and read the three-to-four line exported function in it.  You can see, right there with it, any supporting function it uses that no other files need to use.  You can see from the <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&#40;</span>DEFPACKAGE <span style="color: #66cc66;">...</span><span style="color: #66cc66;">&#41;</span></span></code> form at the top where any other supporting functions came from.  And nothing else is cluttering up the landscape.</p>
<p>We&#8217;ll see if I develop religion on this topic.  At the moment, I think it&#8217;s eventually going to fall into the <i>moderation in all things</i> bucket.  Time will tell.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2012/10/coders-asceticism/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Polylinguistic Code Improvement</title>
		<link>http://nklein.com/2012/09/polylinguistic-code-improvement/</link>
		<comments>http://nklein.com/2012/09/polylinguistic-code-improvement/#comments</comments>
		<pubDate>Mon, 24 Sep 2012 15:18:36 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[code improvement]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1896</guid>
		<description><![CDATA[I have started the Coursera course: Functional Programming Principles in Scala. The first assignment was a few simple recursion problems: calculate a given entry in Pascal&#8217;s triangle, verify that the parens are balanced in a list of characters, and count the number of ways one can give change for a given amount with given coin [...]]]></description>
				<content:encoded><![CDATA[<p>I have started the <a href="http://coursera.org">Coursera</a> course: <a href="https://www.coursera.org/course/progfun">Functional Programming Principles in Scala</a>.  The first assignment was a few simple recursion problems: calculate a given entry in Pascal&#8217;s triangle, verify that the parens are balanced in a list of characters, and count the number of ways one can give change for a given amount with given coin denominations.  I did those assignments six days ago.</p>
<p>Last night, for grins, I thought I would implement them in Common Lisp.  I started out doing them without referencing the Scala code that I&#8217;d written.  Then, I got lazy and pulled up the Scala code for the last problem and a half.</p>
<p>For all three problems, I came up with better solutions that translated back into the Scala.  Some of it was just the benefit of looking at the previous solution again.  Some of it was thinking about it with a new language in mind.  Some of it was being more comfortable with Lisp.  It was such a dramatic improvement in the code that even though I had scored 10/10 six days ago, I still reworked the Scala code and resubmitted it last night.</p>
<p>As it relates to Common Lisp development specifically&#8230;. One of the first Lisp books that I read was Paul Graham&#8217;s <a href="http://www.paulgraham.com/onlisp.html">On Lisp</a>.  In that book, he does a great deal of turning recursive functions into tail-recursive functions.  I had followed his idiom of calling the internal function <code class="codecolorer lisp default"><span class="lisp">rec</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> factorial <span style="color: #66cc66;">&#40;</span>n<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>rec <span style="color: #66cc66;">&#40;</span>n accum<span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">zerop</span> n<span style="color: #66cc66;">&#41;</span>
                 accum
               <span style="color: #66cc66;">&#40;</span>rec <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>- n<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>* n accum<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>rec n <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>For the Scala, I started out naming the internal function with a <code class="codecolorer scala default"><span class="scala">Rec</span></code> suffix on the function name:</p>
<div class="codecolorer-container scala blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="scala codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff; font-weight: bold;">def</span> factorial <span style="color: #F78811;">&#40;</span>n<span style="color: #000080;">:</span> Int<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Int <span style="color: #000080;">=</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> factorialRec <span style="color: #F78811;">&#40;</span>n<span style="color: #000080;">:</span> Int, accum<span style="color: #000080;">:</span> Int<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Int <span style="color: #000080;">=</span>
    <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>n <span style="color: #000080;">==</span> <span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span> accum
    <span style="color: #0000ff; font-weight: bold;">else</span> factorialRec<span style="color: #F78811;">&#40;</span>n-<span style="color: #F78811;">1</span>, n<span style="color: #000080;">*</span>accum<span style="color: #F78811;">&#41;</span>
  factorialRec<span style="color: #F78811;">&#40;</span>n,<span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span></pre></div>
<p>Now, I&#8217;m partial to just re-using the function name:</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> factorial <span style="color: #66cc66;">&#40;</span>n<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>factorial <span style="color: #66cc66;">&#40;</span>n accum<span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">zerop</span> n<span style="color: #66cc66;">&#41;</span>
                 accum
               <span style="color: #66cc66;">&#40;</span>factorial <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>- n<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>* n accum<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>factorial n <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2012/09/polylinguistic-code-improvement/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
