<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nklein software &#187; c++</title>
	<atom:link href="http://nklein.com/tags/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://nklein.com</link>
	<description>software development and consulting</description>
	<lastBuildDate>Thu, 22 Dec 2011 04:42:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Keeping Server and Client Separate</title>
		<link>http://nklein.com/keeping-server-and-client-separate</link>
		<comments>http://nklein.com/keeping-server-and-client-separate#comments</comments>
		<pubDate>Thu, 08 Sep 2011 19:51:33 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[client-server]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[literate programming]]></category>
		<category><![CDATA[userial]]></category>

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

		<guid isPermaLink="false">http://nklein.com/?p=1237</guid>
		<description><![CDATA[A few years back (for a very generous few), we needed to parse a wide variety of XML strings. It was quite tedious to go from the XML to the native-language representations of the data (even from a DOM version). Furthermore, we needed to parse this XML both in Java and in C++. I wrote [...]]]></description>
			<content:encoded><![CDATA[<p>A few years back (for a very generous <q>few</q>), we needed to parse a wide variety of XML strings.  It was quite tedious to go from the XML to the native-language representations of the data (even from a DOM version).  Furthermore, we needed to parse this XML both in Java and in C++.</p>
<p>I wrote (in Java) an XML parser generator that took an XML description of how you&#8217;d like the native-language data structures to look and where in the XML it could find the values for those data structures.  The Java code-base for this was ugly, ugly, ugly.  I tried several times to clean it up into something publishable.  I tried to clean it up several times so that it could actually generate the parser it used to read the XML description file.  Alas, the meta-ness, combined with the clunky Java code, kept me from completing the circle.</p>
<p>Fast forward to last week.  Suddenly, I have a reason to parse a wide variety of XML strings in Objective C.  I certainly didn&#8217;t want to pull out the Java parser generator and try to beat it into generating Objective C, too.  That&#8217;s fortunate, too, because I cannot find any of the copies (in various states of repair) that once lurked in <q>~/src</q>.</p>
<p>What&#8217;s a man to do?  Write it in Lisp, of course.</p>
<h3>Example</h3>
<p>Here&#8217;s an example to show how it works.  Let&#8217;s take some simple XML that lists food items on a menu:</p>
<div class="codecolorer-container xml blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;menu<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;food</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Belgian Waffles&quot;</span> <span style="color: #000066;">price</span>=<span style="color: #ff0000;">&quot;$5.95&quot;</span> <span style="color: #000066;">calories</span>=<span style="color: #ff0000;">&quot;650&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>two of our famous Belgian Waffles with plenty of real maple syrup<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/food<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #808080; font-style: italic;">&lt;!-- ... more food entries, omitted here for brevity ... --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/menu<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div>
<p>We craft an XML description of how to go from the XML into a native representation:</p>
<div class="codecolorer-container xml blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;parser_generator</span> <span style="color: #000066;">root</span>=<span style="color: #ff0000;">&quot;menu&quot;</span> <span style="color: #000066;">from</span>=<span style="color: #ff0000;">&quot;/menu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;struct</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;food item&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;field</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;string&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;name&quot;</span> <span style="color: #000066;">from</span>=<span style="color: #ff0000;">&quot;@name&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;field</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;string&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;price&quot;</span> <span style="color: #000066;">from</span>=<span style="color: #ff0000;">&quot;@price&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;field</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;string&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;description&quot;</span> <span style="color: #000066;">from</span>=<span style="color: #ff0000;">&quot;/description/.&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;field</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;integer&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;calories&quot;</span> <span style="color: #000066;">from</span>=<span style="color: #ff0000;">&quot;@calories&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/struct<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;struct</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;menu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;field</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;menu items&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;array_element</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;food item&quot;</span> <span style="color: #000066;">from</span>=<span style="color: #ff0000;">&quot;/food&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/field<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/struct<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/parser_generator<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div>
<p>Now, you run the parser generator on the above input file:</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> <span style="color: #c20cb9; font-weight: bold;">sh</span> parser-generator.sh <span style="color: #660033;">--language</span>=lisp \
                           <span style="color: #660033;">--types-package</span> menu \
                           <span style="color: #660033;">--reader-package</span> menu-reader \
                           <span style="color: #660033;">--file</span> menu.xml</pre></div>
<p>This generates two files for you: <q>types.lisp</q> and <q>reader.lisp</q>.  This is what <q>types.lisp</q> looks like:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">(defpackage :menu
  (:use :common-lisp)
  (:export #:food-item
             #:name
             #:price
             #:description
             #:calories
           #:menu
             #:menu-items))
&nbsp;
(in-package :menu)
&nbsp;
(defclass food-item ()
  ((name :initarg :name :type string)
   (price :initarg :price :type string)
   (description :initarg :description :type string)
   (calories :initarg :calories :type integer)))
&nbsp;
(defclass menu ()
  ((menu-items :initarg :menu-items :type list :initform nil)))</pre></div>
<p>I will not bore you with all of <q>reader.lisp</q> as it&#8217;s 134 lines of code you never had to write.  The only part you need to worry about is the <q>parse</q> function which takes a stream for or pathname to the XML and returns an instance of the <q>menu</q> class.  Here is a small snippet though:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">;;; =================================================================
;;; food-item struct
;;; =================================================================
(defmethod data progn ((handler sax-handler) (item food-item) path value)
  (with-slots (name price description calories) item
    (case path
      (:|@name| (setf name value))
      (:|@price| (setf price value))
      (:|/description/.| (setf description value))
      (:|@calories| (setf calories (parse-integer value))))))</pre></div>
<h3>Where it&#8217;s at</h3>
<p>I currently have the parser generator generating its own parser (five times fast).  I still have a little bit more that I&#8217;d like to add to include assertions for things like the minimum number of elements in an array or the minimum value of an integer.  I also have a few kinks to work out so that you can return some type other than an instance of a class for cases like this where the <q>menu</q> class just wraps one item.</p>
<p>My next step though is to get it generating Objective C parsers.</p>
<p>Somewhere in there, I&#8217;ll post this to a public git repository.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2010/03/xml-parser-generator/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Casting to Integers Considered Harmful</title>
		<link>http://nklein.com/2009/08/casting-to-integers-considered-harmful/</link>
		<comments>http://nklein.com/2009/08/casting-to-integers-considered-harmful/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 18:46:14 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[rounding]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=707</guid>
		<description><![CDATA[Background Many years back, I wrote some ambient music generation code. The basic structure of the code is this: Take one queen and twenty or so drones in a thirty-two dimensional space. Give them each random positions and velocities. Limit the velocity and acceleration of the queen more than you limit the same for the [...]]]></description>
			<content:encoded><![CDATA[<h3>Background</h3>
<p>Many years back, I wrote <a href="http://old.nklein.com/products/rmusic/">some ambient <q>music</q> generation code</a>.  The basic structure of the code is this:  Take one <q>queen</q> and twenty or so <q>drones</q> in a thirty-two dimensional space.  Give them each random positions and velocities.  Limit the velocity and acceleration of the queen more than you limit the same for the drones.  Now, select some point at random for the queen to target.  Have the queen accelerate toward that target.  Have the drones accelerate toward the queen.  Use the average distance from the drones to the queens in the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5477186a84cc2c889974ca6fd01ca96f.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" />-th dimension as the volume of the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5477186a84cc2c889974ca6fd01ca96f.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" />-th note where the notes are logarithmically spaced across one octave.  Clip negative volumes to zero.  Every so often, or when the queen gets close to the target, give the queen a new target.</p>
<p>It makes for some interesting ambient noise that sounds a bit like movie space noises where the lumbering enemy battleship is looming in orbit as its center portion spins to create artificial gravity within.</p>
<p>I started working on an iPhone application based on this code.  The original code was in C++.  The conversion to Objective C was fairly straightforward and fairly painless (as I used the opportunity to try to <a href="http://nklein.com/2009/02/sapir-whorf-wit-programming-languages/">correct my own faults</a> by breaking things out into separate functions more often).</p>
<h3>Visualization troubles</h3>
<p><a href="http://nklein.com/wp-content/uploads/2009/08/uniform.png"><img src="http://nklein.com/wp-content/uploads/2009/08/uniform-300x225.png" alt="uniform" title="uniform" width="300" height="225" class="alignright size-medium wp-image-709" /></a><br />
The original code though chose random positions and velocities from uniform distributions.  The iPhone app is going to involve visualization as well as <q>auralization</q>.  The picture at the right here is a plot of five thousand points with each coordinate selected from a uniform distribution with range [-20,+20].  Because each axis value is chosen independently, it looks very unnatural.</p>
<p style="clear: both;">
<a href="http://nklein.com/wp-content/uploads/2009/08/gauss.png"><img src="http://nklein.com/wp-content/uploads/2009/08/gauss-300x225.png" alt="gauss" title="gauss" width="300" height="225" class="alignright size-medium wp-image-710" /></a><br />
What to do?  The obvious answer is to use <a href="http://en.wikipedia.org/wiki/Normal_distribution">Gaussian random variables</a> instead of uniform ones.  The picture at the right here is five thousand points with each coordinate selected from a Gaussian distribution with a standard-deviation of 10.  As you can see, this is much more natural looking.</p>
<h3 style="clear: both;">How did I generate the Gaussians?</h3>
<p>I have usually used the Box-Muller method of generating two Gaussian-distributed random variables given two uniformly-distributed random variables:</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> random-gaussian <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>u1 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>u2 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>mag <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">sqrt</span> <span style="color: #66cc66;">&#40;</span>* -<span style="color: #cc66cc;">2.0</span> <span style="color: #66cc66;">&#40;</span>log u1<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>ang <span style="color: #66cc66;">&#40;</span>* <span style="color: #cc66cc;">2.0</span> pi u2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>values <span style="color: #66cc66;">&#40;</span>* mag <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cos</span> ang<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#40;</span>* mag <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">sin</span> ang<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>But, I found an article online that <a href="http://www.taygeta.com/random/gaussian.html">shows a more numerically stable version</a>:</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> random-gaussian <span style="color: #66cc66;">&#40;</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>pick-in-circle <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
           <span style="color: #66cc66;">&#40;</span>loop as u1 <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>
                as u2 <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>
                as mag-squared <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>* u1 u1<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>* u2 u2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;</span> mag-squared <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span>values u1 u2 mag-squared<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>multiple-value-bind <span style="color: #66cc66;">&#40;</span>u1 u2 mag-squared<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>pick-in-circle<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>ww <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">sqrt</span> <span style="color: #66cc66;">&#40;</span>/ <span style="color: #66cc66;">&#40;</span>* -<span style="color: #cc66cc;">2.0</span> <span style="color: #66cc66;">&#40;</span>log mag-squared<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> mag-squared<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>values <span style="color: #66cc66;">&#40;</span>* u1 ww<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>* u2 ww<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>For a quick sanity check, I thought, <q>let&#8217;s just make sure it looks like a Gaussian.</q>  Here, I showed the code in Lisp, but the original code was in Objective-C.  I figured, <q>If I just change the function declaration, I can plop this into a short C program, run a few thousand trials into some histogram buckets, and see what I get.</q></p>
<h3>The trouble with zero</h3>
<p>So, here comes the problem with zero.  I had the following main loop:</p>
<div class="codecolorer-container c blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#define BUCKET_COUNT 33</span>
<span style="color: #339933;">#define STDDEV       8.0</span>
<span style="color: #339933;">#define ITERATIONS   100000</span>
&nbsp;
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> ii<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> ii <span style="color: #339933;">&lt;</span> ITERATIONS<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>ii <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> bb <span style="color: #339933;">=</span> val_to_bucket<span style="color: #009900;">&#40;</span> STDDEV <span style="color: #339933;">*</span> gaussian<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">&lt;=</span> bb <span style="color: #339933;">&amp;&amp;</span> bb <span style="color: #339933;">&lt;</span> BUCKET_COUNT <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #339933;">++</span>buckets<span style="color: #009900;">&#91;</span> bb <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></div>
<p>I now present you with three different implementations of the <em>val_to_bucket()</em> function.</p>
<div class="codecolorer-container c blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">int</span> val_to_bucket<span style="color: #009900;">&#40;</span> <span style="color: #993333;">double</span> _val <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span>_val <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span> BUCKET_COUNT <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> val_to_bucket<span style="color: #009900;">&#40;</span> <span style="color: #993333;">double</span> _val <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span> _val <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span> BUCKET_COUNT <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> val_to_bucket<span style="color: #009900;">&#40;</span> <span style="color: #993333;">double</span> _val <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span> _val <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span> BUCKET_COUNT <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div>
<p>As you can probably guess, after years or reading trick questions, only the last one actually works as far as my main loop is concerned.  Why?  Every number between -1 and +1 becomes zero when you cast the double to an integer.  That&#8217;s twice as big a range as any other integer gets.  So, for the first implementation, the middle bucket has about twice as many things in it as it should.  For the second implementation, the first bucket has more things in it than it should.  For the final implementation, the non-existent bucket before the first one is the overloaded bucket.  In the end, I used this implementation instead so that I wouldn&#8217;t even bias non-existent buckets:</p>
<div class="codecolorer-container c blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">int</span> val_to_bucket<span style="color: #009900;">&#40;</span> <span style="color: #993333;">double</span> _val <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span>lround<span style="color: #009900;">&#40;</span>_val<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span> BUCKET_COUNT <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/08/casting-to-integers-considered-harmful/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Why Not Return A Function?</title>
		<link>http://nklein.com/2009/07/why-not-return-a-function/</link>
		<comments>http://nklein.com/2009/07/why-not-return-a-function/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 19:19:08 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[scheme]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=633</guid>
		<description><![CDATA[This morning, I was catching up on the RSS feeds I follow. I noticed an interesting snippet of code in the Abstract Heresies journal for the Discrete-time Fourier Transform. Here is his snippet of code: &#40;define &#40;dtft samples&#41; &#40;lambda &#40;omega&#41; &#40;sum 0 &#40;vector-length samples&#41; &#40;lambda &#40;n&#41; &#40;* &#40;vector-ref samples n&#41; &#40;make-polar 1.0 &#40;* omega n&#41;&#41;&#41;&#41;&#41;&#41;&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>This morning, I was catching up on the RSS feeds I follow.  I noticed an interesting snippet of code in the <a href="http://funcall.blogspot.com/">Abstract Heresies</a> journal for the <a href="http://funcall.blogspot.com/2009/06/dtft.html">Discrete-time Fourier Transform</a>.  Here is his snippet of code:</p>
<div class="codecolorer-container scheme blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="scheme 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;">define</span> <span style="color: #66cc66;">&#40;</span>dtft samples<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>omega<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>sum <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">vector-length</span> samples<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">vector-ref</span> samples n<span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">make-polar</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span> omega n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>My first thought was <q>That&#8217;s way too short.</q>  Then, I started reading through it.  My next thought was, maybe I don&#8217;t understand scheme at all.  Then, my next thought was, <q>I do understand this code, it just didn&#8217;t do things the way I expected.</q></p>
<p>Here, I believe is a decent translation of the above into Common Lisp:</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> dtft <span style="color: #66cc66;">&#40;</span>samples<span style="color: #66cc66;">&#41;</span>
  #'<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>omega<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>loop for nn from <span style="color: #cc66cc;">0</span> below <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">length</span> samples<span style="color: #66cc66;">&#41;</span>
           summing <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>angle <span style="color: #66cc66;">&#40;</span>* omega nn<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;">aref</span> samples nn<span style="color: #66cc66;">&#41;</span>
                        <span style="color: #66cc66;">&#40;</span>complex <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cos</span> angle<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">sin</span> angle<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>Now, what I find most interesting here is that most implementations you&#8217;ll find for the DTFT (Discrete-Time Fourier Transform) take an array of samples and a wavelength, and returns a result.  This, instead, returns a function which you can call with a wavelength <em>omega</em>.  It returns an evaluator.  This is an interesting pattern that I will have to try to keep in mind.  I have used it in some places before.  But, I am sure there are other places that I should have used it, and missed.  This is one where I would have missed.</p>
<p>Usage for the above would go something like:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>dd <span style="color: #66cc66;">&#40;</span>dtft samples<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>dd angle1<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>dd angle2<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">...</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>For those not into Lisp either (who are you?), here is a rough translation into C++.</p>
<div class="codecolorer-container cpp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#include &lt;cmath&gt;</span>
<span style="color: #339900;">#include &lt;complex&gt;</span>
&nbsp;
<span style="color: #0000ff;">class</span> DTFT <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> len<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">double</span><span style="color: #000040;">*</span> samples<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  DTFT<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">double</span><span style="color: #000040;">*</span> _samples, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> _len <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>len <span style="color: #000080;">=</span> _len<span style="color: #008080;">;</span>
    this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>samples <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #0000ff;">double</span><span style="color: #008000;">&#91;</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>len <span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> ii<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> ii <span style="color: #000080;">&lt;</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>len<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>ii <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
      this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>samples<span style="color: #008000;">&#91;</span> ii <span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> _samples<span style="color: #008000;">&#91;</span> ii <span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  ~DTFT<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">void</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">delete</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>samples<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  std<span style="color: #008080;">::</span><span style="color: #007788;">complex</span><span style="color: #000080;">&lt;</span> <span style="color: #0000ff;">double</span> <span style="color: #000080;">&gt;</span> operator <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">double</span> omega <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span>
    std<span style="color: #008080;">::</span><span style="color: #007788;">complex</span><span style="color: #000080;">&lt;</span> <span style="color: #0000ff;">double</span> <span style="color: #000080;">&gt;</span> sum<span style="color: #008000;">&#40;</span> <span style="color:#800080;">0.0</span>, <span style="color:#800080;">0.0</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> ii<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> ii <span style="color: #000080;">&lt;</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>len<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>ii <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
      sum <span style="color: #000040;">+</span><span style="color: #000080;">=</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>samples<span style="color: #008000;">&#91;</span> ii <span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span> std<span style="color: #008080;">::</span><span style="color: #007788;">polar</span><span style="color: #008000;">&#40;</span> <span style="color:#800080;">1.0</span>, omega <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">return</span> sum<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div>
<p>With usage like:</p>
<div class="codecolorer-container cpp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">DTFT dd<span style="color: #008000;">&#40;</span> samples, <span style="color: #0000dd;">1024</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
dd<span style="color: #008000;">&#40;</span> angle1 <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
dd<span style="color: #008000;">&#40;</span> angle2 <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
...</pre></div>
<p>So, six lines of Scheme or Lisp.  Twenty-five lines of C++ including explicit definition of a class to act as a pseudo-closure, explicit copying and management of the samples buffer, etc.  I suppose, a more direct translation would have used a <em>std::vector</em> to hold the samples and would have just kept a pointer to the buffer.  That would have shaved off six or seven lines and the whole <em>len</em> and <em>_len</em> variables.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/07/why-not-return-a-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>To CLOS or not to CLOS</title>
		<link>http://nklein.com/2009/06/to-clos-or-not-to-clos/</link>
		<comments>http://nklein.com/2009/06/to-clos-or-not-to-clos/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 18:42:51 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[optimization]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=624</guid>
		<description><![CDATA[I am working on some Lisp code. I am trying to mimic the basic structure of a large C++ project. I think the way the C++ project is structured is a good fit for the tasks involved. Most of the C++ stuff is done with classes. Most of the methods of those classes are virtual [...]]]></description>
			<content:encoded><![CDATA[<p>I am working on some Lisp code.  I am trying to mimic the basic structure of a large C++ project.  I think the way the C++ project is structured is a good fit for the tasks involved.</p>
<p>Most of the C++ stuff is done with classes.  Most of the methods of those classes are virtual methods.  Many of the methods will be called multiple times every hundredth of a second.  Very few of the virtual methods ever get overridden by subclasses.</p>
<p>So, I got to thinking.  Does it make sense for me to use CLOS stuff at all for most of this?  Would it be significantly faster to use <em>(defstruct&nbsp;&#8230;)</em> and <em>(defun&nbsp;&#8230;)</em> instead of <em>(defclass&nbsp;&#8230;)</em> and <em>(defgeneric&nbsp;&#8230;)</em>?</p>
<p>My gut instinct was: <q>Yes.</q>  My first set of test code didn&#8217;t bear that out.  For both the classes and the structs, I used <em>(MAKE-INSTANCE&nbsp;&#8230;)</em> to allocate and <em>(WITH-SLOTS&nbsp;&#8230;)</em> to access.  When doing so, the classes with generic functions took about 1.5 seconds for 10,000,000 iterations under SBCL while the structs with non-generic functions took about 2.2 seconds.</p>
<p>For the next iteration of testing, I decided to use accessors instead of slots.  I had the following defined:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>defclass base-class <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>slot-one <span style="color: #66cc66;">:</span><span style="color: #555;">initform</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> 1<span style="color: #66cc66;">.</span>0f0<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">type</span> single-<span style="color: #b1b100;">float</span>
             <span style="color: #66cc66;">:</span><span style="color: #555;">accessor</span> class-slot-one<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span>slot-two <span style="color: #66cc66;">:</span><span style="color: #555;">initform</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> 1<span style="color: #66cc66;">.</span>0f0<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">type</span> single-<span style="color: #b1b100;">float</span>
             <span style="color: #66cc66;">:</span><span style="color: #555;">accessor</span> class-slot-two<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defclass sub-class <span style="color: #66cc66;">&#40;</span>base-class<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>slot-three <span style="color: #66cc66;">:</span><span style="color: #555;">initform</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> 1<span style="color: #66cc66;">.</span>0f0<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">type</span> single-<span style="color: #b1b100;">float</span>
               <span style="color: #66cc66;">:</span><span style="color: #555;">accessor</span> class-slot-three<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span>slot-four  <span style="color: #66cc66;">:</span><span style="color: #555;">initform</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> 1<span style="color: #66cc66;">.</span>0f0<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">type</span> single-<span style="color: #b1b100;">float</span>
               <span style="color: #66cc66;">:</span><span style="color: #555;">accessor</span> class-slot-four<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defstruct <span style="color: #66cc66;">&#40;</span>base-struct<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>slot-one <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> 1<span style="color: #66cc66;">.</span>0f0<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">type</span> single-<span style="color: #b1b100;">float</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>slot-two <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> 1<span style="color: #66cc66;">.</span>0f0<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">type</span> single-<span style="color: #b1b100;">float</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defstruct <span style="color: #66cc66;">&#40;</span>sub-struct <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">include</span> base-struct<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>slot-three <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> 1<span style="color: #66cc66;">.</span>0f0<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">type</span> single-<span style="color: #b1b100;">float</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>slot-four <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> 1<span style="color: #66cc66;">.</span>0f0<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">type</span> single-<span style="color: #b1b100;">float</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>I then switched from using calls like <em>(slot-value instance &#8216;slot-three)</em> in my functions to using calls like <em>(class-slot-three instance)</em> or <em>(sub-struct-slot-three instance)</em>.  Now, 10,000,000 iterations took 2.6 seconds for the classes and 0.3 seconds for the structs in SBCL.  In Clozure (64-bit), 10,000,000 iterations with classes and with method-dispatch and with accessors took 11.0 seconds, with classess and without method-dispatch but with accessors took 6.1 seconds, and with structs and without method-dispatch and with accessors took 0.4 seconds.</p>
<p>There is much more that I can explore to tune this code.  But, for now, I think the answer is fairly easy.  I am going to use structs, functions, and accessors for as many cases as I can.  Here is the <a href="http://nklein.com/wp-content/uploads/2009/06/gf.lisp">generic-function, class, struct test code</a> with accessors.  The first loop uses classes and generic functions.  The second loop uses classes and regular functions.  The third loop uses structs and regular functions.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/06/to-clos-or-not-to-clos/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How I Know I Don&#8217;t Know Enough About Lisp Macros</title>
		<link>http://nklein.com/2009/06/how-i-know-i-dont-know-enough-about-lisp-macros/</link>
		<comments>http://nklein.com/2009/06/how-i-know-i-dont-know-enough-about-lisp-macros/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 05:40:02 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[m4]]></category>
		<category><![CDATA[macros]]></category>
		<category><![CDATA[nroff]]></category>
		<category><![CDATA[TeX]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=584</guid>
		<description><![CDATA[I have a pretty good grasp on macros in Lisp, C/C++, TeX, m4, and nroff. Lisp macros are, far and away, the most powerful in that set. In fact, Lisp macros get a bad rap by sharing a name with macros in those other languages. Still, I often hit the same wall when trying to [...]]]></description>
			<content:encoded><![CDATA[<p>I have a pretty good grasp on macros in Lisp, C/C++, TeX, m4, and nroff.  Lisp macros are, far and away, the most powerful in that set.  In fact, Lisp macros get a bad rap by sharing a name with macros in those other languages.  Still, I often hit the same wall when trying to write a Lisp macro so I know that there&#8217;s more I have to learn.</p>
<h3>A Quick Look at Macros</h3>
<p>First, let&#8217;s review macros for a moment to make sure we&#8217;re all on the same page.  The C/C++ macros are the simplest to understand so we&#8217;ll start there.  The C and C++ compilers run in two stages.  First, the C preprocessor runs to expand all of your macros.  Then, the compiler runs to actually compile your code.  In C and C++, macros are declared using the <em>#define</em> command to the preprocessor.  For example, you might do something like the following:<br />
<span id="more-584"></span></p>
<div class="codecolorer-container c blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#define START_TIMER  __start_time__ = get_current_time();</span>
<span style="color: #339933;">#define END_TIMER    __elapsed__ = get_current_time() - __start_time__;</span></pre></div>
<p>Then, later in you code, you might do something like:</p>
<div class="codecolorer-container c blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">START_TIMER
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>ii <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> ii <span style="color: #339933;">&lt;</span> MAX_ITERATIONS<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        do_foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
END_TIMER</pre></div>
<p>The C preprocessor will replace the <em>START_TIMER</em> and <em>END_TIMER</em> with the code you declared in the <em>#define</em>.  Your resulting code would then go into the actual compile phase looking like this:</p>
<div class="codecolorer-container c blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">__start_time__ <span style="color: #339933;">=</span> get_current_time<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>ii <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> ii <span style="color: #339933;">&lt;</span> MAX_ITERATIONS<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        do_foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
__elapsed__ <span style="color: #339933;">=</span> get_current_time<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> __start_time__<span style="color: #339933;">;</span></pre></div>
<p>The preprocessor did some simple string replacements on your behalf.  [Actually, it is somewhat helpful to think of it as doing <b>token</b> replacement instead since the name of your macro has to be a valid C token and you have to jump through some ugly, implementation-specific hoops to get it to concatenate things rather than whitespace separate them.]  You can also create macros with arguments like:</p>
<div class="codecolorer-container c blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#define WARN(x,y)     log_message( __LOG_LEVEL_WARN__, x, y )</span>
...
<span style="color: #202020;">WARN</span><span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;buffer_copy()&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Output buffer is not properly aligned&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div>
<p>If you look back at the <em>START_TIMER</em> and <em>END_TIMER</em> macros above, you will notice that the semicolons, which delimit statements in C and C++, are included in the macro definitions.  As such, they aren&#8217;t used explicitly where the macros are invoked.  So?  Well, that means that your program needn&#8217;t bear any resemblance to compilable C before the preprocessor is run.  Some sets of macros exploit this to make your C code look like FORTRAN or Bourne Shell or PASCAL or C++.</p>
<p>Lisp macros are a different beast entirely.  Invoking a macro in Lisp looks just like everything else in Lisp.  [To be fair, some macros (like <em>(LOOP&nbsp;...)</em>) go a little nutty and make things look like a Lisp wrapper around some non-Lisp.  Most macros are much more Lispy.  Additionally, Lisp has two different sorts of macros: macros and reader macros.  With reader macros, you could probably make your Lisp code look something like PASCAL if you were exceedingly ambitious and exceedingly misguided.  But, from there on out, when I say <q>macro</q>, I specifically mean just <q>macro</q> as distinguished from <q>reader macro</q>.]</p>
<p>For pedagogic purposes, let&#8217;s look at those same macros defined in Lisp:</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> START_TIMER <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> *start-time* <span style="color: #66cc66;">&#40;</span>get-current-time<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;">defmacro</span> END_TIMER <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> *elapsed* <span style="color: #66cc66;">&#40;</span>- <span style="color: #66cc66;">&#40;</span>get-current-time<span style="color: #66cc66;">&#41;</span> *start-time*<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;">defmacro</span> WARN <span style="color: #66cc66;">&#40;</span>x y<span style="color: #66cc66;">&#41;</span> `<span style="color: #66cc66;">&#40;</span>log_message +log-level-warn+ <span style="color: #66cc66;">,</span>x <span style="color: #66cc66;">,</span>y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>The first two macros are straight substitutions:  wherever you find the Lisp form <em>(START_TIMER)</em>, substitute the form <em>(setf *start-time* (get-current-time))</em>, and similarly for the <em>(END_TIMER)</em> form.  The single quote in front of the <em>(SETF&nbsp;&#8230;)</em> forms in those first two macros indicates that the following form, while parsed, is not to be evaluated at the time the macro is expanded.  In the <em>(WARN&nbsp;&#8230;)</em> macro, things are a bit more complicated.  The backquote before the <em>(LOG_MESSAGE&nbsp;&#8230;)</em> form indicates that the form itself is not to be evaluated at expansion time, but that some portions within it (those demarcated with commas) will need to be substituted.  As you would 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>WARN <span style="color: #ff0000;">&quot;square-root&quot;</span> <span style="color: #ff0000;">&quot;Not interested in handling negative arguments today&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>would expand into:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>log_message +log-level-warning+ <span style="color: #ff0000;">&quot;square-root&quot;</span> <span style="color: #ff0000;">&quot;Not interested in handling negative arguments today&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<h3>What makes Lisp macros so great?</h3>
<p>From what I&#8217;ve shown you so far, you might be thinking: <q>What make Lisp macros so great?  Everything you&#8217;ve done with Lisp macros, I can do with the C preprocessor.  Further, I can do things with the C preprocessor that are syntactically nothing at all like C.  You already said you can&#8217;t do Lisp macros that are syntactically nothing like Lisp.</q></p>
<p>There is, of course, an aesthetic argument about why it would be better if the C preprocessor enforced some semblance of C on you.  I wouldn&#8217;t respect that argument much either if someone were poking at my language.  Aesthetics is good.  Aesthetics is important.  Should the compiler enforce them?  Only if it&#8217;s going to give me something much better in return.</p>
<p>What do Lisp macros give you?  They give you Lisp!  What more could you possible want?</p>
<p>Let&#8217;s say that you&#8217;ve written code in Assembly Language or BASIC so long that the code you&#8217;re writing today screams for a <a href="http://en.wikipedia.org/wiki/Jump_table">Jump table or computed GOTO</a>.  Heck, the code you wrote yesterday used a jump table, too?  Maybe you could factor out jump tables.  You could do this in C by creating a function:</p>
<div class="codecolorer-container c blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">void</span> do_jump_table<span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> choice<span style="color: #339933;">,</span> <span style="color: #993333;">void</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>array_of_functions<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span> <span style="color: #993333;">void</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>array_of_functions<span style="color: #009900;">&#91;</span>choice<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
...
<span style="color: #993333;">void</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>my_jump_table<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span> <span style="color: #993333;">void</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> foo<span style="color: #339933;">,</span> bar<span style="color: #339933;">,</span> baz<span style="color: #339933;">,</span> cat<span style="color: #339933;">,</span> dog<span style="color: #339933;">,</span> horse<span style="color: #339933;">,</span> tick <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
do_jump_table<span style="color: #009900;">&#40;</span> choice<span style="color: #339933;">,</span> my_jump_table <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div>
<p>That will work for you as long it&#8217;s okay that all of your functions have the same signature, you don&#8217;t mind the overhead of the extra function call to <em>do_jump_table()</em>, you don&#8217;t mind making a separate function for each different jump table option, and your choice never stumbles past the end of the jump table.  You can do it this way in Lisp if you like, too (without having to write a different <em>do_jump_table</em> for every different return-type you might like from your jump tables.</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> do-jump-table <span style="color: #66cc66;">&#40;</span>choice array-of-functions<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">aref</span> array-of-functions choice<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">...</span>
<span style="color: #66cc66;">&#40;</span>do-jump-table choice #<span style="color: #66cc66;">&lt;</span> #'foo<span style="color: #66cc66;">,</span> #'bar<span style="color: #66cc66;">,</span> #'baz<span style="color: #66cc66;">,</span> #'cat<span style="color: #66cc66;">,</span> #'dog<span style="color: #66cc66;">,</span> #'horse<span style="color: #66cc66;">,</span> #'tick <span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>What if you do care about the overhead of the extra function call to <em>do_jump_table()</em>, you do mind making a separate function for each different jump table option, or you are concerned about doing something graceful when your choice stumbles past the end of the jump table?   You could address that last concern with more error checking and, in the C case, more parameters to the function.  The other concerns are tougher though.</p>
<p>If we don&#8217;t want to have to write separate <em>foo</em>, <em>bar</em>, <em>baz</em>, etc. functions, then we want to try this with macros.  Rather than taking an array of function pointers, we&#8217;re going to let it take some variable number of statements&#8212;one for each case in the jump table.  Let&#8217;s start with the Lisp version:</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> do-jump-table <span style="color: #66cc66;">&#40;</span> choice <span style="color: #66cc66;">&amp;</span>rest options <span style="color: #66cc66;">&#41;</span>
  `<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">case</span> <span style="color: #66cc66;">,</span>choice
     <span style="color: #66cc66;">,</span>@<span style="color: #66cc66;">&#40;</span>loop for ii from <span style="color: #cc66cc;">0</span>
            for oo in options
            collecting <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> ii oo<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#40;</span>otherwise <span style="color: #66cc66;">&#40;</span>do-something-graceful<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>Then, we could use it with some code 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>do-jump-table op-code
               <span style="color: #66cc66;">&#40;</span>write <span style="color: #ff0000;">&quot;NOOP&quot;</span><span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#40;</span>write <span style="color: #ff0000;">&quot;SYNC&quot;</span><span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&gt;=</span> +protocol-version+ <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
                 <span style="color: #66cc66;">&#40;</span>write <span style="color: #ff0000;">&quot;QUIT&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>This would expand to:</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;">case</span> op-code
  <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#40;</span>write <span style="color: #ff0000;">&quot;NOOP&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#40;</span>write <span style="color: #ff0000;">&quot;SYNC&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&gt;=</span> +protocol-version+ <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>write <span style="color: #ff0000;">&quot;QUIT&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>otherwise <span style="color: #66cc66;">&#40;</span>do-something-graceful<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>I challenge you to make such a macro in C, C++, TeX, m4, or nroff.  I&#8217;ve written lots of custom C programs in the past to preprocess my code before sending it to the C preprocessor and the C compiler.  In Lisp, it&#8217;s all right there.  I can use all of Lisp&#8217;s magic to turn my code into the code I want it to become.  No temporary files.  No fancy Makefile tricks to get everything squared away.  It&#8217;s just all there.</p>
<h3>What don&#8217;t I know?</h3>
<p>The <em>do-jump-table</em> macro we just defined expands to one statement (one lisp form): <em>(CASE&nbsp;&#8230;)</em>.  There are many times when I want my macro to expand to more than one form, however.  This isn&#8217;t legit.  I still end up wanting to do it.  For example, I might think that since I&#8217;m doing things with macros, that I might want the whole <em>(CASE&nbsp;&#8230;)</em> construct to be in the main code and just have the macro expand the different cases:</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;">case</span> op-code
    <span style="color: #66cc66;">&#40;</span>do-jump-table-cases <span style="color: #66cc66;">&#40;</span>write <span style="color: #ff0000;">&quot;NOOP&quot;</span><span style="color: #66cc66;">&#41;</span>
                         <span style="color: #66cc66;">&#40;</span>write <span style="color: #ff0000;">&quot;SYNC&quot;</span><span style="color: #66cc66;">&#41;</span>
                         <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&gt;=</span> +protocol-version+ <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
                             <span style="color: #66cc66;">&#40;</span>write <span style="color: #ff0000;">&quot;QUIT&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>otherwise <span style="color: #66cc66;">&#40;</span>do-something-especially-graceful<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Lisp says no.  I can understand why it says that.  In this case, it&#8217;s easy enough to work around.  But, in other cases, I end up flailing.</p>
<p>I feel that once I understand Lisp macros more completely, I won&#8217;t keep trying to jam that gorgeous, round peg into all of these square holes in my logic.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/06/how-i-know-i-dont-know-enough-about-lisp-macros/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Programming When Clarity Counts</title>
		<link>http://nklein.com/2009/05/programming-when-clarity-counts/</link>
		<comments>http://nklein.com/2009/05/programming-when-clarity-counts/#comments</comments>
		<pubDate>Wed, 06 May 2009 04:10:25 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=285</guid>
		<description><![CDATA[In my previous post, I wrote some code in Perl because I wanted the code to be clear and obvious. Wait? What? Who uses Perl when they want clarity? Perl I admit: I was surprised myself. Seriously, I tried several other languages first. Let&#8217;s just focus on the first loop here. Here&#8217;s the Perl again [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://nklein.com/2009/05/how-to-make-a-weighted-random-choice/">my previous post</a>,  I wrote some code in Perl because I wanted the code to be clear and obvious.  Wait?  What?  Who uses Perl when they want clarity?</p>
<h3>Perl</h3>
<p>I admit:  I was surprised myself.  Seriously, I tried several other languages first.  Let&#8217;s just focus on the first loop here.  Here&#8217;s the Perl again for reference:</p>
<div class="codecolorer-container perl blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$total</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$item</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">@list_of_items</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$total</span> <span style="color: #339933;">+=</span> weight<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$item</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div>
<h3>Objective C</h3>
<p>The actual code that I was attempting to explain was in Objective C with the NeXTStep foundation classes.  I couldn&#8217;t imagine that anyone would want to try to understand it:</p>
<div class="codecolorer-container objc blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a61390;">double</span> total <span style="color: #002200;">=</span> <span style="color: #2400d9;">0.0</span>;
<span style="color: #400080;">NSEnumerator</span><span style="color: #002200;">*</span> ee <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>itemList objectEnumerator<span style="color: #002200;">&#93;</span>;
Item<span style="color: #002200;">*</span> item;
<span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span> item <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>Item<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>ee nextObject<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    total <span style="color: #002200;">+=</span> <span style="color: #002200;">&#91;</span>item weight<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div>
<h3>C</h3>
<p>My first attempt for the article was to write it in C using an array of items.</p>
<div class="codecolorer-container c blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">double</span> total <span style="color: #339933;">=</span> <span style="color:#800080;">0.0</span><span style="color: #339933;">;</span>
<span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> ii<span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> ii <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> ii <span style="color: #339933;">&lt;</span> itemCount<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>ii <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    total <span style="color: #339933;">+=</span> weight<span style="color: #009900;">&#40;</span> itemArray<span style="color: #009900;">&#91;</span> ii <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div>
<p>That&#8217;s not too terrible.  There is, however, a great deal of syntax and code devoted to handling the array including an extra variable to track its length.  Beyond that, the loop counter is of little use to someone trying to understand the concept.</p>
<h3>C++</h3>
<p>My next go was C++.  My hope was that iterators would make the looping less painful.  C++ iterators, however, are the very model of pain.</p>
<div class="codecolorer-container cpp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">double</span> total <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span><span style="color: #008080;">;</span>
std<span style="color: #008080;">::</span><span style="color: #007788;">list</span><span style="color: #000080;">&lt;</span> Item<span style="color: #000040;">*</span> <span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">const_iterator</span> it<span style="color: #008080;">;</span>
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> it <span style="color: #000080;">=</span> itemList.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> it <span style="color: #000040;">!</span><span style="color: #000080;">=</span> itemList.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>it <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    total <span style="color: #000040;">+</span><span style="color: #000080;">=</span> weight<span style="color: #008000;">&#40;</span> <span style="color: #000040;">*</span>it <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div>
<p>This requires the reader to wade through a syntactic thicket to get the iterator into the picture at all.  It also requires a certain comfort with pointers that one wouldn&#8217;t have coming from another language.</p>
<h3>Java</h3>
<p>My next attempt was Java.  I hate Java.  It may not be terrible for this code with the new looping constructs that came in Java 1.5.  Alas, I quit coding it before I made it through this first loop.</p>
<h3>Lisp</h3>
<p>Of course, I wanted to code this in Lisp:</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>total <span style="color: #66cc66;">&#40;</span>reduce #'+ list-of-items <span style="color: #66cc66;">:</span><span style="color: #555;">key</span> #'weight<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">...</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>That would be perfectly clear to someone who has used Lisp or Scheme.  Alas, not everyone has.</p>
<h3>So&#8230; Perl&#8230;</h3>
<p>If you want to write the Perl code, you have to understand the difference between scalars and arrays.  To read the code, however, you don&#8217;t have to grok that distinction to see what&#8217;s going on.  I didn&#8217;t try it in Python.  Maybe it would be clear to a wider audience.  I may have to try it next time.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/05/programming-when-clarity-counts/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Sapir-Whorf with Programming Languages</title>
		<link>http://nklein.com/2009/02/sapir-whorf-wit-programming-languages/</link>
		<comments>http://nklein.com/2009/02/sapir-whorf-wit-programming-languages/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 22:29:31 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[objective c]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=120</guid>
		<description><![CDATA[I've been coding in Objective-C for a month or so now.  It is interesting (in a <a href="http://en.wikipedia.org/wiki/Sapir-Whorf_hypothesis">Sapir-Whorf</a> sort of way), how the language changes the way that I code.  Switching from Objective-C to Lisp made me notice something that I hadn't really noticed when moving from C++/Java/Perl to Lisp.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been coding in Objective-C for a month or so now.  It is interesting (in a <a href="http://en.wikipedia.org/wiki/Sapir-Whorf_hypothesis">Sapir-Whorf</a> sort of way), how the language changes the way that I code.  Switching from Objective-C to Lisp made me notice something that I hadn&#8217;t really noticed when moving from C++/Java/Perl to Lisp.</p>
<blockquote><p>
In Lisp, I will pull something out into a separate function if it makes the current function more self-contained, more one idea.  In C++, I will only pull something out into a separate function if I need the same functionality in multiple places.
</p></blockquote>
<p>Actually, it&#8217;s even worse than that in C++.  For stuff that is less than six or so lines, I might maintain it in several functions.  Or, if I&#8217;m using some Literate Programming tool, I will just use the same chunk in multiple places.  The notable exception in C++ is when I want to use something as a loop conditional, I may bother to break it out into its<br />
own function:</p>
<div class="codecolorer-container cpp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span> incrementCounter<span style="color: #008000;">&#40;</span> cntr, min, max, dimensions <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// body of loop here</span>
<span style="color: #008000;">&#125;</span></pre></div>
<p>In C++ or Objective-C, I might do something like this:</p>
<div class="codecolorer-container cpp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">// some portion of my function</span>
&nbsp;
<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> choice <span style="color: #000080;">=</span> random<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">%</span> length<span style="color: #008080;">;</span>
<span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> currentChoice <span style="color: #000080;">=</span> options<span style="color: #008000;">&#91;</span> choice <span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">// yes, I know I can memcpy(), but that's not as obvious</span>
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> ii<span style="color: #000080;">=</span>choice<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> ii <span style="color: #000080;">&lt;</span> length<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>ii <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    options<span style="color: #008000;">&#91;</span> ii<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span> <span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> options<span style="color: #008000;">&#91;</span> ii <span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #000040;">--</span>length<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// some code using currentChoice</span></pre></div>
<p>In Lisp, I would never consider keeping that code inline.  I would put it in another function right away.</p>
<p>This, this is one of the things I meant when I said <q>Lisp is just plain fun.</q>  It&#8217;s easy to make a new function.  I can just do it.  I don&#8217;t have to fret too much over the name.  I don&#8217;t have to fret too much over the argument<br />
list.  I don&#8217;t have to pre-declare it in this header file with the same signature as that implementation file.  I<br />
don&#8217;t have to pretend its a method when it&#8217;s really just a function.  I can return multiple values if I need to do so.  I don&#8217;t have to worry much about which compilation units will need to see this to compile.</p>
<p>Part of the maze generation code that I wrote in Objective-C needs to track walls.  I don&#8217;t need the same structure during generation that I will use once it&#8217;s generated.  So, I have a Wall class declared.  It feels wrong to declare it right in the header file for the Maze.  It feels silly to break it out into its own header file.  What I should be doing is making a separate MazeFactory and have its implementation include the declaration of this Wall class.  But, that is such overkill here.  I just want the darn maze.</p>
<p>In Lisp, I would just be done with no feelings of guilt at all.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/02/sapir-whorf-wit-programming-languages/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>First decent images from my new raytracer</title>
		<link>http://nklein.com/2008/09/first-decent-images-from-my-new-raytracer/</link>
		<comments>http://nklein.com/2008/09/first-decent-images-from-my-new-raytracer/#comments</comments>
		<pubDate>Thu, 25 Sep 2008 23:13:40 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[mpi]]></category>
		<category><![CDATA[rt]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=490</guid>
		<description><![CDATA[Some time ago, I wrote an n-dimensional raytracer in C++. It does a fair number of things, none of them efficiently, most of the rigidly. There are a bunch of things that I wanted to do with it for a long time, but it&#8217;s been too slow and rigid to make any of those things [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago, I wrote an <a href="http://www.nklein.com/products/rt">n-dimensional raytracer</a> in C++.  It does a fair number of things, none of them efficiently, most of the rigidly.</p>
<p>There are a bunch of things that I wanted to do with it for a long time, but it&#8217;s been too slow and rigid to make any of those things fun.</p>
<p>Enter Lisp.  As soon as it made it through my skull that Lisp is actually compiled (honest-to-goodness your-CPU instructions), I wanted to rewrite the whole thing in Lisp.  I have finally gotten started on doing that.  And, I just made it to the point where I&#8217;m actually tracing rays.  Here is a stereo pair of a three-dimensional scene:</p>
<p><center></p>
<table cellpadding="2" cellspacing="0">
<tr>
<td><a href="http://nklein.com/wp-content/uploads/2009/06/lrt-l.png"><img src="http://nklein.com/wp-content/uploads/2009/06/lrt-l.png" alt="lrt-l" title="lrt-l" width="240" height="135" class="alignnone size-full wp-image-491" /></a></td>
<td><a href="http://nklein.com/wp-content/uploads/2009/06/lrt-r.png"><img src="http://nklein.com/wp-content/uploads/2009/06/lrt-r.png" alt="lrt-r" title="lrt-r" width="240" height="135" class="alignnone size-full wp-image-492" /></a></td>
</tr>
</table>
<p></center></p>
<p><span id="more-490"></span></p>
<p>It use&#8217;s <a href="http://xach.livejournal.com/">xach</a>&#8216;s ZPNG library for output, my OpenMPI library for sharing work across machines, and a thin layer that I wrote on top of Portable Threads for threading within a machine.</p>
<p>It doesn&#8217;t yet do reflections and refractions, directional lights, or most of the shapes that my old raytracer does.<br />
But, it&#8217;s already got multithreading, MPI, more meaningful camera parameters, and functional color characteristics.</p>
<p>So, here&#8217;s the source code that generated the above images. Note that the one sphere has checkboarded diffuseness and the other has gradated phong-exponent and positional coloring.</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><span style="color: #b1b100;">defun</span> make-checkerboard-func <span style="color: #66cc66;">&#40;</span>a b<span style="color: #66cc66;">&#41;</span>
  #'<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&amp;</span>amp<span style="color: #808080; font-style: italic;">;key position)</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>sum <span style="color: #66cc66;">&#40;</span>reduce #'+
			 position
			 <span style="color: #66cc66;">:</span><span style="color: #555;">key</span> #'<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>vv<span style="color: #66cc66;">&#41;</span>
				  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&amp;</span>lt<span style="color: #808080; font-style: italic;">; 1.0 (mod vv 2.0)) 1 0)))))</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">evenp</span> sum<span style="color: #66cc66;">&#41;</span> a b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> my-universe <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">universe</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">spatial-dimensions</span> <span style="color: #cc66cc;">3</span>
		<span style="color: #66cc66;">:</span><span style="color: #555;">color-dimensions</span> <span style="color: #cc66cc;">3</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>look-at <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span> <span style="color: #cc66cc;">18.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	  <span style="color: #66cc66;">&#40;</span>eye-offset <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.25</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	  <span style="color: #66cc66;">&#40;</span>aspect <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span> <span style="color: #cc66cc;">16.0</span> <span style="color: #cc66cc;">9.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	  <span style="color: #66cc66;">&#40;</span>field-of-view <span style="color: #cc66cc;">120.0</span><span style="color: #66cc66;">&#41;</span>
	  <span style="color: #66cc66;">&#40;</span>object-scale <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span> <span style="color: #cc66cc;">3.0</span> <span style="color: #cc66cc;">3.0</span> <span style="color: #cc66cc;">3.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	  <span style="color: #66cc66;">&#40;</span>x-axis <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	  <span style="color: #66cc66;">&#40;</span>y-axis <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	  <span style="color: #66cc66;">&#40;</span>z-axis <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	  <span style="color: #66cc66;">&#40;</span>angle <span style="color: #cc66cc;">10.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">with-transforms</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">translation</span> <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span>* eye-offset -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>
			   <span style="color: #66cc66;">:</span><span style="color: #555;">look-at</span> look-at<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">camera</span> <span style="color: #66cc66;">:</span><span style="color: #b1b100;">name</span> <span style="color: #66cc66;">:</span><span style="color: #555;">main-camera-</span><span style="color: #b1b100;">l</span>
		   <span style="color: #66cc66;">:</span><span style="color: #555;">aspect</span> aspect
		   <span style="color: #66cc66;">:</span><span style="color: #555;">field-of-view</span> field-of-view<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">with-transforms</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">translation</span> <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span>* eye-offset <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>
			   <span style="color: #66cc66;">:</span><span style="color: #555;">look-at</span> look-at<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">camera</span> <span style="color: #66cc66;">:</span><span style="color: #b1b100;">name</span> <span style="color: #66cc66;">:</span><span style="color: #555;">main-camera-r</span>
		   <span style="color: #66cc66;">:</span><span style="color: #555;">aspect</span> aspect
		   <span style="color: #66cc66;">:</span><span style="color: #555;">field-of-view</span> field-of-view<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">with-translation</span> <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span> <span style="color: #cc66cc;">0.0</span> -<span style="color: #cc66cc;">5.0</span> -<span style="color: #cc66cc;">2.0</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">light</span> <span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span> <span style="color: #cc66cc;">0.7</span> <span style="color: #cc66cc;">0.7</span> <span style="color: #cc66cc;">0.7</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>rt<span style="color: #66cc66;">:</span><span style="color: #555;">with-translation</span> <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span> -<span style="color: #cc66cc;">10.0</span> -<span style="color: #cc66cc;">5.0</span> <span style="color: #cc66cc;">8.0</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">light</span> <span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span> <span style="color: #cc66cc;">0.7</span> <span style="color: #cc66cc;">0.7</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">with-transforms</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">translation</span> look-at
			   <span style="color: #66cc66;">:</span><span style="color: #555;">scaling</span> object-scale
			   <span style="color: #66cc66;">:</span><span style="color: #555;">look-at</span> <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>
			   <span style="color: #66cc66;">:</span><span style="color: #555;">translation</span> <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span> -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>
			   <span style="color: #66cc66;">:</span><span style="color: #555;">rotation</span> <span style="color: #66cc66;">&#40;</span>x-axis y-axis angle<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">with-transforms</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">translation</span> <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span> <span style="color: #cc66cc;">0.0</span> -<span style="color: #cc66cc;">1.5</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>
			     <span style="color: #66cc66;">:</span><span style="color: #555;">color-scaling</span> <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span> <span style="color: #cc66cc;">0.75</span> <span style="color: #cc66cc;">0.75</span> <span style="color: #cc66cc;">0.75</span><span style="color: #66cc66;">&#41;</span>
			     <span style="color: #66cc66;">:</span><span style="color: #555;">color-rotation</span> <span style="color: #66cc66;">&#40;</span>y-axis z-axis angle<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	  <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">with-color</span> <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">c</span> <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span> <span style="color: #cc66cc;">0.2</span> <span style="color: #cc66cc;">0.8</span> <span style="color: #cc66cc;">0.2</span><span style="color: #66cc66;">&#41;</span>
			       <span style="color: #66cc66;">:</span><span style="color: #555;">diffuseness</span> <span style="color: #66cc66;">&#40;</span>make-checkerboard-func <span style="color: #cc66cc;">0.2</span> <span style="color: #cc66cc;">0.6</span><span style="color: #66cc66;">&#41;</span>
			       <span style="color: #66cc66;">:</span><span style="color: #555;">specularity</span> <span style="color: #cc66cc;">0.4</span><span style="color: #66cc66;">&#41;</span>
	    <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">sphere</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>rt<span style="color: #66cc66;">:</span><span style="color: #555;">with-transforms</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">translation</span> <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span> <span style="color: #cc66cc;">2.0</span> <span style="color: #cc66cc;">2.0</span> <span style="color: #cc66cc;">1.5</span><span style="color: #66cc66;">&#41;</span>
			     <span style="color: #66cc66;">:</span><span style="color: #555;">scaling</span> <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span> <span style="color: #cc66cc;">1.5</span> <span style="color: #cc66cc;">1.5</span> <span style="color: #cc66cc;">1.5</span><span style="color: #66cc66;">&#41;</span>
			     <span style="color: #66cc66;">:</span><span style="color: #555;">color-translation</span> <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.2</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	  <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">with-color</span> <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">c</span> #'<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&amp;</span>amp<span style="color: #808080; font-style: italic;">;key position)</span>
				   <span style="color: #66cc66;">&#40;</span>map 'vector #'<span style="color: #b1b100;">abs</span> position<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
			       <span style="color: #66cc66;">:</span><span style="color: #555;">diffuseness</span> <span style="color: #cc66cc;">0.6</span>
			       <span style="color: #66cc66;">:</span><span style="color: #555;">specularity</span> <span style="color: #cc66cc;">0.4</span>
			       <span style="color: #66cc66;">:</span><span style="color: #555;">phong-exponent</span> #'<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&amp;</span>amp<span style="color: #808080; font-style: italic;">;key position)</span>
						   <span style="color: #66cc66;">&#40;</span>+ <span style="color: #cc66cc;">10</span>
						      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">abs</span> <span style="color: #66cc66;">&#40;</span>* <span style="color: #cc66cc;">100</span>
							     <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">aref</span> position <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
							     <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	      <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">sphere</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>rt<span style="color: #66cc66;">:</span><span style="color: #555;">with-rotation</span> <span style="color: #66cc66;">&#40;</span>x-axis y-axis angle<span style="color: #66cc66;">&#41;</span>
	  <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">with-translation</span> <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span> -<span style="color: #cc66cc;">15.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>
	    <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">with-color</span> <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">c</span> <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span> <span style="color: #cc66cc;">0.8</span> <span style="color: #cc66cc;">0.2</span> <span style="color: #cc66cc;">0.2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	      <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">halfspace</span> <span style="color: #66cc66;">:</span><span style="color: #b1b100;">name</span> <span style="color: #66cc66;">:</span><span style="color: #555;">hspace1</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><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
#+<span style="color: #66cc66;">:</span><span style="color: #555;">openmpi</span>
<span style="color: #66cc66;">&#40;</span>mpi<span style="color: #66cc66;">:</span><span style="color: #555;">init</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">with-workers</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</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>uu <span style="color: #66cc66;">&#40;</span>my-universe<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	 <span style="color: #66cc66;">&#40;</span>dpd <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">v</span> <span style="color: #cc66cc;">2.0</span> <span style="color: #cc66cc;">2.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">render-png</span> <span style="color: #66cc66;">:</span><span style="color: #555;">dots-per-degree</span> dpd
		    <span style="color: #66cc66;">:</span><span style="color: #555;">universe</span> uu
		    <span style="color: #66cc66;">:</span><span style="color: #555;">filename</span> #P<span style="color: #ff0000;">&quot;output-l.png&quot;</span>
		    <span style="color: #66cc66;">:</span><span style="color: #555;">camera-</span><span style="color: #b1b100;">name</span> <span style="color: #66cc66;">:</span><span style="color: #555;">main-camera-</span><span style="color: #b1b100;">l</span><span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#40;</span>rt<span style="color: #66cc66;">:</span><span style="color: #555;">render-png</span> <span style="color: #66cc66;">:</span><span style="color: #555;">dots-per-degree</span> dpd
		    <span style="color: #66cc66;">:</span><span style="color: #555;">universe</span> uu
		    <span style="color: #66cc66;">:</span><span style="color: #555;">filename</span> #P<span style="color: #ff0000;">&quot;output-r.png&quot;</span>
		    <span style="color: #66cc66;">:</span><span style="color: #555;">camera-</span><span style="color: #b1b100;">name</span> <span style="color: #66cc66;">:</span><span style="color: #555;">main-camera-r</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
#+<span style="color: #66cc66;">:</span><span style="color: #555;">openmpi</span>
<span style="color: #66cc66;">&#40;</span>mpi<span style="color: #66cc66;">:</span><span style="color: #555;">finalize</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>And, here are the same images swapped for those of you who prefer cross-eyed stereo pairs:</p>
<p><center></p>
<table cellpadding="2" cellspacing="0">
<tr>
<td><a href="http://nklein.com/wp-content/uploads/2009/06/lrt-r.png"><img src="http://nklein.com/wp-content/uploads/2009/06/lrt-r.png" alt="lrt-r" title="lrt-r" width="240" height="135" class="alignnone size-full wp-image-492" /></a></td>
<td><a href="http://nklein.com/wp-content/uploads/2009/06/lrt-l.png"><img src="http://nklein.com/wp-content/uploads/2009/06/lrt-l.png" alt="lrt-l" title="lrt-l" width="240" height="135" class="alignnone size-full wp-image-491" /></a></td>
</tr>
</table>
<p></center></p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2008/09/first-decent-images-from-my-new-raytracer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>rt v2.7.2007.05.17</title>
		<link>http://nklein.com/2007/05/rt-v2720070517/</link>
		<comments>http://nklein.com/2007/05/rt-v2720070517/#comments</comments>
		<pubDate>Thu, 17 May 2007 15:45:04 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[New Versions]]></category>
		<category><![CDATA[Releases]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[rt]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=321</guid>
		<description><![CDATA[The documentation The source code The feature set now includes: Phong shading, reflection, refraction Functional textures (including textures generated by raytracing other scenes) Cylinders (including spheres and cubes) Quadratic surfaces Regular polytopes Halfspaces Convex hulls of a sets of points Extrusions of lower-dimensional objects Intersections, unions, and complements Different colors for k-dimensional subfacets of convex [...]]]></description>
			<content:encoded><![CDATA[<ul>
<li>The <a href='http://nklein.com/wp-content/uploads/2009/05/rt2720070517.pdf'>documentation</a></li>
<li>The <a href='http://nklein.com/wp-content/uploads/2009/05/rt2720070517.tgz'>source code</a></li>
<li>The feature set now includes:
<ul>
<li>Phong shading, reflection, refraction</li>
<li>Functional textures (including textures generated by<br />
	raytracing other scenes)</li>
<li>Cylinders (including spheres and cubes)</li>
<li>Quadratic surfaces</li>
<li>Regular polytopes</li>
<li>Halfspaces</li>
<li>Convex hulls of a sets of points</li>
<li>Extrusions of lower-dimensional objects</li>
<li>Intersections, unions, and complements</li>
<li>Different colors for k-dimensional subfacets<br />
	of convex hulls and coxeter polytopes</li>
</ul>
</li>
<li>Here are some sample images:
<ul>
<li>A stereo pair of slices of <a href="http://nklein.com/wp-content/uploads/2009/05/24.png">some 24-cells</a> along with the <a href="http://nklein.com/wp-content/uploads/2007/05/c24-s.rt">input file</a>.  The vertexes (0-facets) are pink, the edges (1-facets) are dark gray, the faces (2-facets) are yellowish, and the facets (3-facets) are green.</li>
<li>Some <a href="http://nklein.com/wp-content/uploads/2007/05/coxeter.jpg">3-D polytopes</a> with edges and vertexes colored differently and the <a href="http://nklein.com/wp-content/uploads/2007/05/coxeter.rt">input file</a>.</li>
</ul>
</li>
</ul>
<p class="clear">&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2007/05/rt-v2720070517/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

