<?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>items.sjbach.com</title>
	<atom:link href="http://items.sjbach.com/feed" rel="self" type="application/rss+xml" />
	<link>http://items.sjbach.com</link>
	<description></description>
	<lastBuildDate>Mon, 26 Apr 2010 22:05:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Critiquing Clojure</title>
		<link>http://items.sjbach.com/567/critiquing-clojure</link>
		<comments>http://items.sjbach.com/567/critiquing-clojure#comments</comments>
		<pubDate>Mon, 22 Jun 2009 04:13:04 +0000</pubDate>
		<dc:creator>Stephen Bach</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://items.sjbach.com/?p=567</guid>
		<description><![CDATA[
Table of contents
1. No dynamic binding of dynamic variables
2. Functions must be declared before first call form
3. No keyword arguments in lambda-lists
4. Commas are whitespace
5. Syntax for type specification is ugly
6. All the good names are taken
7. Conclusion

Last week Brian Carper published Five Things that Mildly Annoy Me in Clojure.  Here&#8217;s another short list. [...]]]></description>
			<content:encoded><![CDATA[<ul class="toc">
<li class="title">Table of contents</li>
<li><a href="#1">1. No dynamic binding of dynamic variables</a></li>
<li><a href="#2">2. Functions must be declared before first call form</a></li>
<li><a href="#3">3. No keyword arguments in lambda-lists</a></li>
<li><a href="#4">4. Commas are whitespace</a></li>
<li><a href="#5">5. Syntax for type specification is ugly</a></li>
<li><a href="#6">6. All the good names are taken</a></li>
<li><a href="#7">7. Conclusion</a></li>
</ul>
<p>Last week Brian Carper published <a href="http://briancarper.net/blog/five-things-that-mildly-annoy-me-in-clojure">Five Things that Mildly Annoy Me in Clojure</a>.  Here&#8217;s another short list.  Brian&#8217;s criticisms are perhaps more substantive than mine; I can&#8217;t claim to be more than a dilettante of the language.</p>
<p>Also: I hope this won&#8217;t be received as an attempt to dogpile what is perhaps the most compelling of a new wave of programming languages.  The points below &#8212; in order of increasing subjectivity &#8212; are but a by-product of genuine interest.</p>
<p><a name="1"></a></p>
<h3>1. No dynamic binding of dynamic variables</h3>
<p>In Clojure, variables defined using <code>def</code> and its variants have dynamic scope.  Dynamic variables provide a way to circumvent the functional, side effect-free nature of Clojure:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">def</span> *foo* <span style="color: #cc66cc;">0</span><span style="color: #005a89;">&#41;</span>
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">defn</span> print-foo <span style="color: #005a89;">&#91;</span><span style="color: #005a89;">&#93;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">println</span> *foo*<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #005a89;">&#40;</span>print-foo<span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #cc66cc;">0</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; The binding form rebinds dynamic variables:</span>
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">binding</span> <span style="color: #005a89;">&#91;</span>*foo* <span style="color: #cc66cc;">1</span><span style="color: #005a89;">&#93;</span>
  <span style="color: #005a89;">&#40;</span>print-foo<span style="color: #005a89;">&#41;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">println</span> *foo*<span style="color: #005a89;">&#41;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">binding</span> <span style="color: #005a89;">&#91;</span>*foo* <span style="color: #cc66cc;">2</span><span style="color: #005a89;">&#93;</span>
    <span style="color: #005a89;">&#40;</span>print-foo<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #cc66cc;">1</span>
   <span style="color: #cc66cc;">1</span>
   <span style="color: #cc66cc;">2</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; Compare with let, which binds lexically:</span>
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">let</span> <span style="color: #005a89;">&#91;</span>*foo* <span style="color: #cc66cc;">1</span><span style="color: #005a89;">&#93;</span>
  <span style="color: #005a89;">&#40;</span>print-foo<span style="color: #005a89;">&#41;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">println</span> *foo*<span style="color: #005a89;">&#41;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">let</span> <span style="color: #005a89;">&#91;</span>*foo* <span style="color: #cc66cc;">2</span><span style="color: #005a89;">&#93;</span>
    <span style="color: #005a89;">&#40;</span>print-foo<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #cc66cc;">0</span>
   <span style="color: #cc66cc;">1</span>
   <span style="color: #cc66cc;">0</span></pre></div></div>

<p>Common Lisp has analogues for the forms above, and also has the <code>PROGV</code> special form which allows dynamic re-binding of dynamic variables at runtime.  That is, the names of the variables which are to be rebound don&#8217;t need to be known at compile time as they do in Clojure.  See an example below:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">defvar</span> *foo* <span style="color: #cc66cc;">0</span><span style="color: #005a89;">&#41;</span>
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">defvar</span> *bar* <span style="color: #cc66cc;">1</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">defun</span> print-foo-bar <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#41;</span>
  <span style="color: #005a89;">&#40;</span>format <span style="color: #007719;">t</span> <span style="color: #ff0000;">&quot;~A ~A~%&quot;</span> *foo* *bar*<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">defun</span> call-print-foo-bar <span style="color: #005a89;">&#40;</span>&amp;optional vars vals<span style="color: #005a89;">&#41;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">progv</span> vars vals
    <span style="color: #005a89;">&#40;</span>print-foo-bar<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #005a89;">&#40;</span>call-print-foo-bar<span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">1</span>
<span style="color: #005a89;">&#40;</span>call-print-foo-bar '<span style="color: #005a89;">&#40;</span>*foo*<span style="color: #005a89;">&#41;</span> '<span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">1</span>
<span style="color: #005a89;">&#40;</span>call-print-foo-bar '<span style="color: #005a89;">&#40;</span>*foo* *bar*<span style="color: #005a89;">&#41;</span> '<span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">7</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">7</span></pre></div></div>

<p>The arguments to <code>CALL-PRINT-FOO-BAR</code> could come from a config file, an input stream, or anywhere else.</p>
<p>In practice, <code>PROGV</code> is used rarely &#8212; though effectively &#8212; and perhaps that&#8217;s an argument for excluding it from Clojure.  A related argument is that use of dynamic variables should be discouraged in Clojure.  But since <code>PROGV</code> represents functionality which can&#8217;t be built back into the language through macrology, its omission is a pity.</p>
<p><a name="2"></a></p>
<h3>2. Functions must be declared before first call form</h3>
<p>There may be a good technical reason for this, but it feels un-Lisplike.  In Common Lisp, you can do this:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">defun</span> fn1 <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#41;</span>
  <span style="color: #005a89;">&#40;</span>fn2 <span style="color: #000099;">:foo</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">defun</span> fn2 <span style="color: #005a89;">&#40;</span>arg<span style="color: #005a89;">&#41;</span>
  arg<span style="color: #005a89;">&#41;</span></pre></div></div>

<p>Even in Ruby:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC;">def</span> fn1
  fn2 <span style="color:#ff3333; font-weight:bold;">:foo</span>
<span style="color:#9966CC;">end</span>
&nbsp;
<span style="color:#9966CC;">def</span> fn2<span style="color:#006600; font-weight:bold;">&#40;</span>arg<span style="color:#006600; font-weight:bold;">&#41;</span>
  arg
<span style="color:#9966CC;">end</span></pre></div></div>

<p>But in Clojure, you must do this:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">declare</span> fn2<span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">defn</span> fn1 <span style="color: #005a89;">&#91;</span><span style="color: #005a89;">&#93;</span>
  <span style="color: #005a89;">&#40;</span>fn2 <span style="color: #000099;">:foo</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">defn</span> fn2 <span style="color: #005a89;">&#91;</span>arg<span style="color: #005a89;">&#93;</span>
  arg<span style="color: #005a89;">&#41;</span></pre></div></div>

<p>It&#8217;s a piece of bookkeeping which really should be the purview of the compiler.</p>
<p><a name="3"></a></p>
<h3>3. No keyword arguments in lambda-lists</h3>
<p>Functions in Common Lisp can accept named arguments:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">defun</span> func <span style="color: #005a89;">&#40;</span>a b c &amp;key d e<span style="color: #005a89;">&#41;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span> a b c d e<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #005a89;">&#40;</span>func <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #000099;">:e</span> <span style="color: #cc66cc;">5</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #007719;">NIL</span> <span style="color: #cc66cc;">5</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>This becomes helpful as a function accumulates many arguments &#8212; undesirable but inevitable &#8212; and it&#8217;s also useful in a short function when the purpose of its arguments can&#8217;t be made clear by its name.  Which of the following calls is more descriptive:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span>set-permissions resource <span style="color: #007719;">t</span> <span style="color: #007719;">nil</span> <span style="color: #007719;">t</span><span style="color: #005a89;">&#41;</span>
<span style="color: #005a89;">&#40;</span>set-permissions resource <span style="color: #000099;">:read</span> <span style="color: #007719;">t</span> <span style="color: #000099;">:write</span> <span style="color: #007719;">nil</span> <span style="color: #000099;">:execute</span> <span style="color: #007719;">t</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>The Clojure <code>defn</code> and <code>fn</code> forms can&#8217;t take keyword arguments.  A somewhat idiomatic alternative is to include a map parameter which is destructured for the function&#8217;s arguments:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">defn</span> func <span style="color: #005a89;">&#91;</span>a b c <span style="color: #005a89;">&#123;</span>d <span style="color: #000099;">:d</span> e <span style="color: #000099;">:e</span><span style="color: #005a89;">&#125;</span><span style="color: #005a89;">&#93;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span> a b c d e<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #005a89;">&#40;</span>func <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #005a89;">&#123;</span><span style="color: #000099;">:e</span> <span style="color: #cc66cc;">5</span><span style="color: #005a89;">&#125;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #007719;">nil</span> <span style="color: #cc66cc;">5</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #005a89;">&#40;</span>set-permissions resource <span style="color: #005a89;">&#123;</span><span style="color: #000099;">:read</span> t <span style="color: #000099;">:write</span> <span style="color: #007719;">nil</span> <span style="color: #000099;">:execute</span> t<span style="color: #005a89;">&#125;</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>This works, but it feels like a mild hack.  Also, Common Lisp&#8217;s definition of func is much easier to parse at a glance than Clojure&#8217;s is.</p>
<p>A couple additional notes:</p>
<ul>
<li>The Clojure functions <code>atom</code> and <code>ref</code> (at least) <em>do</em> take a couple keyword arguments, but this is done through a manual parse of the arg list instead of by way of built-in language support.</li>
<li><code>clojure-contrib</code> includes a macro <code>defnk</code> which wraps <code>defn</code> to allow keywords.  It&#8217;s nice to have, but only direct uses of <code>defnk</code> can benefit &#8212; that is, general <code>fn</code> or <code>defn</code> forms or the macros which wrap them are necessarily left out.</li>
</ul>
<p><a name="4"></a></p>
<h3>4. Commas are whitespace</h3>
<p>I think the intent here is to appeal to programmers of popular languages, where invariably commas are used to separate function arguments.  So you can do any of the things below, and maybe you prefer one way to the other:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">defn</span> func1 <span style="color: #005a89;">&#91;</span>foo, bar, baz<span style="color: #005a89;">&#93;</span>
  ...<span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">defn</span> func2 <span style="color: #005a89;">&#91;</span>foo bar baz<span style="color: #005a89;">&#93;</span>
  ...<span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #005a89;">&#40;</span>func1 <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #005a89;">&#41;</span>
<span style="color: #005a89;">&#40;</span>func2 <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>I will admit it may be easier to quickly scan maps having commas, especially when both keys and values are keywords:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #005a89;">&#123;</span>:color <span style="color: #000099;">:blue</span> :input :keyboard :os :linux<span style="color: #005a89;">&#125;</span>
<span style="color: #005a89;">&#123;</span>:color <span style="color: #000099;">:red</span>, :input :mouse, :os :windows<span style="color: #005a89;">&#125;</span></pre></div></div>

<p>But speaking generally, commas-as-whitespace only adds line width and noise.  And worse, it causes a break in convention from Lisp&#8217;s backquote facility:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; Scheme / Common Lisp</span>
`<span style="color: #005a89;">&#40;</span>foo bar ,baz<span style="color: #005a89;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; Clojure</span>
`<span style="color: #005a89;">&#40;</span>foo bar ~baz<span style="color: #005a89;">&#41;</span></pre></div></div>

<p>Commas are easier to scan for in backquoted lists than tildes are, as they hang lower than most other glyphs.  Also, they&#8217;re a more sensible counterpart to backquotes.  I do approve of moving away from tired Lisp conventions, but sometimes different isn&#8217;t better.</p>
<p><a name="5"></a></p>
<h3>5. Syntax for type specification is ugly</h3>
<p>To add type hints to variables, Clojure uses a shortcut version of the metadata reader macro:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">let</span> <span style="color: #005a89;">&#91;</span>#^Integer a x
      #^Integer b y<span style="color: #005a89;">&#93;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span> a b<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>It looks gross, though I admit I have no better alternative to offer.  Common Lisp does an equally bad job here, probably worse, with its <code>DECLARE</code> placement restrictions and <code>THE</code> verbosity.  Static languages like Java and C will probably always be able to specify types more clearly.</p>
<p>Type coercion is cleaner:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">let</span> <span style="color: #005a89;">&#91;</span>a <span style="color: #005a89;">&#40;</span><span style="color: #007719;">int</span> <span style="color: #cc66cc;">2</span><span style="color: #005a89;">&#41;</span>
      b <span style="color: #005a89;">&#40;</span><span style="color: #007719;">int</span> <span style="color: #cc66cc;">3</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#93;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span> a b<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>But it means something different, even though it&#8217;s similar in this context.  Also, only certain types can be coerced.</p>
<p>To programmers reading this to discover reasons to chicken out learning Clojure: it would be only on rare occasions that you&#8217;d want to insert type hints in Clojure code anyway.</p>
<p><a name="6"></a></p>
<h3>6. All the good names are taken</h3>
<p>Lisp-1 vs. Lisp-2 is an old debate, but here are a few observations all the same.  Like Scheme, Clojure is a Lisp-1, which means that functions and variables share the same namespace.  Names of functions in Clojure are often kept short to allow for compact code.  So, these words and many more are &#8220;reserved&#8221;, as they name core functions:</p>
<ul>
<li><code>map</code></li>
<li><code>vec</code></li>
<li><code>fn</code></li>
<li><code>seq</code></li>
<li><code>set</code></li>
<li><code>str</code></li>
<li><code>count</code></li>
<li><code>key</code></li>
<li><code>val</code></li>
</ul>
<p>The drawback is that these would also make <em>great</em> variable names when vagueness is a virtue, but since they&#8217;re already used, programmers must use names like <code>a-fn</code>, <code>a-map</code>, <code>the-vec</code>, etc. instead.  (You could still name variables e.g. <code>map</code> or <code>str</code>, but if you do, you shadow those functions and confuse people who read your code.)  By itself this would just be a little awkward, but as the API does often use parameter names like <code>coll</code> &#8212; since there is no function named <code>coll</code> &#8212; it&#8217;s also inconsistent.</p>
<p>If Clojure were a Lisp-2, you could <a href="http://xach.livejournal.com/220305.html">and would</a> go ahead and use those names as variables without shadowing the functions.  I can understand why many people dislike Lisp-2-ness.  I felt the same way before I learned Common Lisp.  Now I prefer it, and I&#8217;m certain it&#8217;s something anyone can get used to.</p>
<p>Lisp-2 is little harder to bend your head around.  Which of these two calls is right?</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">funcall</span> fn arg1 arg2<span style="color: #005a89;">&#41;</span>
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">funcall</span> #'fn arg1 arg2<span style="color: #005a89;">&#41;</span></pre></div></div>

<p>Answer: both, but they mean different things.  In the first call, fn is a variable which holds a function as its value.  In the second call, fn had previously been declared as a function, and <code>#'</code> is basically a lookup for the name &#8216;fn&#8217; in the function namespace.  (That&#8217;s not quite what is going on, but it&#8217;s an easy way to think about it.)  So, the first form calls the function stored in fn and the second form calls the function which is actually named &#8216;fn&#8217;.</p>
<p><a name="7"></a></p>
<h3>7. Conclusion</h3>
<p>Clojure is becoming observably more popular.  It&#8217;s a bridge allowing both Lisp and Java to move forward, and it&#8217;s well-positioned for the coming age of widespread parallelization.  If these snags are some of the worst things to find in Clojure, that&#8217;s more a complement than a condemnation.</p>
]]></content:encoded>
			<wfw:commentRss>http://items.sjbach.com/567/critiquing-clojure/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Extensibility in Vim and Emacs</title>
		<link>http://items.sjbach.com/560/extensibility-in-vim-and-emacs</link>
		<comments>http://items.sjbach.com/560/extensibility-in-vim-and-emacs#comments</comments>
		<pubDate>Tue, 07 Apr 2009 14:04:01 +0000</pubDate>
		<dc:creator>Stephen Bach</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://items.sjbach.com/?p=560</guid>
		<description><![CDATA[
Table of contents
1. Development resources
2. Provisions for extension
3. Portability requirements
4. Conclusion

Emacs and Vim both provide facility for extension.  But as they represent divergent philosophies &#8212; Vim following the &#8220;small is beautiful&#8221; and &#8220;do one thing well&#8221; precepts of Unix, Emacs coming from a belief that the editor is an operational hub &#8212; they have [...]]]></description>
			<content:encoded><![CDATA[<ul class="toc">
<li class="title">Table of contents</li>
<li><a href="#1">1. Development resources</a></li>
<li><a href="#2">2. Provisions for extension</a></li>
<li><a href="#3">3. Portability requirements</a></li>
<li><a href="#4">4. Conclusion</a></li>
</ul>
<p>Emacs and Vim both provide facility for extension.  But as they represent divergent philosophies &#8212; Vim following the &#8220;small is beautiful&#8221; and &#8220;do one thing well&#8221; <a href="http://en.wikipedia.org/wiki/Unix_philosophy">precepts of Unix</a>, Emacs coming from a belief that the editor is an operational hub &#8212; they have different objectives here.</p>
<p>This is a comparison article with a focus on plugin development.  The following articles offer more general comparisons of the editors, and are worth reading:</p>
<ul>
<li><a href="http://pinard.progiciels-bpi.ca/opinions/editors.html">Thoughts on editors (Emacs in particular) by François Pinard</a></li>
<li><a href="http://gnuvince.wordpress.com/2007/02/24/emacs-and-vim">Emacs and Vim by Vincent Foley</a></li>
<li><a href="http://www.io.com/~dierdorf/emacsvi.html">Emacs and Vi by John Dierdorf</a></li>
</ul>
<p>I&#8217;ve written previously on <a href="http://items.sjbach.com/97/writing-a-vim-plugin">writing Vim plugins</a> and <a href="http://items.sjbach.com/544/surveying-emacs-lisp">using Emacs Lisp</a>.</p>
<p><a name="1"></a></p>
<h3>1. Development resources</h3>
<p>For me, programming has never been a sit-down-and-go activity.  It&#8217;s more like traditional writing: I think about what it is I want to say, do a little fact checking, and then find a way to say it.  Programming is perhaps easier than writing, though, because the mechanism and the medium share the same space, so your tools can meet you half way.</p>
<div class="subsection">
<h4>i. Integrated help</h4>
<p>The <code>:help</code> command in <span class="vim">Vim</span> is not just for new users; it&#8217;s also a great boon for experienced users.  Each section and subsection of Vim&#8217;s extensive documentation includes one or more descriptive keywords.  These appear in the <code>:help</code> command&#8217;s completion list to make it easier to find what you are looking for, even when you don&#8217;t know exactly what it is.</p>
<p>Imagine we wish to know the syntax for ignoring character case in a regex.  Typing &#8220;:help ignore&lt;TAB&gt;&#8221; shows:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">:help ignore
/ignorecase          filetype-ignore      +wildignore
'ignorecase'         g:netrw_ignorenetrc  'eventignore'
ignore-errors        'foldignore'         'noignorecase'
efm-ignore           'wildignore'</pre></div></div>

<p>Trial-and-error will prove <code>/ignorecase</code> as the help section we want.  &#8220;:help case&lt;TAB&gt;&#8221; shows a similar list.</p>
<p>The equivalent in <span class="emacs">Emacs</span> is probably <code>C-h d</code>, <code>apropos-documentation</code>.  It&#8217;s actually a little handier than <code>:help</code>: because the convention in Emacs Lisp is to supply <a href="http://en.wikipedia.org/wiki/Docstring">docstrings</a> for API symbols, <code>apropos-documentation</code> searches a larger space having a higher chance of relevancy.</p>
<p>Relatedly, there are <code>C-h f</code>, <code>describe-function</code>, and <code>C-h v</code>, <code>describe-variable</code>, which present full API documentation and can even jump to the definition of a given symbol.  Really convenient.</p>
<h4>ii. Reference material</h4>
<p>When programming, no matter your skill or experience, it&#8217;s imperative to have some kind of reference material.  Documentation is okay, but existing code is the real blessing.  Here is a place where <span class="emacs">Emacs</span> shines.  The basic core of the editor is written in C (opaquely), but the rest of its functionality is written in Emacs Lisp and is immediately available for viewing.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #9966CC;">dpkg</span> <span style="color: #660033;">-L</span> <span style="color: #008000;">emacs-snapshot-el</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #9966CC;">grep</span> <span style="color: #ff0000;">'\.el\.gz$'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #9966CC;">wc</span> <span style="color: #660033;">-l</span>
<span style="color: #000000;">1125</span>
$ <span style="color: #9966CC;">gunzip</span> <span style="color: #660033;">--stdout</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #9966CC;">dpkg</span> <span style="color: #660033;">-L</span> <span style="color: #008000;">emacs-snapshot-el</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #9966CC;">grep</span> <span style="color: #ff0000;">'\.el\.gz$'</span><span style="color: #000000; font-weight: bold;">`</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #9966CC;">wc</span> <span style="color: #660033;">-l</span>
<span style="color: #000000;">1243201</span></pre></div></div>

<p>Over one million lines-of-code in 1125 files &#8212; a treasure trove.</p>
<p>
This is harder to measure in <span class="vim">Vim</span>.  Its base package includes about 1000 <code>.vim</code> files, but the vast majority are filetype or indentation specifiers, or colour themes, none of which are generally of use to a programmer.  There is an unofficial <code>vim-scripts</code> package in Debian-based distributions, though it too skews toward themes.  Here are the numbers anyway:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #9966CC;">dpkg</span> <span style="color: #660033;">-L</span> <span style="color: #008000;">vim-scripts</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #9966CC;">grep</span> <span style="color: #ff0000;">'\.vim$'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #9966CC;">wc</span> <span style="color: #660033;">-l</span>
<span style="color: #000000;">172</span>
$ <span style="color: #9966CC;">cat</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #9966CC;">dpkg</span> <span style="color: #660033;">-L</span> <span style="color: #008000;">vim-scripts</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #9966CC;">grep</span> <span style="color: #ff0000;">'\.vim$'</span><span style="color: #000000; font-weight: bold;">`</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #9966CC;">wc</span> <span style="color: #660033;">-l</span>
<span style="color: #000000;">47972</span></pre></div></div>

</div>
<p><a name="2"></a></p>
<h3>2. Provisions for extension</h3>
<p>As an environment for editor extensions, Emacs can be fairly called full-featured; Vim can&#8217;t.  (I&#8217;m in a good position to make this statement having written sizable plugins for both editors.)</p>
<p>
Rather than write a feature-by-feature comparison, I&#8217;ll just list some things Emacs natively supports which Vim does not, or does only poorly:</p>
<div class="subsection">
<h4>i. Key capture/filename entry</h4>
<ul>
<li>As a convenience for input, <span class="emacs">Emacs</span> provides simple minibuffer functions such as <code>read-file-name</code> and <code>read-buffer</code> as well as more general <code>read-from-minibuffer</code> and <code>read-string</code>, which offer a lot of customization.  Capturing key presses is made easy with <code>read-key-sequence</code>; to respond to general user action, watch functions can be added to hook variables such as <code>post-command-hook</code>.</li>
<li><span class="vim">Vim</span> offers <code>input()</code> for using the command-line in a script, but it is limited.  <a href="http://vim.wikia.com/wiki/Capture_all_keys">Key capture is very difficult</a>; <code>getchar()</code> exists, but is incomplete.  Inexplicably, there is no <code>CharacterPress</code> <a href="http://www.vim.org/htmldoc/autocmd.html"><code>autocmd</code> event</a> (or anything similar).  The only certain way to capture keys is to remap <em>all</em> of them to call a user function, then later restore all previous mappings.  A bad hack.</li>
</ul>
<h4>ii. Specialized buffers</h4>
<ul>
<li>Creating a new temporary buffer or a special-purpose buffer in <span class="emacs">Emacs</span> can be done using <code>with-temp-buffer</code> and <code>generate-new-buffer</code>, or several other calls.</li>
<li><span class="vim">Vim</span> provides no standard means to create a special buffer; the documentation recommends to create a new buffer and to set these options:
<p>
<div class="wp_syntax"><div class="code"><pre class="vim2" style="font-family:monospace;">:<span style="color: #007719;">setlocal</span> <span style="color: #005a89;">buftype</span>=nofile
:<span style="color: #007719;">setlocal</span> <span style="color: #005a89;">bufhidden</span>=hide
:<span style="color: #007719;">setlocal</span> <span style="color: #005a89;">noswapfile</span></pre></div></div>

</p>
<p>  The problem is, this new buffer will also inherit many user settings, such as <code>wrap</code>, <code>number</code>, <code>foldcolumn</code>, <code>cursorline</code>, <code>spell</code>, and <code>sidescroll</code>.  There is no way &#8212; that I know of &#8212; to create a fresh, blank buffer.  For correctness, all of these settings should be enumerated and explicitly turned off upon buffer creation.  To make this even less satisfactory, not all buffer settings can be set per-buffer.</li>
</ul>
<h4>iii. Programmatic window cycling/traversal</h4>
<p><span class="emacs">Emacs</span> has many useful functions:</p>
<ul>
<li><code>walk-windows</code> &#8212; equivalent to mapping <code>(window-list)</code> through a given function.</li>
<li><code>window-tree</code> &#8212; returns a tree representing the window layout in the given frame.</li>
<li><code>save-window-excursion</code> &#8212; screw around with the current layout temporarily, without repercussion.</li>
<li>Many more.  And as a bonus, the minibuffer can be manipulated with standard window functions.</li>
</ul>
<p>Similar functions in <span class="vim">Vim</span>:</p>
<ul>
<li>Like <code>walk-windows</code>: there is <code>:windo</code>, but it only works on commands, not functions, and gives up completely on any minor error.</li>
<li>The closest analogue to <code>window-tree</code> may be <code>winnr()</code>, a multi-purpose function.  It returns the following, depending on context:
<ol>
<li>The number of the current window (to be used as an argument in other functions)</li>
<li>The count of open windows</li>
<li>The number of the last accessed window</li>
</ol>
</li>
<li>Like <code>save-window-excursion</code>: <code>winrestcmd()</code> generates a sequence of storable window commands that can be called to restore the current window configuration; but it doesn&#8217;t always work.  There are also <code>winsaveview()</code> and <code>winrestview()</code>, but they also don&#8217;t always work.</li>
</ul>
</ul>
<h4>iv. Buffer ordering</h4>
<p>When enumerating buffers for some purpose, the most suitable ordering is often <a href="http://en.wikipedia.org/wiki/Most_Recently_Used">most-recently used (MRU)</a>.  This is how <span class="emacs">Emacs</span> acts by default with e.g. <code>buffer-list</code>.  <span class="vim">Vim</span> seems to order buffers by most recently opened, or maybe it&#8217;s more arbitrary.  MRU is possible in Vim, but it must be hacked in manually by use of <code>autocmds</code>, watching these events:</p>
<ul>
<li><code>BufEnter</code></li>
<li><code>BufDelete</code></li>
<li><code>BufWipeout</code></li>
</ul>
<h4>v. Environment variable interpretation</h4>
<ul>
<li><span class="emacs">Emacs</span>: <code>getenv</code>/<code>setenv</code>, <code>process-environment</code>, various helper functions.</li>
<li><span class="vim">Vim</span>:
<p>
<div class="wp_syntax"><div class="code"><pre class="vim2" style="font-family:monospace;">:<span style="color: #007719;">let</span> $VAR = value  <span style="color: #808080; font-style: italic;">&quot; set</span>
:<span style="color: #007719;">let</span> var = $VAR    <span style="color: #808080; font-style: italic;">&quot; get</span></pre></div></div>

</p>
<p>  Sometimes works when setting options:</p>

<div class="wp_syntax"><div class="code"><pre class="vim2" style="font-family:monospace;">:<span style="color: #007719;">set</span> <span style="color: #005a89;">term</span>=$TERM    <span style="color: #808080; font-style: italic;">&quot; works</span>
:<span style="color: #007719;">set</span> <span style="color: #005a89;">history</span>=$NUM  <span style="color: #808080; font-style: italic;">&quot; does not work</span></pre></div></div>

</li>
</ul>
<h4>vi. Text deletion</h4>
<p>A minor problem (or convenience) of <span class="vim">Vim</span> is that every scripted delete action will clobber the unnamed register and numbered registers that we use for quick cut+pastes.  (<span class="emacs">Emacs</span> folks: this could be like losing your kill ring every time you run <code>find-file</code> or <code>dired</code>.)  I struggled with this for a long time before learning of <a href="http://stackoverflow.com/questions/54255/in-vim-is-there-a-way-to-delete-without-putting-text-in-the-register">Vim&#8217;s blackhole register</a>.  In general, the behaviour of Vim&#8217;s delete registers is intricate and unlikeable.</p>
<h4>vii. Completion</h4>
<ul>
<li><span class="emacs">Emacs</span> has heavy-duty completion support, from simple, high-level functions such as <code>read-buffer</code> and <code>read-variable</code> to accept values at the minibuffer, to <code>try-completion</code> and <code>all-completions</code> which can be used programmatically without action from a user.</li>
<li><span class="vim">Vim</span> command definitions can be specified with a special argument to include completion support at the ex command line.  It&#8217;s a little awkward, though.  Copy/paste the following block and then type &#8220;:Finger &lt;TAB&gt;&#8221; in Vim for a demonstration:
<p>
<div class="wp_syntax"><div class="code"><pre class="vim2" style="font-family:monospace;"><span style="color: #007719;">command</span> -<span style="color: #005a89;">complete</span>=custom,ListUsers -<span style="color: #005a89;">nargs</span>=<span style="color: #cc66cc;">1</span> Finger <span style="color: #ff0000;">!</span>finger <span style="color: #ff0000;">&lt;args&gt;</span>
<span style="color: #007719;">fun</span> ListUsers<span style="color: #005a89;">&#40;</span>A,L,P<span style="color: #005a89;">&#41;</span>
    <span style="color: #007719;">return</span> <span style="color: #005a89;">system</span><span style="color: #005a89;">&#40;</span><span style="color: #808080; font-style: italic;">&quot;cut -d: -f1 /etc/ passwd&quot;)</span>
<span style="color: #007719;">endfun</span></pre></div></div>

</p>
<p>See <a href="http://www.vim.org/htmldoc/map.html#:command-completion"><code>:help :command-completion</code></a> for an explanation.  I don&#8217;t think Vim has built-in support for programmatic completion.</li>
</ul>
</div>
<p>Speaking broadly: Emacs feels engineered, while Vim gives the impression of having grown piecemeal.  As a platform it is awkward and missing some useful bits.  This can be liberating, as one need not worry about duplicating something already part of the API, but it&#8217;s also limiting.</p>
<p><a href="http://items.sjbach.com/97/writing-a-vim-plugin#alternative">Vim offers bindings into other languages</a>, and you may choose to eschew Vim Script and mitigate some of these issues.  But a side effect of doing so is that you&#8217;ll need to account for differences in string syntax when directly interfacing with the editor.  Check out these Ruby-to-Vim special character escaping functions:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC;">def</span> <span style="color:#007719;">vim_single_quote_escape</span><span style="color:#006600; font-weight:bold;">&#40;</span>s<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#808080; font-style:italic;"># Everything in a Vim single-quoted string is literal, except single</span>
  <span style="color:#808080; font-style:italic;"># quotes.  Single quotes are escaped by doubling them.</span>
  s.<span style="color:#CC0066;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;'&quot;</span>, <span style="color:#996600;">&quot;''&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC;">end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC;">def</span> <span style="color:#007719;">vim_filename_escape</span><span style="color:#006600; font-weight:bold;">&#40;</span>s<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#808080; font-style:italic;"># Escape slashes, open square brackets, spaces, and double quotes</span>
  <span style="color:#808080; font-style:italic;"># using backslashes.</span>
  s.<span style="color:#CC0066;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#91;</span>\<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'&quot; <span style="color:#000099;">\\</span>]/, '</span>\\\\\<span style="color:#006666;">0</span><span style="color:#996600;">')
end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC;">def</span> <span style="color:#007719;">vim_regex_escape</span><span style="color:#006600; font-weight:bold;">&#40;</span>s<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#808080; font-style:italic;"># Escape lots of stuff.</span>
    s.<span style="color:#CC0066;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#91;</span>\<span style="color:#006600; font-weight:bold;">&#93;</span>\<span style="color:#006600; font-weight:bold;">&#91;</span>.~<span style="color:#996600;">&quot;^$<span style="color:#000099;">\\</span>*]/,'<span style="color:#000099;">\\</span><span style="color:#000099;">\\</span><span style="color:#000099;">\0</span>')
end</span></pre></div></div>

<p>It took me a few releases to get these right.  Admittedly, this glue is the cost of doing business with an external language.</p>
<p><a name="3"></a></p>
<h3>3. Portability requirements</h3>
<p>(To be clear: &#8220;portability&#8221; here refers to accounting for differences between versions of the same editor, whether running on the same operating system or not.)</p>
<p>
When developing for <span class="vim">Vim</span>, it&#8217;s unlikely that portability will be an issue; Vim Script has changed little in the last few years.  Also, since upgrading Vim is painless, users will do so, and therefore it&#8217;s common for a plugin writer to target only the most recent major release.  There&#8217;s even <a href="http://vimdoc.sourceforge.net/htmldoc/pi_getscript.html">an integrated feature to automatically update plugins</a> since Vim 7.1, which suggests faith there won&#8217;t be breaking changes in the future.</p>
<p>
As discussed in a previous article, <a href="http://items.sjbach.com/544/surveying-emacs-lisp">portability must be a greater concern to developers of <span class="emacs">Emacs</span> packages</a>.  It&#8217;s probably a good idea to choose a portability target during development instead of trying to hack it in after the fact.</p>
<p><a name="4"></a></p>
<h3>4. Conclusion</h3>
<p>I&#8217;d feel uncomfortable to mark this as a win for Emacs or a loss for Vim, even given all the evidence.  The basic philosophies of the two editors are distinct enough to make the comparison unfair.  To have a video editor integrated into Vim would be neither funny nor useful, while for Emacs it&#8217;s perhaps both.</p>
<p>
But I do think it&#8217;s fair to say that Vim Script is an ugly language, a DSL which has been stretched to the breaking point.  It&#8217;s an intellectual dead-end.  Emacs Lisp feels more like a true programming language and has much the elegance of any other Lisp.  And to learn it pays dividends, as it overlaps in respectable portion with Common Lisp.</p>
]]></content:encoded>
			<wfw:commentRss>http://items.sjbach.com/560/extensibility-in-vim-and-emacs/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Surveying Emacs Lisp</title>
		<link>http://items.sjbach.com/544/surveying-emacs-lisp</link>
		<comments>http://items.sjbach.com/544/surveying-emacs-lisp#comments</comments>
		<pubDate>Tue, 31 Mar 2009 13:55:49 +0000</pubDate>
		<dc:creator>Stephen Bach</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://items.sjbach.com/?p=544</guid>
		<description><![CDATA[
Table of contents
1. Overview
2. Portability considerations
3. A short case study
4. The Common Lisp compatibility package
5. Conclusion

Emacs is well known for being highly configurable.  In large part, this is due to its close tie to some form of Lisp over the majority of its long and varied history.  Within all extant implementations of the [...]]]></description>
			<content:encoded><![CDATA[<ul class="toc">
<li class="title">Table of contents</li>
<li><a href="#1">1. Overview</a></li>
<li><a href="#2">2. Portability considerations</a></li>
<li><a href="#3">3. A short case study</a></li>
<li><a href="#4">4. The Common Lisp compatibility package</a></li>
<li><a href="#5">5. Conclusion</a></li>
</ul>
<p>Emacs is well known for being highly configurable.  In large part, this is due to its close tie to some form of Lisp over the majority of its <a href="http://en.wikipedia.org/wiki/Emacs#History">long and varied history</a>.  Within all extant implementations of the editor, the Lisp is Emacs Lisp, a language which is hard to love but easy to like.</p>
<p><a name="1"></a></p>
<h3>1. Overview</h3>
<p>Emacs Lisp (inconsistently abbreviated to Elisp) is descended from MacLisp and looks a lot like Common Lisp.</p>

<div class="wp_syntax"><div class="code"><pre class="emacslisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">defun</span> fibonacci <span style="color: #005a89;">&#40;</span>n<span style="color: #005a89;">&#41;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">if</span> <span style="color: #005a89;">&#40;</span><span style="color: #66cc66;">&lt;</span> n <span style="color: #cc66cc;">2</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #cc66cc;">1</span>
    <span style="color: #005a89;">&#40;</span><span style="color: #007719;">+</span> <span style="color: #005a89;">&#40;</span>fibonacci <span style="color: #005a89;">&#40;</span>- n <span style="color: #cc66cc;">1</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
       <span style="color: #005a89;">&#40;</span>fibonacci <span style="color: #005a89;">&#40;</span>- n <span style="color: #cc66cc;">2</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>Being a Lisp, it is inherently extensible, and so is well-suited as an embedded language.  Actually, a case can be made that Emacs Lisp is more powerful than many general-purpose programming languages.  Some of its notable features:</p>
<ul>
<li>lambdas and first-class functions</li>
<li>macros</li>
<li>byte-code compilation</li>
<li>an interactive shell/REPL (through the <code>*scratch*</code> buffer)</li>
<li>an integrated debugger and profiler</li>
</ul>
<p>Considering its complete isolation to a sub-platform, Emacs Lisp is very successful.  Tools that have been written in it include several IRC clients, a popular mail/news reader, two different terminal emulators, a web browser, and even a video editor.  Here are several excellent programming resources I&#8217;ve found helpful:</p>
<ul>
<li><a href="http://www.gnu.org/software/emacs/manual/elisp.html">The GNU Emacs Lisp Reference Manual</a> (there are separate &#8212; and comprehensive &#8212; manuals for the editor and its language)</li>
<li><a href="http://steve-yegge.blogspot.com/2008/01/emergency-elisp.html">Emergency Elisp by Steve Yegge</a>, a primer</li>
<li><a href="http://www.nongnu.org/emacs-tiny-tools/elisp-coding/">Jari Aalto&#8217;s Tiny Tools documentation</a>, which is not just about Tiny Tools</li>
<li><a href="http://www.emacswiki.org/cgi-bin/emacs-en?CategoryCode">The Code category of EmacsWiki</a></li>
<li><a href="http://groups.google.com/group/comp.emacs/topics">comp.emacs</a> and <a href="http://groups.google.com/group/comp.emacs.xemacs/topics">comp.emacs.xemacs</a></li>
</ul>
<p><a name="2"></a></p>
<h3>2. Portability considerations</h3>
<p>Emacs users tend to customize their editor to a great extent, so upgrading to a new version can be a dog, and therefore extension writers are quite conservative about portability.</p>
<p>
And alas, the situation is doubly complicated.  Developers must consider not only compatibility between versions (e.g. 21 to 22), but also compatibility between variants, as GNU Emacs and XEmacs are both popular.<a name="note1_start"></a><sup><a href="#note1_end">[1]</a></sup>  My informal survey of <a href="http://www.itasoftware.com">ITA Software</a>, an Emacs-heavy development house, seems to show that engineers stick with what they know; those who picked up Emacs during the periods GNU Emacs lagged behind XEmacs continue to use XEmacs, while most of the others use GNU Emacs.  It&#8217;s split pretty evenly.</p>
<p>
It&#8217;d be useful to identify the actual usage breakdown among the different versions of the editor, but these numbers don&#8217;t exist;  <a href="http://popcon.debian.org">Debian Popularity Contest</a> can at least provide an estimate:</p>
<div align="center">
<table>
<tr>
<th colspan="3">Tracked usage of Emacs packages in Debian</th>
</tr>
<tr>
<th>Package</th>
<th>Installs</th>
<th>Notes</th>
</tr>
<tr>
<td>emacsen-common</td>
<td>15890</td>
<td>(required package for all Emacs installs)</td>
</tr>
<tr>
<td>emacs21</td>
<td>7522</td>
<td>GNU</td>
</tr>
<tr>
<td>emacs22</td>
<td>4738</td>
<td>GNU</td>
</tr>
<tr>
<td>xemacs21</td>
<td>2211</td>
<td></td>
</tr>
<tr>
<td>emacs-snapshot</td>
<td>427</td>
<td>GNU (at the time of writing, v23.0)</td>
</tr>
</table>
</div>
<p>Likewise, <a href="http://popcon.ubuntu.com">Ubuntu Popularity Contest</a>:</p>
<div align="center">
<table>
<tr>
<th colspan="3">Tracked usage of Emacs packages in Ubuntu</th>
</tr>
<tr>
<th>Package</th>
<th>Installs</th>
<th>Notes</th>
</tr>
<tr>
<td>emacsen-common</td>
<td>86805</td>
<td>(required package for all Emacs installs)</td>
</tr>
<tr>
<td>emacs21</td>
<td>32358</td>
<td>GNU</td>
</tr>
<tr>
<td>emacs22</td>
<td>16640</td>
<td>GNU</td>
</tr>
<tr>
<td>xemacs21</td>
<td>7394</td>
<td></td>
</tr>
<tr>
<td>emacs-snapshot</td>
<td>4764</td>
<td>GNU (at the time of writing, v23.0)</td>
</tr>
</table>
</div>
<p>Again, at best these tables should be considered datapoints in a larger census, but they do seem to show GNU Emacs as significantly more popular than XEmacs, and 21 as the widest-used GNU Emacs version.  (Can anyone provide additional stats?)</p>
<p>
<a name="note1_end"></a><sup><a href="#note1_start">[1]</a></sup> Other variants such as Aquamacs may deserve a mention here as well &#8212; I have no information about their install bases.</p>
<p><a name="3"></a></p>
<h3>3. A short case study</h3>
<p>Two years ago I wrote a <a href="http://www.vim.org/scripts/script.php?script_id=1890">filesystem explorer / buffer switcher plugin for Vim</a> and then one year ago <a href="http://www.emacswiki.org/emacs/LustyExplorer">brought it to Emacs</a>.  It had been written in Ruby, which has rough language parity with Emacs Lisp.  (No macros vs. no closures.)</p>
<div class="subsection">
<h4>i. Initial development</h4>
<p>Since Emacs Lisp is not really object-oriented, I dropped the original formal inheritance model.  Also, I removed several classes outright, as existing functionality in Emacs &#8212; stuff which didn&#8217;t exist in Vim &#8212; made them redundant.</p>
<p>A minimal port was finished in a few hours and 200 lines-of-code.  The current version sits at <a href="http://github.com/sjbach/lusty/blob/b25f54d2120fecae6dbcb5a16f7e8194cea4638a/lusty-explorer.el">494 lines</a>.  For comparison, the Vim plugin (on which development continues in parallel) is <a href="http://github.com/sjbach/lusty/blob/b25f54d2120fecae6dbcb5a16f7e8194cea4638a/lusty-explorer.vim">1466 lines</a>.</p>
<p>From the Vim plugin, here is an inefficient Ruby function to convert an array of strings into <code>column_count</code> columns (reading downward, i.e. the way <code>ls</code> works):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC;">def</span> <span style="color:#007719;">columnize</span><span style="color:#006600; font-weight:bold;">&#40;</span>strings, column_count<span style="color:#006600; font-weight:bold;">&#41;</span>
  rows = <span style="color:#006600; font-weight:bold;">&#40;</span>strings.<span style="color:#9900CC;">length</span> <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#CC0066;">Float</span><span style="color:#006600; font-weight:bold;">&#40;</span>column_count<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">ceil</span>
&nbsp;
  <span style="color:#808080; font-style:italic;"># Break the array into sub arrays representing columns</span>
  cols = strings.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>array, e<span style="color:#006600; font-weight:bold;">|</span>
           <span style="color:#9966CC;">if</span> array.<span style="color:#9900CC;">last</span>.<span style="color:#9900CC;">size</span> <span style="color:#006600; font-weight:bold;">&lt;</span> rows
             array.<span style="color:#9900CC;">last</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> e
           <span style="color:#9966CC;">else</span>
             array <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span>e<span style="color:#006600; font-weight:bold;">&#93;</span>
           <span style="color:#9966CC;">end</span>
           array
         <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
  <span style="color:#0000FF;">return</span> cols
<span style="color:#9966CC;">end</span></pre></div></div>

<p>And below, the close equivalent in Emacs Lisp.  It uses <code>push</code>+<code>nreverse</code> instead of append (<code>&lt;&lt;</code>) above, which obfuscates the algorithm a little:</p>

<div class="wp_syntax"><div class="code"><pre class="emacslisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">defun</span> <span style="color: #9966cc;">columnize</span> <span style="color: #005a89;">&#40;</span>strings column-count<span style="color: #005a89;">&#41;</span>
  <span style="color: #ff0000;">&quot;Break the list STRINGS into sublists representing columns.&quot;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">let</span> <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span>nrows <span style="color: #005a89;">&#40;</span><span style="color: #007719;">ceiling</span> <span style="color: #005a89;">&#40;</span><span style="color: #66cc66;">/</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">length</span> strings<span style="color: #005a89;">&#41;</span>
                           <span style="color: #005a89;">&#40;</span><span style="color: #007719;">float</span> column-count<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
    <span style="color: #005a89;">&#40;</span><span style="color: #007719;">nreverse</span>
     <span style="color: #005a89;">&#40;</span><span style="color: #007719;">mapcar</span> '<span style="color: #007719;">nreverse</span>
             <span style="color: #005a89;">&#40;</span><span style="color: #007719;">reduce</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">lambda</span> <span style="color: #005a89;">&#40;</span>lst e<span style="color: #005a89;">&#41;</span>
                       <span style="color: #005a89;">&#40;</span><span style="color: #007719;">if</span> <span style="color: #005a89;">&#40;</span><span style="color: #66cc66;">&lt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">length</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">car</span> lst<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
                              nrows<span style="color: #005a89;">&#41;</span>
                           <span style="color: #005a89;">&#40;</span><span style="color: #007719;">push</span> e <span style="color: #005a89;">&#40;</span><span style="color: #007719;">car</span> lst<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
                         <span style="color: #005a89;">&#40;</span><span style="color: #007719;">push</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span> e<span style="color: #005a89;">&#41;</span> lst<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
                       lst<span style="color: #005a89;">&#41;</span>
                     strings
                     <span style="color: #000099;">:initial-value</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>There is room for improvement in both cases &#8212; 10 pride points to the person who codes up the best rewrite of either function. <img src='http://items.sjbach.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>So far I&#8217;ve had to invest much less development time on the Emacs extension, though in fairness it has benefited from having an original to crib from.  Also, it&#8217;s currently less featureful than the Vim plugin.</p>
<h4>ii. Backporting</h4>
<p>I had targeted GNU Emacs 23, since that&#8217;s the version I use.  The port to GNU Emacs 22 was trivial.  A port to GNU Emacs 21 is difficult for the lack of several convenience functions, so it&#8217;s on the back burner.</p>
<p>
I spent about an hour on a tricky port to XEmacs before deciding that for two reasons, I couldn&#8217;t justify the effort:</p>
<ol>
<li>Ugly portability wrappers would complicate the otherwise compact code.  A concise assert becomes an awkward block:<a name="note2_start"></a><sup><a href="#note2_end">[2]</a></sup>

<div class="wp_syntax"><div class="code"><pre class="emacslisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; Before</span>
<span style="color: #005a89;">&#40;</span><span style="color: #ff2222;">assert</span> <span style="color: #005a89;">&#40;</span><span style="color: #9966cc;">minibufferp</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; After</span>
<span style="color: #005a89;">&#40;</span><span style="color: #ff2222;">assert</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">if</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">boundp</span> 'xemacsp<span style="color: #005a89;">&#41;</span>
            <span style="color: #005a89;">&#40;</span><span style="color: #007719;">eq</span> <span style="color: #005a89;">&#40;</span><span style="color: #9966cc;">window-buffer</span> <span style="color: #005a89;">&#40;</span><span style="color: #9966cc;">minibuffer-window</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
                <span style="color: #005a89;">&#40;</span><span style="color: #9966cc;">current-buffer</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
          <span style="color: #005a89;">&#40;</span><span style="color: #9966cc;">minibufferp</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span></pre></div></div>

</li>
<li>This package was written for my use, and I haven&#8217;t yet had reason to use XEmacs.</li>
</ol>
<p>Perhaps if I had targeted GNU Emacs 21 as my baseline, portability wouldn&#8217;t have been as much of an issue; the library couldn&#8217;t have been as easy to write, however, and I believe side-projects especially should optimize for development time.
</p></div>
<p><a name="note2_end"></a><sup><a href="#note2_start">[2]</a></sup> A colleague &#8212; and also a more capable Emacs user &#8212; suggests to isolate portability wrappers as a partial solution.  This is good practice in general.  So, in perhaps a separate file:</p>

<div class="wp_syntax"><div class="code"><pre class="emacslisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">when</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">not</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">boundp</span> '<span style="color: #9966cc;">minibufferp</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">defun</span> <span style="color: #9966cc;">minibufferp</span> <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#41;</span>
    <span style="color: #005a89;">&#40;</span><span style="color: #007719;">eq</span> <span style="color: #005a89;">&#40;</span><span style="color: #9966cc;">window-buffer</span> <span style="color: #005a89;">&#40;</span><span style="color: #9966cc;">minibuffer-window</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
        <span style="color: #005a89;">&#40;</span><span style="color: #9966cc;">current-buffer</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">provide</span> 'lusty-xemacs<span style="color: #005a89;">&#41;</span></pre></div></div>

<p>Then, within the main code:</p>

<div class="wp_syntax"><div class="code"><pre class="emacslisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">when</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">boundp</span> 'xemacsp<span style="color: #005a89;">&#41;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">require</span> 'lusty-xemacs<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; ...</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; Clean again!</span>
<span style="color: #005a89;">&#40;</span><span style="color: #ff2222;">assert</span> <span style="color: #005a89;">&#40;</span><span style="color: #9966cc;">minibufferp</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>This is likely what I&#8217;ll do when I re-examine the XEmacs port.</p>
<p><a name="4"></a></p>
<h3>4. The Common Lisp compatibility package</h3>
<p>The comprehensive <a href="http://www.gnu.org/software/emacs/manual/html_node/cl/"><code>cl</code> package</a> provides many great extensions to Emacs Lisp which make the language more approachable, especially to Common Lisp programmers.  Some of its nifty additions:</p>
<ul>
<li><code>defun*</code>, <code>defmacro*</code> (adding implicit blocks and keyword arguments)</li>
<li><code>flet</code>, <code>labels</code>, <code>macrolet</code></li>
<li><code>loop</code>, <code>do</code></li>
<li><code>lexical-let</code></li>
<li><code>multiple-value-bind</code>, <code>destructuring-bind</code></li>
<li><code>case</code>, <code>ecase</code>, <code>typecase</code></li>
<li><code>setf</code>, <code>incf</code>/<code>decf</code>, <code>define-modify-macro</code></li>
<li><code>push</code>, <code>pushnew</code>, <code>pop</code></li>
</ul>
<p>Pragmatically, XEmacs loads this package by default.  GNU Emacs, however, lightly discourages its use.  <a href="http://www.gnu.org/software/emacs/manual/html_node/cl/Overview.html#Overview">From the reference manual</a>:</p>
<blockquote><p>
&#8230; we have a policy that packages installed in Emacs must not load <code>cl</code> at run time. &#8230; If you are writing packages that you plan to distribute and invite widespread use for, you might want to observe the same rule.
</p></blockquote>
<p><a href="http://www.gnu.org/software/emacs/elisp/html_node/Coding-Conventions.html#Coding-Conventions">And also</a>:</p>
<blockquote><p>
Please don&#8217;t require the <code>cl</code> package of Common Lisp extensions at runtime. Use of this package is optional, and is not part of the standard Emacs namespace.
</p></blockquote>
<p>An imperfect workaround using <code>eval-when-compile</code> is advocated instead.  I suggest to disregard the undertone of this note; Emacs Lisp benefits considerably from the inclusion of <code>cl</code>, and it&#8217;s time to move forward.  In his article about Ejacs, <a href="http://steve-yegge.blogspot.com/2008/11/ejacs-javascript-interpreter-for-emacs.html">Emacs expert Steve Yegge likewise recommends unrestrained use of <code>cl</code></a>.</p>
<p><a name="5"></a></p>
<h3>5. Conclusion</h3>
<p>Under the all-important light of getting things done, Emacs Lisp is not a bad language.  Emacs users are fortunate, and they should be thankful &#8212; users of other editors are not so lucky.</p>
<p>
Thanks to Ron Gut and Chris Burke for their comments and suggestions.</p>
]]></content:encoded>
			<wfw:commentRss>http://items.sjbach.com/544/surveying-emacs-lisp/feed</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Clojure&#8217;s new regex syntax</title>
		<link>http://items.sjbach.com/406/clojures-new-regex-syntax</link>
		<comments>http://items.sjbach.com/406/clojures-new-regex-syntax#comments</comments>
		<pubDate>Wed, 19 Nov 2008 15:00:20 +0000</pubDate>
		<dc:creator>Stephen Bach</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://items.sjbach.com/?p=406</guid>
		<description><![CDATA[
Table of contents
1. What has changed
2. Clojure vs foo
3. The real reason this is neat

Last week, Rich Hickey announced a few notable changes to Clojure, including ahead-of-time compilation and a cleaner syntax for regular expressions.  Both are improvements, but the syntax is especially interesting for a reason unrelated to its function.  First, a [...]]]></description>
			<content:encoded><![CDATA[<ul class="toc">
<li class="title">Table of contents</li>
<li><a href="#1">1. What has changed</a></li>
<li><a href="#2">2. Clojure vs foo</a></li>
<li><a href="#3">3. The real reason this is neat</a></li>
</ul>
<p>Last week, <a href="http://groups.google.com/group/clojure/msg/58e3f8e5dfb876c9">Rich Hickey announced a few notable changes to Clojure</a>, including <a href="http://en.wikipedia.org/wiki/AOT_compiler">ahead-of-time compilation</a> and a cleaner syntax for regular expressions.  Both are improvements, but the syntax is especially interesting for a reason unrelated to its function.  First, a quick overview.</p>
<p><a name="1"></a></p>
<h3>1. What has changed</h3>
<p>In a sentence, fewer backslashes.  The notation is now more in line with that of scripting languages, where regular expressions are first-class literals, than that of general-purpose languages like C++ or Java, where regexes are just specialized strings.</p>
<p>Say we are given a stream including this text:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">...
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span>  <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;images/11/apple1.gif&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span>   <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;images/2/bulb2.jpeg&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;images/354/citrus32_a.png&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
...</pre></div></div>

<p>We want to select <code>IMG</code> tags and capture the basename (without extension) of each source file.  This can be done in many ways; here&#8217;s a blueprint which is just barely good enough:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&lt;img [whitespace]+
     src=&quot; [word-char]+ / [digit]+ / ([word-char]+) ...</pre></div></div>

<p>Converting this to Clojure&#8217;s old syntax gives us a somewhat unwieldly <code>#"&lt;img\\s+src=\"\\w+/\\d+/(\\w+)"</code>.  A quick test:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">let</span> <span style="color: #005a89;">&#91;</span>lines <span style="color: #ff0000;">&quot;...
             &lt;img  src=<span style="color: #000099; font-weight: bold;">\&quot;</span>images/11/apple1.gif<span style="color: #000099; font-weight: bold;">\&quot;</span>/&gt;
             &lt;img   src=<span style="color: #000099; font-weight: bold;">\&quot;</span>images/2/bulb2.jpeg<span style="color: #000099; font-weight: bold;">\&quot;</span>/&gt;
             &lt;img src=<span style="color: #000099; font-weight: bold;">\&quot;</span>images/354/citrus32_a.png<span style="color: #000099; font-weight: bold;">\&quot;</span>/&gt;
             ...&quot;</span><span style="color: #005a89;">&#93;</span>
  <span style="color: #808080; font-style: italic;">;; Return only the captures, not the full matches.</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">map</span> <span style="color: #007719;">second</span>
       <span style="color: #005a89;">&#40;</span><span style="color: #007719;">re-seq</span> #<span style="color: #ff0000;">&quot;&lt;img<span style="color: #000099; font-weight: bold;">\\</span>s+src=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\\</span>w+/<span style="color: #000099; font-weight: bold;">\\</span>d+/(<span style="color: #000099; font-weight: bold;">\\</span>w+)&quot;</span> lines<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">=&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #ff0000;">&quot;apple1&quot;</span> <span style="color: #ff0000;">&quot;bulb2&quot;</span> <span style="color: #ff0000;">&quot;citrus32_a&quot;</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>The new update to the reader allows us to remove the double escaping of the regex specials in the literal:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">map</span> <span style="color: #007719;">second</span>
     <span style="color: #005a89;">&#40;</span><span style="color: #007719;">re-seq</span> #<span style="color: #ff0000;">&quot;&lt;img<span style="color: #000099; font-weight: bold;">\s</span>+src=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\w</span>+/<span style="color: #000099; font-weight: bold;">\d</span>+/(<span style="color: #000099; font-weight: bold;">\w</span>+)&quot;</span> lines<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p><a name="2"></a></p>
<h3>2. Clojure vs foo</h3>
<p>Since we&#8217;re on the topic, here&#8217;s how Clojure&#8217;s syntax compares to popular languages.</p>
<div class="subsection">
<h5>Ruby and Perl 5</h5>
<p>
<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;"># Regular usage
/&lt;img\s+src=&quot;\w+\/\d+\/(\w+)/
&nbsp;
# Choosing a different delimiter:
 m|&lt;img\s+src=&quot;\w+/\d+/(\w+)|     # Perl
%r|&lt;img\s+src=&quot;\w+/\d+/(\w+)|     # Ruby</pre></div></div>

</p>
<p>The clearest of all extant languages (at least in this regard), Ruby and Perl can avoid some extra escaping by changing the delimiter character from <code>/</code> to <code>|</code>.</p>
<h5>Emacs Lisp</h5>
<p>
<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&quot;&lt;img\\s-+src=\&quot;\\w+/[[:digit:]]+/\\(\\w+\\)&quot;</pre></div></div>

</p>
<p>Well, the expression is long and ugly.  An upside is that because of the quote delimiters, forward-slashes need not be escaped.</p>
<h5>Java</h5>
<p>
<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&quot;&lt;img\\s+src=\&quot;\\w+/\\d+/(\\w+)&quot;</pre></div></div>

</p>
<p>This is the same as Clojure&#8217;s original syntax.  For reference, Clojure and Java share a regex engine and are equivalent in power.</p>
<h5>Common Lisp</h5>
<p>
<a href="http://www.weitz.de/cl-ppcre/">Edi Weitz&#8217;s professional CL-PPCRE package</a> is essentially the standard for dealing with regular expressions in CL.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&quot;&lt;img\\s+src=\&quot;\\w+/\\d+/(\\w+)&quot;</pre></div></div>

</p>
<p>Also, <a href="http://www.weitz.de/cl-interpol/">Edi&#8217;s CL-INTERPOL</a> provides a reader macro which simplifies regex literals to the level of Perl&#8217;s:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">#?r|&lt;img\s+src=&quot;\w+/\d+/(\w+)|</pre></div></div>

<p>Finally, the <a href="http://letoverlambda.com/index.cl/guest/chap4.html">reader macro mastery</a> of <a href="http://letoverlambda.com/">Doug Hoyte&#8217;s Let Over Lambda</a> gives a method of making clear, functional literals:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">;; This is a callable lambda:
;; #~m|&lt;img\s+src=&quot;\w+/\d+/(\w+)|
&nbsp;
'#~m|&lt;img\s+src=&quot;\w+/\d+/(\w+)|
&nbsp;
=&gt; (LAMBDA (#:STR236)
     (CL-PPCRE:SCAN &quot;&lt;img\\s+src=\&quot;\\w+/\\d+/(\\w+)&quot;
                    #:STR236))</pre></div></div>

</div>
<p><a name="3"></a></p>
<h3>3. The real reason this is neat</h3>
<p>The modification was <a href="http://groups.google.com/group/clojure/msg/b96e533bd0b37759">proposed by Chris Houser</a> (with a simple patch) on October 7, politely debated until October 10, and committed to Clojure in <a href="http://www.bitbucket.org/shoover/clojure-mirror/changeset/281683a3402f/">r1070 on October 15</a>.*  This syntax was better, and the discussion skipped <em>if</em> it should be applied, directly to <em>how</em>.</p>
<p>Turnaround time for a breaking change: one week.  You have to respect that velocity.</p>
<p>
There is a feeling in the development community that Clojure has a good chance of becoming an important language.  Now is the brief time when any interested programmer could contribute something significant, in an environment which recognizes intelligent contribution and lacks &#8212; for the moment &#8212; politics and tradition.</p>
<p>
Want to help?  The <a href="http://groups.google.com/group/clojure">Clojure mailing list</a> is high signal-to-noise, and subscribing is a good way to get acclimated.  Also, communicating realtime with Rich Hickey and other Clojure experts is no more difficult than joining an IRC channel: <a href="irc://irc.freenode.net/%23clojure">#clojure on freenode</a>.</p>
<hr />
<p>* There was also a similar discussion in March, but it didn&#8217;t include a patch.</p>
]]></content:encoded>
			<wfw:commentRss>http://items.sjbach.com/406/clojures-new-regex-syntax/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Configuring Vim right</title>
		<link>http://items.sjbach.com/319/configuring-vim-right</link>
		<comments>http://items.sjbach.com/319/configuring-vim-right#comments</comments>
		<pubDate>Fri, 07 Nov 2008 03:13:18 +0000</pubDate>
		<dc:creator>Stephen Bach</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://items.sjbach.com/?p=319</guid>
		<description><![CDATA[
Table of contents
1. Essential .vimrc configuration items
2. Recommended .vimrc configuration items

I have spent a lot of time looking at a Vim window, and correspondingly, a lot of time testing different configurations.  These are the best non-standard options I&#8217;ve found or stolen from others over the years; listed below in order of descending usefulness &#8212; [...]]]></description>
			<content:encoded><![CDATA[<ul class="toc">
<li class="title">Table of contents</li>
<li><a href="#1">1. Essential <code>.vimrc</code> configuration items</a></li>
<li><a href="#2">2. Recommended <code>.vimrc</code> configuration items</a></li>
</ul>
<p>I have spent a lot of time looking at a Vim window, and correspondingly, a lot of time testing different configurations.  These are the best non-standard options I&#8217;ve found or stolen from others over the years; listed below in order of descending usefulness &#8212; though I think everything in this article is worth skimming &#8212; are tips which should have value to anyone, no matter how they like to run Vim. That is, there is minimal editorializing.</p>
<p>Note: no plugins are covered here, just vanilla Vim.</p>
<p><a name="1"></a></p>
<h3>Essential <code>.vimrc</code> configuration items</h3>
<p>For whatever reason, the following options aren&#8217;t set by default, but they should be.</p>
<ol>
<li class="vimlist">Turn on <code>hidden</code><br />
<span></p>
<p>
Don&#8217;t worry about the name.  What this does is allow Vim to manage multiple buffers effectively.
</p>
<ul type="disc">
<li>The current buffer can be put to the background without writing to disk;</li>
<li>When a background buffer becomes current again, marks and undo-history are remembered.</li>
</ul>
<p>Turn this on.</p>

<div class="wp_syntax"><div class="code"><pre class="vim2" style="font-family:monospace;"><span style="color: #007719;">set</span> <span style="color: #005a89;">hidden</span></pre></div></div>

<p></span>
</li>
<li class="vimlist">Remap <code>`</code> to <code>'</code><br />
<span></p>
<p>
These are very similar keys.  Typing <code>'a</code> will jump to the line in the current file marked with <code>ma</code>.  However, <code>`a</code> will jump to the line <i>and column</i> marked with <code>ma</code>.</p>
<p>It&#8217;s more useful in any case I can imagine, but it&#8217;s located way off in the corner of the keyboard.  The best way to handle this is just to swap them:
</p>

<div class="wp_syntax"><div class="code"><pre class="vim2" style="font-family:monospace;"><span style="color: #007719;">nnoremap</span> ' `
<span style="color: #007719;">nnoremap</span> ` '</pre></div></div>

<p></span>
</li>
<li class="vimlist">Map <code>leader</code> to <code>,</code><br />
<span></p>
<p>
The <code>leader</code> character is your own personal modifier key, as <code>g</code> is Vim&#8217;s modifier key (when compared to <code>vi</code>).  The default <code>leader</code> is <code>\</code>, but this isn&#8217;t located standardly on all keyboards and requires a pinky stretch in any case.
</p>

<div class="wp_syntax"><div class="code"><pre class="vim2" style="font-family:monospace;"><span style="color: #007719;">let</span> mapleader = <span style="color: #808080; font-style: italic;">&quot;,&quot;</span></pre></div></div>

<p><code>&lt;SPACE&gt;</code> is also a good choice.  Note: you can of course have several &#8220;personal modifier keys&#8221; simply by mapping a sequence, but the <code>leader</code> key is handled more formally.<br />
</span>
</li>
<li class="vimlist">Keep a longer history<br />
<span></p>
<p>
By default, Vim only remembers the last 20 commands and search patterns entered.  It&#8217;s nice to boost this up:
</p>

<div class="wp_syntax"><div class="code"><pre class="vim2" style="font-family:monospace;"><span style="color: #007719;">set</span> <span style="color: #005a89;">history</span>=<span style="color: #cc66cc;">1000</span></pre></div></div>

<p></span>
</li>
<li class="vimlist">Enable extended <code>%</code> matching<br />
<span></p>
<p>
The <code>%</code> key will switch between opening and closing brackets.  By sourcing <code>matchit.vim</code>, it can also switch among e.g. <code>if/elsif/else/end</code>, between opening and closing XML tags, and more.
</p>

<div class="wp_syntax"><div class="code"><pre class="vim2" style="font-family:monospace;"><span style="color: #007719;">runtime</span> macros/matchit.vim</pre></div></div>

<p>Note: <code>runtime</code> is the same as <code>source</code> except that the path is relative to the Vim installation directory.<br />
</span>
</li>
<li class="vimlist">Make file/command completion useful<br />
<span></p>
<p>
By default, pressing <code>&lt;TAB&gt;</code> in command mode will choose the <em>first</em> possible completion with no indication of how many others there might be.  The following configuration lets you see what your other options are:
</p>

<div class="wp_syntax"><div class="code"><pre class="vim2" style="font-family:monospace;"><span style="color: #007719;">set</span> <span style="color: #005a89;">wildmenu</span></pre></div></div>

<p>To have the completion behave similarly to a shell, i.e. complete only up to the point of ambiguity (while still showing you what your options are), also add the following:</p>

<div class="wp_syntax"><div class="code"><pre class="vim2" style="font-family:monospace;"><span style="color: #007719;">set</span> <span style="color: #005a89;">wildmode</span>=list:longest</pre></div></div>

</li>
<p></span>
</ol>
<p><a name="2"></a></p>
<h3>Recommended <code>.vimrc</code> configuration items</h3>
<p>Most people like these.</p>
<ol>
<li class="vimlist">Use case-smart searching<br />
<span></p>
<p>
These two options, when set together, will make <code>/</code>-style searches case-sensitive only if there is a capital letter in the search expression.  <code>*</code>-style searches continue to be consistently case-sensitive.
</p>

<div class="wp_syntax"><div class="code"><pre class="vim2" style="font-family:monospace;"><span style="color: #007719;">set</span> <span style="color: #005a89;">ignorecase</span> 
<span style="color: #007719;">set</span> <span style="color: #005a89;">smartcase</span></pre></div></div>

<p>This is usually the most useful combination.<br />
</span>
</li>
<li class="vimlist">Set the terminal title<br />
<span></p>
<p>
A running <code>gvim</code> will always have a window title, but when <code>vim</code> is run within an xterm, by default it inherits the terminal&#8217;s current title.
</p>

<div class="wp_syntax"><div class="code"><pre class="vim2" style="font-family:monospace;"><span style="color: #007719;">set</span> <span style="color: #005a89;">title</span></pre></div></div>

<p>This gives e.g. <code>| page.html (~) - VIM |</code>.<br />
</span>
</li>
<li class="vimlist">Maintain more context around the cursor<br />
<span></p>
<p>
When the cursor is moved outside the viewport of the current window, the buffer is scrolled by a single line.  Setting the option below will start the scrolling three lines before the border, keeping more context around where you&#8217;re working.
</p>

<div class="wp_syntax"><div class="code"><pre class="vim2" style="font-family:monospace;"><span style="color: #007719;">set</span> <span style="color: #005a89;">scrolloff</span>=<span style="color: #cc66cc;">3</span></pre></div></div>

<p>Typing <code>zz</code> is also handy; it centers the window on the cursor without moving the cursor.  (But watch out for <code>ZZ</code>!)<br />
</span>
</li>
<li class="vimlist">Store temporary files in a central spot<br />
<span></p>
<p>
Swap files and backups are annoying but can save you a lot of trouble.  Rather than spread them all around your filesystem, isolate them to a single directory:
</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #9966CC;">mkdir</span> ~<span style="color: #000000; font-weight: bold;">/</span>.vim-tmp  <span style="color: #666666; font-style: italic;"># or whatever</span></pre></div></div>

<p>And in <code>.vimrc</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="vim2" style="font-family:monospace;"><span style="color: #007719;">set</span> <span style="color: #005a89;">backupdir</span>=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp
<span style="color: #007719;">set</span> <span style="color: #005a89;">directory</span>=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp</pre></div></div>

<p>This is especially valuable after an unexpected reboot &#8212; you don&#8217;t have to track down all the leftover temp files.  However: if you are editing files on a shared file system, it&#8217;ll be easier to clobber concurrent modifications, as other users&#8217; Vim processes won&#8217;t see your swaps.<br />
</span>
</li>
<li class="vimlist">Scroll the viewport faster<br />
<span></p>
<p>
<code>&lt;C-e&gt;</code> and <code>&lt;C-y&gt;</code> scroll the viewport a single line.  I like to speed this up:
</p>

<div class="wp_syntax"><div class="code"><pre class="vim2" style="font-family:monospace;"><span style="color: #007719;">nnoremap</span> <span style="color: #ff0000;">&lt;C-e&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #ff0000;">&lt;C-e&gt;</span>
<span style="color: #007719;">nnoremap</span> <span style="color: #ff0000;">&lt;C-y&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #ff0000;">&lt;C-y&gt;</span></pre></div></div>

<p></span>
</li>
<li class="vimlist">Enable limited line numbering<br />
<span></p>
<p>
It&#8217;s often useful to know where you are in a buffer, but full line numbering is distracting.  Setting the option below is a good compromise:</p>
<p>
<div class="wp_syntax"><div class="code"><pre class="vim2" style="font-family:monospace;"><span style="color: #007719;">set</span> <span style="color: #005a89;">ruler</span></pre></div></div>

<p>Now in the bottom right corner of the status line there will be something like: <code>529, 35    68%</code>, representing line 529, column 35, about 68% of the way to the end.<br />
</span>
</li>
<li class="vimlist">A bunch of stuff your OS should already do<br />
<span></p>
<p>
If you are running Windows or OS X or a sloppy Linux distribution, you may not be using these:
</p>

<div class="wp_syntax"><div class="code"><pre class="vim2" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&quot; Intuitive backspacing in insert mode</span>
<span style="color: #007719;">set</span> <span style="color: #005a89;">backspace</span>=indent,eol,start
&nbsp;
<span style="color: #808080; font-style: italic;">&quot; File-type highlighting and configuration.</span>
<span style="color: #808080; font-style: italic;">&quot; Run :filetype (without args) to see what you may have</span>
<span style="color: #808080; font-style: italic;">&quot; to turn on yourself, or just set them all to be sure.</span>
<span style="color: #007719;">syntax</span> on
<span style="color: #007719;">filetype</span> on
<span style="color: #007719;">filetype</span> plugin on
<span style="color: #007719;">filetype</span> indent on
&nbsp;
<span style="color: #808080; font-style: italic;">&quot; Highlight search terms...</span>
<span style="color: #007719;">set</span> <span style="color: #005a89;">hlsearch</span>
<span style="color: #007719;">set</span> <span style="color: #005a89;">incsearch</span> <span style="color: #808080; font-style: italic;">&quot; ...dynamically as they are typed.</span></pre></div></div>

<p>The <code>filetype</code> lines enable type-specific configuration, such as knowledge of syntax and indentation.  E.g. foo.c will be opened with Vim&#8217;s pre-configured C settings, and bar.py will be opened with Python settings.</p>
<p>If the search term highlighting gets annoying, set a key to switch it off temporarily:</p>

<div class="wp_syntax"><div class="code"><pre class="vim2" style="font-family:monospace;"><span style="color: #007719;">nmap</span> <span style="color: #ff0000;">&lt;silent&gt;</span> <span style="color: #ff0000;">&lt;leader&gt;</span>n :silent :nohlsearch<span style="color: #ff0000;">&lt;CR&gt;</span></pre></div></div>

<p></span>
</li>
<li class="vimlist">Catch trailing whitespace<br />
<span></p>
<p>
The following will make tabs and trailing spaces visible when requested:
</p>

<div class="wp_syntax"><div class="code"><pre class="vim2" style="font-family:monospace;"><span style="color: #007719;">set</span> <span style="color: #005a89;">listchars</span>=tab:&gt;-,trail:·,eol:$
<span style="color: #007719;">nmap</span> <span style="color: #ff0000;">&lt;silent&gt;</span> <span style="color: #ff0000;">&lt;leader&gt;</span>s :<span style="color: #007719;">set</span> <span style="color: #005a89;">nolist</span><span style="color: #ff0000;">!&lt;CR&gt;</span></pre></div></div>

<p>By default whitespace will be hidden, but now it can be toggled with <code>,s</code>.<br />
</span>
</li>
<li class="vimlist">Stifle many interruptive prompts<br />
<span></p>
<p>
The &#8220;<code>Press ENTER or type command to continue</code>&#8221; prompt is jarring and usually unnecessary.  You can shorten command-line text and other info tokens with, e.g.:
</p>

<div class="wp_syntax"><div class="code"><pre class="vim2" style="font-family:monospace;"><span style="color: #007719;">set</span> <span style="color: #005a89;">shortmess</span>=atI</pre></div></div>

<p>See <code>:help shortmess</code> for the breakdown of what this changes.  You can also pare things down further if you like.<br />
</span>
</li>
<li class="vimlist">Stop distracting your co-workers<br />
<span></p>
<p>Vim is a little surly, beeping at you at every chance.  You can either find <a href="http://ubuntu-tutorials.com/2007/07/26/turning-off-the-system-hardware-beep-linux-tutorial/">a way to turn off the bell completely</a>, or more usefully, make the bell visual:
</p>

<div class="wp_syntax"><div class="code"><pre class="vim2" style="font-family:monospace;"><span style="color: #007719;">set</span> <span style="color: #005a89;">visualbell</span></pre></div></div>

<p>Instead of emitting an obnoxious noise, the window will flash very briefly.  This is similar to <code>screen</code>&#8217;s interpretation of the bell in its default configuration.<br />
</span>
</li>
</ol>
<p>Here is <a href="http://github.com/sjbach/env/tree/master/dotfiles/vimrc">my own <code>.vimrc</code></a>, which includes all these settings (and some more which are less generally useful).  A fairly good source for other configuration tips is the <a href="http://vim.wikia.com/wiki/Vim_Tips_Wiki">Vim Tips Wiki</a>.</p>
<p>Thanks to Adam Katz and Chris Gaal for their comments and suggestions.</p>
]]></content:encoded>
			<wfw:commentRss>http://items.sjbach.com/319/configuring-vim-right/feed</wfw:commentRss>
		<slash:comments>36</slash:comments>
		</item>
		<item>
		<title>Extending the ITERATE macro</title>
		<link>http://items.sjbach.com/280/extending-the-iterate-macro</link>
		<comments>http://items.sjbach.com/280/extending-the-iterate-macro#comments</comments>
		<pubDate>Sun, 26 Oct 2008 19:35:32 +0000</pubDate>
		<dc:creator>Stephen Bach</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://items.sjbach.com/?p=280</guid>
		<description><![CDATA[
Table of contents
1. Generators
2. Writing a new gathering clause
3. Writing a new driver
Appendix: Extending the LOOP macro

In a previous article I gave an overview of ITERATE but punted on showing its best feature, the ability to extend it to support new iteration constructs.  These can be separated into two groups:

Gatherers, like collecting or summing, [...]]]></description>
			<content:encoded><![CDATA[<ul class="toc">
<li class="title">Table of contents</li>
<li><a href="#1">1. Generators</a></li>
<li><a href="#2">2. Writing a new gathering clause</a></li>
<li><a href="#3">3. Writing a new driver</a></li>
<li><a href="#appendix">Appendix: Extending the LOOP macro</a></li>
</ul>
<p>In a previous article I gave <a href="http://items.sjbach.com/211/comparing-loop-and-iterate">an overview of ITERATE</a> but punted on showing its best feature, the ability to extend it to support new iteration constructs.  These can be separated into two groups:</p>
<ol>
<li>Gatherers, like <code>collecting</code> or <code>summing</code>, for accumulating or reducing an expression;</li>
<li>Drivers, introduced with <code>for</code> or <code>generate</code>, for changing a value in a pattern or looping over a data structure.</li>
</ol>
<p>The <code>generate</code> driver type didn&#8217;t appear in the article comparing LOOP and ITERATE (it&#8217;s wholly a feature of ITERATE), so here first is a short introduction.</p>
<p><a name="1"></a></p>
<h3>1. Generators</h3>
<p>A <code>generate</code> clause is like a lazy <code>for</code>; its named variable will only update its value when told with <code>NEXT</code>.  An example:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; First, the regular `for`:</span>
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">iter</span> <span style="color: #005a89;">&#40;</span>for el in '<span style="color: #005a89;">&#40;</span>a b c<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>for i from <span style="color: #cc66cc;">10</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span><span style="color: #007719;">when</span> <span style="color: #005a89;">&#40;</span>primep i<span style="color: #005a89;">&#41;</span>
        <span style="color: #005a89;">&#40;</span>collect <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span> i el<span style="color: #005a89;">&#41;</span> into primes<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>collect el into letters<span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>finally <span style="color: #005a89;">&#40;</span><span style="color: #007719;">return</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">values</span> letters
                               primes<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #005a89;">&#40;</span>A B C<span style="color: #005a89;">&#41;</span>
   <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">11</span> B<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; Now, using `generate` for el instead:</span>
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">iter</span> <span style="color: #005a89;">&#40;</span>generate el in '<span style="color: #005a89;">&#40;</span>a b c<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>for i from <span style="color: #cc66cc;">10</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span><span style="color: #007719;">when</span> <span style="color: #005a89;">&#40;</span>primep i<span style="color: #005a89;">&#41;</span>
        <span style="color: #005a89;">&#40;</span>collect <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span> i <span style="color: #005a89;">&#40;</span>next el<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span> into primes<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>collect el into letters<span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>finally <span style="color: #005a89;">&#40;</span><span style="color: #007719;">return</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">values</span> letters
                               primes<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">NIL</span> A A B B B B C C<span style="color: #005a89;">&#41;</span>
   <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">11</span> A<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">13</span> B<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">17</span> C<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>With these semantics, <code>generate</code> can appear anywhere <code>for</code> can appear:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span>generate i upfrom <span style="color: #cc66cc;">0</span><span style="color: #005a89;">&#41;</span>
<span style="color: #005a89;">&#40;</span>generate c in-string <span style="color: #ff0000;">&quot;black&quot;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #005a89;">&#40;</span>generate form in-file <span style="color: #ff0000;">&quot;sexp.lisp&quot;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #005a89;">&#40;</span>generate sym in-package :<span style="color: #007719;">ITERATE</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p><a name="2"></a></p>
<h3>2. Writing a new gathering clause</h3>
<p>In the previous article I wrote a quick <code>dividing</code> clause:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">defmacro</span> dividing <span style="color: #005a89;">&#40;</span>num &amp;keys <span style="color: #005a89;">&#40;</span>initial-value <span style="color: #cc66cc;">0</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
  `<span style="color: #005a89;">&#40;</span>reducing ,num by #'<span style="color: #66cc66;">/</span> initial-value ,initial-value<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">iter</span> <span style="color: #005a89;">&#40;</span>for i in '<span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">10</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">2</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>dividing i <span style="color: #000099;">:initial-value</span> <span style="color: #cc66cc;">100</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #cc66cc;">1</span></pre></div></div>

<p>This was easy, but it&#8217;s not fully idiomatic; <code>:initial-value</code> must be specified as a keyword, and I can&#8217;t collect the value into a variable without modifying the macro.  Updating the macro with this functionality makes it noticeably more complex:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">defmacro</span> dividing <span style="color: #005a89;">&#40;</span>num &amp;key <span style="color: #005a89;">&#40;</span>initial-value <span style="color: #cc66cc;">0</span><span style="color: #005a89;">&#41;</span> into<span style="color: #005a89;">&#41;</span>
  `<span style="color: #005a89;">&#40;</span>reducing ,num by #'<span style="color: #66cc66;">/</span>
             initial-value ,initial-value
             ,@<span style="color: #005a89;">&#40;</span><span style="color: #007719;">when</span> into
                 `<span style="color: #005a89;">&#40;</span>into ,into<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>We can solve both problems cleanly by using instead <a href="http://common-lisp.net/project/iterate/doc/Rolling-Your-Own.html#index-defmacro_002dclause-121">ITERATE&#8217;s <code>DEFMACRO-CLAUSE</code></a>:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">defmacro-clause</span> <span style="color: #005a89;">&#40;</span>DIVIDING expr
                  &amp;optional
                  INTO var
                  FROM start<span style="color: #005a89;">&#41;</span>
  <span style="color: #ff0000;">&quot;Divide into a variable&quot;</span>
  `<span style="color: #005a89;">&#40;</span>reducing ,expr by #'<span style="color: #66cc66;">/</span> into ,var initial-value ,start<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; Now this works:</span>
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">iter</span> <span style="color: #005a89;">&#40;</span>for i in '<span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">10</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">2</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>dividing i from <span style="color: #cc66cc;">100</span> into quotient<span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>multiplying i into product<span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>finally <span style="color: #005a89;">&#40;</span><span style="color: #007719;">return</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">values</span> product quotient<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #cc66cc;">100</span>
   <span style="color: #cc66cc;">1</span></pre></div></div>

<p><code>DEFMACRO-CLAUSE</code> takes care of setting up support for both keyword and regular symbol syntax, provides more informative error messages when the clause is misused, and adds the clause to <code>DISPLAY-ITERATE-CLAUSES</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">display-iterate-clauses</span> 'dividing<span style="color: #005a89;">&#41;</span>
DIVIDING &amp;OPTIONAL INTO FROM  Divide into a variable</pre></div></div>

<p>Lastly, to continue the LOOP idiom of naming clause actions in both their infinitival and present-participle form, we can use <code>DEFSYNONYM</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">defsynonym</span> divide dividing<span style="color: #005a89;">&#41;</span></pre></div></div>

<p><a name="3"></a></p>
<h3>3. Writing a new driver</h3>
<p>The easiest way to write new <code>for</code> clauses is with <a href="http://common-lisp.net/project/iterate/doc/Writing-Drivers.html#index-defmacro_002ddriver-122"><code>DEFMACRO-DRIVER</code></a>.  ITERATE offers <code>in</code>, <code>on</code>, <code>in-vector</code>, <code>in-string</code>, the more general <code>in-sequence</code>, <code>in-hashtable</code>, and more.  Say we want to write a driver to iterate over the leaves of a tree.  This is a little tricky since tree traversal is usually written recursively.  To start us off, here&#8217;s a function which will return all leaves:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">defun</span> collect-leaves <span style="color: #005a89;">&#40;</span>tree<span style="color: #005a89;">&#41;</span>
  <span style="color: #ff0000;">&quot;Return all leaf nodes in depth-first order.&quot;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">iter</span> <span style="color: #005a89;">&#40;</span>with stack <span style="color: #66cc66;">=</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span> tree<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
        <span style="color: #005a89;">&#40;</span>while stack<span style="color: #005a89;">&#41;</span>
        <span style="color: #005a89;">&#40;</span>for node <span style="color: #66cc66;">=</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">pop</span> stack<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
        <span style="color: #005a89;">&#40;</span><span style="color: #007719;">if</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">consp</span> node<span style="color: #005a89;">&#41;</span>
            <span style="color: #005a89;">&#40;</span><span style="color: #007719;">destructuring-bind</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">l</span> . r<span style="color: #005a89;">&#41;</span> node
              <span style="color: #005a89;">&#40;</span><span style="color: #007719;">unless</span> <span style="color: #005a89;">&#40;</span>endp r<span style="color: #005a89;">&#41;</span>
                <span style="color: #005a89;">&#40;</span><span style="color: #007719;">push</span> r stack<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
              <span style="color: #005a89;">&#40;</span><span style="color: #007719;">push</span> <span style="color: #007719;">l</span> stack<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
            <span style="color: #005a89;">&#40;</span>collect node<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #005a89;">&#40;</span>collect-leaves '<span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span>a b<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span>c<span style="color: #005a89;">&#41;</span> d<span style="color: #005a89;">&#41;</span> e <span style="color: #005a89;">&#40;</span>f <span style="color: #005a89;">&#40;</span>g <span style="color: #005a89;">&#40;</span>h<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span> i<span style="color: #005a89;">&#41;</span> j<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #005a89;">&#40;</span>A B C D E F G H I J<span style="color: #005a89;">&#41;</span></pre></div></div>

<p>We need only splice this into a macro, with some modifications:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">defmacro-driver</span> <span style="color: #005a89;">&#40;</span>FOR leaf IN-TREE tree<span style="color: #005a89;">&#41;</span>
  <span style="color: #ff0000;">&quot;Iterate over the leaves in a tree&quot;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">let</span> <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span>gtree <span style="color: #005a89;">&#40;</span><span style="color: #007719;">gensym</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
        <span style="color: #005a89;">&#40;</span>stack <span style="color: #005a89;">&#40;</span><span style="color: #007719;">gensym</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
        <span style="color: #005a89;">&#40;</span>kwd <span style="color: #005a89;">&#40;</span><span style="color: #007719;">if</span> generate 'generate 'for<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
    `<span style="color: #005a89;">&#40;</span><span style="color: #007719;">progn</span>
       <span style="color: #005a89;">&#40;</span>with ,gtree <span style="color: #66cc66;">=</span> ,tree<span style="color: #005a89;">&#41;</span>
       <span style="color: #005a89;">&#40;</span>with ,stack <span style="color: #66cc66;">=</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span> ,gtree<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
       <span style="color: #005a89;">&#40;</span>,kwd ,leaf next
             <span style="color: #005a89;">&#40;</span><span style="color: #007719;">let</span> <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span>next-leaf
                    <span style="color: #005a89;">&#40;</span><span style="color: #007719;">iter</span> <span style="color: #005a89;">&#40;</span>while ,stack<span style="color: #005a89;">&#41;</span>
                          <span style="color: #005a89;">&#40;</span>for node <span style="color: #66cc66;">=</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">pop</span> ,stack<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
                          <span style="color: #005a89;">&#40;</span><span style="color: #007719;">if</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">consp</span> node<span style="color: #005a89;">&#41;</span>
                              <span style="color: #005a89;">&#40;</span><span style="color: #007719;">destructuring-bind</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">l</span> . r<span style="color: #005a89;">&#41;</span>
                                  node
                                <span style="color: #005a89;">&#40;</span><span style="color: #007719;">unless</span> <span style="color: #005a89;">&#40;</span>endp r<span style="color: #005a89;">&#41;</span>
                                  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">push</span> r ,stack<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
                                <span style="color: #005a89;">&#40;</span><span style="color: #007719;">push</span> <span style="color: #007719;">l</span> ,stack<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
                              <span style="color: #005a89;">&#40;</span><span style="color: #007719;">return</span> node<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
               <span style="color: #005a89;">&#40;</span><span style="color: #007719;">or</span> next-leaf <span style="color: #005a89;">&#40;</span>terminate<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<ul>
<li>The <code>(if generate 'generate 'for)</code> bit makes <code>in-tree</code> compatible with both <code>generate</code> and <code>for</code>.
<li>The form introduced by the symbol <code>next</code> is the code that will run on each iteration of <code>for</code> or on occurrence of the <code>NEXT</code> operator with <code>generate</code>.
<li>Here we&#8217;ve slipped an ITERATE call inside the driver, effectively nesting ITERATE loops.  This wasn&#8217;t necessary; DO or LOOP could also have been used.</li>
<li><code>TERMINATE</code> is a special form in an ITERATE driver to indicate completion.</li>
</ul>
<p>Finally, bringing it all together:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">iter</span> <span style="color: #005a89;">&#40;</span>for leaf in-tree '<span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #005a89;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #005a89;">&#41;</span> <span style="color: #cc66cc;">8</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">4</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span> <span style="color: #cc66cc;">2</span><span style="color: #005a89;">&#41;</span> <span style="color: #cc66cc;">3</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>collecting leaf into leaves<span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>multiplying leaf into product<span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>dividing leaf into quotient initial-value <span style="color: #cc66cc;">2000</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>finally <span style="color: #005a89;">&#40;</span><span style="color: #007719;">return</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">values</span> leaves
                               product
                               quotient<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #005a89;">&#41;</span>
   <span style="color: #cc66cc;">11520</span>
   <span style="color: #cc66cc;">25</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">144</span></pre></div></div>

<p><a name="appendix"></a></p>
<h3>Appendix: Extending the LOOP macro</h3>
<p><a href="http://sbcl.sourceforge.net">SBCL</a> developer Richard Kreuter showed me an implementation-specific way to incorporate new syntax into LOOP.  The linked code adds a <code>for</code> clause for binding multiple value returns (functionality that exists in ITERATE, but not the standard LOOP macro).  This is meant less as a point of comparison than as proof that LOOP can indeed be extended.</p>
<p><a href="/loop-values-path.lisp">loop-values-path.lisp</a></p>
<p>Usage:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">loop</span> for dividend in '<span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">5</span><span style="color: #005a89;">&#41;</span>
      for <span style="color: #005a89;">&#40;</span>quotient remainder<span style="color: #005a89;">&#41;</span> being the <span style="color: #007719;">values</span> of
          <span style="color: #005a89;">&#40;</span><span style="color: #007719;">floor</span> dividend <span style="color: #cc66cc;">2</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #007719;">do</span> <span style="color: #005a89;">&#40;</span>format <span style="color: #007719;">t</span> <span style="color: #ff0000;">&quot;~D ~D~%&quot;</span> quotient remainder<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">1</span>
<span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">0</span>
<span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">1</span>
<span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">0</span>
<span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">1</span></pre></div></div>

<p>See also this file from <a href="http://clsql.b9.com/">CLSQL</a> which provides LOOP extensions for iterating over records and supports seven Common Lisp compilers:</p>
<p><a href="/loop-extension.lisp">loop-extension.lisp</a></p>
<p>Thanks to Sam Freilich for his comments and suggestions.</p>
]]></content:encoded>
			<wfw:commentRss>http://items.sjbach.com/280/extending-the-iterate-macro/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Comparing LOOP and ITERATE</title>
		<link>http://items.sjbach.com/211/comparing-loop-and-iterate</link>
		<comments>http://items.sjbach.com/211/comparing-loop-and-iterate#comments</comments>
		<pubDate>Mon, 20 Oct 2008 02:35:59 +0000</pubDate>
		<dc:creator>Stephen Bach</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://items.sjbach.com/?p=211</guid>
		<description><![CDATA[
Table of contents
1. Views on LOOP
2. The purpose of ITERATE
3. Comparing looping clauses
4. Documentation
5. Obtaining ITERATE
6. Conclusion

Looping is the most common non-trivial construct in imperative programming, so having a domain language for generating iteration code is unquestionably a good thing.  I&#8217;ll explore two options within Common Lisp:

LOOP is a standard macro with an expressive [...]]]></description>
			<content:encoded><![CDATA[<ul class="toc">
<li class="title">Table of contents</li>
<li><a href="#1">1. Views on LOOP</a></li>
<li><a href="#2">2. The purpose of ITERATE</a></li>
<li><a href="#3">3. Comparing looping clauses</a></li>
<li><a href="#4">4. Documentation</a></li>
<li><a href="#5">5. Obtaining ITERATE</a></li>
<li><a href="#6">6. Conclusion</a></li>
</ul>
<p>Looping is the most common non-trivial construct in imperative programming, so having a domain language for generating iteration code is unquestionably a good thing.  I&#8217;ll explore two options within Common Lisp:</p>
<ol>
<li><a href="http://www.lispworks.com/documentation/HyperSpec/Body/m_loop.htm">LOOP</a> is a standard macro with an expressive syntax and built-in support for several iterative patterns.  It&#8217;s lauded and criticized in equal portion.</li>
<li><a href="http://common-lisp.net/project/iterate/">ITERATE</a> is a second looping macro created outside of the standard as an answer to the problems and limitations of LOOP.</li>
</ol>
<p>Superficially, the differences between them are few:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">loop</span> for i from <span style="color: #cc66cc;">0</span>
      for el in '<span style="color: #005a89;">&#40;</span>a b c d e f<span style="color: #005a89;">&#41;</span>
      collect <span style="color: #005a89;">&#40;</span><span style="color: #007719;">cons</span> i el<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">0</span> . A<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">1</span> . B<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">2</span> . C<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">3</span> . D<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">4</span> . E<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">5</span> . F<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span> 
&nbsp;
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">iter</span> <span style="color: #005a89;">&#40;</span>for i from <span style="color: #cc66cc;">0</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>for el in '<span style="color: #005a89;">&#40;</span>a b c d e f<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>collect <span style="color: #005a89;">&#40;</span><span style="color: #007719;">cons</span> i el<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">0</span> . A<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">1</span> . B<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">2</span> . C<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">3</span> . D<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">4</span> . E<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">5</span> . F<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>The Common Lisp standard also includes the looping macros <a href="http://www.lispworks.com/documentation/HyperSpec/Body/m_do_do.htm">DO and DO*</a>.  They&#8217;re widely used but can be terse and obscure; in any case, I&#8217;m leaving them out of the comparison.</p>
<p><a name="1"></a></p>
<h3>1. Views on LOOP</h3>
<p>The following are the three most frequent criticisms of LOOP:</p>
<ol>
<li>
<p>It doesn&#8217;t look like Lisp.  The degenerate case is hash table iteration.  I need to look this up every time:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">loop</span> for key being the hash-keys in *some-table*
          using <span style="color: #005a89;">&#40;</span>hash-value val<span style="color: #005a89;">&#41;</span>
      collect <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span> key val<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>As a general compromise, some developers use keyword syntax for LOOP forms, though it&#8217;s not a definite improvement:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">loop</span> <span style="color: #000099;">:for</span> counter <span style="color: #000099;">:downfrom</span> <span style="color: #cc66cc;">20</span> <span style="color: #000099;">:downto</span> <span style="color: #cc66cc;">0</span> <span style="color: #000099;">:by</span> <span style="color: #cc66cc;">2</span>
      :<span style="color: #007719;">when</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">zerop</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">mod</span> counter <span style="color: #cc66cc;">3</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #000099;">:collect</span> counter<span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">18</span> <span style="color: #cc66cc;">12</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">0</span><span style="color: #005a89;">&#41;</span></pre></div></div>

</li>
<li>Editors auto-indent LOOP poorly.  I work for <a href="http://www.itasoftware.com">one of the preeminent employers of Lisp developers</a>, yet no one I know has an Emacs configuration which indents all corners of LOOP correctly.  (That is, for my definition of correct; there&#8217;s no standard.)</li>
<li>LOOP can behave unpredictably when <code>for</code> clauses interact in a certain way.  However, in my experience this behaviour only presents itself in examples contrived to show it.</li>
</ol>
<p>For these reasons and others, <a href="http://www.paulgraham.com">Paul Graham</a> doesn&#8217;t recommend LOOP, and <a href="http://gigamonkeys.com/book">Peter Seibel</a> remains neutral about it.  But it has its place.</p>
<p>See also these articles:</p>
<ul>
<li><a href="http://www.paulgraham.com/loop.html">Dan Weinreb discusses the tradeoffs in accepting LOOP into Common Lisp</a></li>
<li><a href="http://funcall.blogspot.com/2007/06/why-i-am-knee-jerk-anti-loopist.html">Joe Marshall&#8217;s &#8220;Why I am a knee-jerk anti-LOOPist&#8221;</a></li>
<li><a href="http://exploring-lisp.blogspot.com/2007/06/in-defense-of-loop.html">Geoff Wozniak&#8217;s &#8220;In defense of LOOP&#8221;</a>.</li>
</ul>
<p><a name="2"></a></p>
<h3>2. The purpose of ITERATE</h3>
<p><a href="http://common-lisp.net/project/iterate">The ITERATE website</a> makes two main claims:</p>
<ol>
<li>It&#8217;s extensible</li>
<li>It helps editors auto-indent by having a more Lisp-like syntax</li>
</ol>
<p>Both are true.  ITERATE is as much a mini-language as LOOP, but its clauses look like, and frequently are, regular Lisp forms.</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; A LOOP example</span>
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">loop</span> for i upto <span style="color: #cc66cc;">20</span>
      <span style="color: #007719;">if</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">oddp</span> i<span style="color: #005a89;">&#41;</span>
        collect i into odds
      else
        collect i into evens
      finally <span style="color: #005a89;">&#40;</span><span style="color: #007719;">return</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">values</span> evens odds<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span> 
<span style="color: #66cc66;">=&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">10</span> <span style="color: #cc66cc;">12</span> <span style="color: #cc66cc;">14</span> <span style="color: #cc66cc;">16</span> <span style="color: #cc66cc;">18</span> <span style="color: #cc66cc;">20</span><span style="color: #005a89;">&#41;</span>
   <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">11</span> <span style="color: #cc66cc;">13</span> <span style="color: #cc66cc;">15</span> <span style="color: #cc66cc;">17</span> <span style="color: #cc66cc;">19</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; The equivalent in ITERATE</span>
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">iter</span> <span style="color: #005a89;">&#40;</span>for i from <span style="color: #cc66cc;">0</span> to <span style="color: #cc66cc;">20</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span><span style="color: #007719;">if</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">oddp</span> i<span style="color: #005a89;">&#41;</span>
          <span style="color: #005a89;">&#40;</span>collect i into odds<span style="color: #005a89;">&#41;</span>
          <span style="color: #005a89;">&#40;</span>collect i into evens<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>finally <span style="color: #005a89;">&#40;</span><span style="color: #007719;">return</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">values</span> evens odds<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">10</span> <span style="color: #cc66cc;">12</span> <span style="color: #cc66cc;">14</span> <span style="color: #cc66cc;">16</span> <span style="color: #cc66cc;">18</span> <span style="color: #cc66cc;">20</span><span style="color: #005a89;">&#41;</span>
   <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">11</span> <span style="color: #cc66cc;">13</span> <span style="color: #cc66cc;">15</span> <span style="color: #cc66cc;">17</span> <span style="color: #cc66cc;">19</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>The <code>IF</code> above is not an ITERATE construct &#8212; it&#8217;s the normal Common Lisp operator.  The LOOP <code>if</code> clause will be too, but only after a couple layers of macroexpansion.</p>
<p>Another example, harder to reproduce in LOOP:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">macrolet</span> <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span>divisorp <span style="color: #005a89;">&#40;</span>n m<span style="color: #005a89;">&#41;</span>
             `<span style="color: #005a89;">&#40;</span><span style="color: #007719;">zerop</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">mod</span> ,n ,m<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">iter</span> <span style="color: #005a89;">&#40;</span>for i from <span style="color: #cc66cc;">0</span> to <span style="color: #cc66cc;">30</span><span style="color: #005a89;">&#41;</span>
        <span style="color: #005a89;">&#40;</span><span style="color: #007719;">cond</span> <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span>divisorp i <span style="color: #cc66cc;">2</span><span style="color: #005a89;">&#41;</span>
               <span style="color: #005a89;">&#40;</span>collect i into twos<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
              <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span>divisorp i <span style="color: #cc66cc;">3</span><span style="color: #005a89;">&#41;</span>
               <span style="color: #005a89;">&#40;</span>collect i into threes<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
              <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span>divisorp i <span style="color: #cc66cc;">5</span><span style="color: #005a89;">&#41;</span>
               <span style="color: #005a89;">&#40;</span>collect i into fives<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
        <span style="color: #005a89;">&#40;</span>finally <span style="color: #005a89;">&#40;</span><span style="color: #007719;">return</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">values</span> twos threes fives<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">10</span> <span style="color: #cc66cc;">12</span> <span style="color: #cc66cc;">14</span> <span style="color: #cc66cc;">16</span> <span style="color: #cc66cc;">18</span> <span style="color: #cc66cc;">20</span> <span style="color: #cc66cc;">22</span> <span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">26</span> <span style="color: #cc66cc;">28</span> <span style="color: #cc66cc;">30</span><span style="color: #005a89;">&#41;</span>
   <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">15</span> <span style="color: #cc66cc;">21</span> <span style="color: #cc66cc;">27</span><span style="color: #005a89;">&#41;</span>
   <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">25</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>It&#8217;s true that distinguishing an ITERATE form from a regular form is less straightforward than it is with LOOP, but in practice the distinction is rarely important.  A benefit of the intermixing is that ITERATE clauses such as <code>collect</code> may appear arbitrarily deep in the body, in contrast to LOOP where they must fall within the mini-language portion.</p>
<p>Also: for comparison with LOOP&#8217;s egregious hash table iteration syntax in (1), here is the cleaner equivalent in ITERATE:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">iter</span> <span style="color: #005a89;">&#40;</span>for <span style="color: #005a89;">&#40;</span>key val<span style="color: #005a89;">&#41;</span> in-hashtable *some-table*<span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>collecting <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span> key val<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>ITERATE&#8217;s extensibility is <a href="http://items.sjbach.com/280/extending-the-iterate-macro">discussed in-depth in a second article</a>.  Through some means, <a href="http://osdir.com/ml/lisp.cmucl.general/2004-01/msg00075.html">LOOP can apparently be extended</a>, but it&#8217;s not done often or easily.  <a href="http://sbcl.sourceforge.net/">SBCL</a>, at least, provides LOOP extension hooks, but using them ties you to that compiler.</p>
<p><a name="3"></a></p>
<h3>3. Comparing looping clauses</h3>
<p>In most cases, ITERATE is a superset of functionality.</p>
<div class="subsection">
<h5>*Accumulation*</h5>
<p>LOOP offers <code>collecting</code>, <code>nconcing</code>, and <code>appending</code>.  ITERATE has these and also <code>adjoining</code>, <code>unioning</code>, <code>nunioning</code>, and <code>accumulating</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">iter</span> <span style="color: #005a89;">&#40;</span>for el in '<span style="color: #005a89;">&#40;</span>a b c a d b<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>adjoining el<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #005a89;">&#40;</span>A B C D<span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">iter</span> <span style="color: #005a89;">&#40;</span>for lst in '<span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span>a b c<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span>d b a<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span>g d h<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>unioning lst<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #005a89;">&#40;</span>A B C D G H<span style="color: #005a89;">&#41;</span></pre></div></div>

<p><code>accumulating</code> is an accumulator builder.  Here&#8217;s how to implement <code>unioning</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">iter</span> <span style="color: #005a89;">&#40;</span>for lst in '<span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span>a b c<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span>d b a<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span>g d h<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>accumulating lst by #'<span style="color: #007719;">union</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #005a89;">&#40;</span>A B C G D H<span style="color: #005a89;">&#41;</span></pre></div></div>

<h5>*Reduction*</h5>
<p>LOOP has <code>summing</code>, <code>counting</code>, <code>maximizing</code>, and <code>minimizing</code>.  ITERATE also includes <code>multiplying</code> and <code>reducing</code>.  <code>reducing</code> is the reduction builder:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">iter</span> <span style="color: #005a89;">&#40;</span>with dividend <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>for divisor in '<span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">10</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">2</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>reducing divisor by #'<span style="color: #66cc66;">/</span> initial-value dividend<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #cc66cc;">1</span></pre></div></div>

<p>A simple macro to lessen code noise:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">defmacro</span> dividing <span style="color: #005a89;">&#40;</span>num &amp;keys <span style="color: #005a89;">&#40;</span>initial-value <span style="color: #cc66cc;">0</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
  `<span style="color: #005a89;">&#40;</span>reducing ,num by #'<span style="color: #66cc66;">/</span> initial-value ,initial-value<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">iter</span> <span style="color: #005a89;">&#40;</span>for i in '<span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">10</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">2</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>dividing i <span style="color: #000099;">:initial-value</span> <span style="color: #cc66cc;">100</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #cc66cc;">1</span></pre></div></div>

<p>(Obviously the above is better stated <code>(/ 100 10 5 2)</code>, but imagine leveraging this clause in a more complicated loop.)</p>
<p>The ITERATE package provides a macro, <code>DEFMACRO-CLAUSE</code>, to create new clauses more idiomatically; read more about it in <a href="http://items.sjbach.com/280/extending-the-iterate-macro">the follow up article</a>.</p>
<p>For comparison, <a href="http://subpic.blogspot.com/2007/02/more-then-list-comprehensions-lisp-loop.html">this article describes one way of writing new reduction constructs for LOOP</a>.</p>
<h5>*Boolean aggregation*</h5>
<p>These are the same in both: <code>always</code>, <code>never</code>, <code>thereis</code>, corresponding to the functions <a href="http://www.lispworks.com/documentation/HyperSpec/Body/f_everyc.htm"><code>EVERY</code>, <code>NOTANY</code>, and <code>SOME</code></a>.  ITERATE can specify both <code>always</code> and <code>never</code> in the same loop, but not to much purpose.</p>
<h5>*Finding*</h5>
<p>There is no analogue for <code>finding</code> in LOOP.  The ITERATE website provides a good use case:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">iter</span> <span style="color: #005a89;">&#40;</span>for lst in '<span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span>a<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span>b c d<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span>e f<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>finding lst maximizing <span style="color: #005a89;">&#40;</span><span style="color: #007719;">length</span> lst<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #005a89;">&#40;</span>B C D<span style="color: #005a89;">&#41;</span> 
&nbsp;
<span style="color: #808080; font-style: italic;">;; The rough equivalent in LOOP:</span>
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">loop</span> with max-lst <span style="color: #66cc66;">=</span> <span style="color: #007719;">nil</span>
      with max-key <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>
      for lst in '<span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span>a<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span>b c d<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span>e f<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
      for key <span style="color: #66cc66;">=</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">length</span> lst<span style="color: #005a89;">&#41;</span>
      <span style="color: #007719;">do</span>
      <span style="color: #005a89;">&#40;</span><span style="color: #007719;">when</span> <span style="color: #005a89;">&#40;</span><span style="color: #66cc66;">&gt;</span> key max-key<span style="color: #005a89;">&#41;</span>
        <span style="color: #005a89;">&#40;</span><span style="color: #007719;">setf</span> max-lst lst
              max-key key<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
      finally <span style="color: #005a89;">&#40;</span><span style="color: #007719;">return</span> max-lst<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #005a89;">&#40;</span>B C D<span style="color: #005a89;">&#41;</span></pre></div></div>

<p><code>finding</code> is a pattern for using the result of one expression based on the result of another.</p>
<h5>*Control flow*</h5>
<p>ITERATE has <code>next-iteration</code>, which is like <code>continue</code> in C or <code>next</code> in Perl.  It&#8217;s a major inconvenience of LOOP that it doesn&#8217;t have this construct.  ITERATE also offers the <code>(if-first-time <i>then</i> <i>else</i>)</code> form and the <code>first-iteration-p</code> var for conditioning on the initial iteration in cases which aren&#8217;t covered by patterns.</p>
<h5>*Destructuring*</h5>
<p>In a <code>for</code> clause, ITERATE and LOOP have the same syntax and same destructuring capabilities.  However, ITERATE can also &#8220;destructure&#8221; multiple-value returns:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span>for <span style="color: #005a89;">&#40;</span><span style="color: #007719;">values</span> <span style="color: #005a89;">&#40;</span>a . b<span style="color: #005a89;">&#41;</span> c d<span style="color: #005a89;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">three-valued-function</span> ...<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>The consing equivalent in LOOP:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;">for <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span>a . b<span style="color: #005a89;">&#41;</span> c d<span style="color: #005a89;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">multiple-value-list</span>
                     <span style="color: #005a89;">&#40;</span><span style="color: #007719;">three-valued-function</span> ...<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>LOOP can do destructuring in a <code>with</code> clause, but ITERATE cannot.  The manual cites implementation difficulty.</p>
<h5>*Parallel binding*</h5>
<p>This is what DO does and DO* doesn&#8217;t, and it&#8217;s strictly unsupported in ITERATE.  LOOP includes this optionally with an <code>and</code> clause:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">loop</span> for el in '<span style="color: #005a89;">&#40;</span>a b c d e<span style="color: #005a89;">&#41;</span>
      <span style="color: #007719;">and</span> prev-el <span style="color: #66cc66;">=</span> <span style="color: #007719;">nil</span> then el
      collect <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span> el prev-el<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span>A <span style="color: #007719;">NIL</span><span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span>B A<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span>C B<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span>D C<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span>E D<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>The ITERATE documentation states: </p>
<blockquote><p>&#8220;My view is that if you are depending on the serial/parallel distinction, you are doing something obscure&#8221;</p></blockquote>
<p>I have to agree.  Also, the useful case of parallel binding in the LOOP example above can be accomplished with another ITERATE concept, variable backtracking:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">iter</span> <span style="color: #005a89;">&#40;</span>for el in '<span style="color: #005a89;">&#40;</span>a b c d e<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>for prev-el previous el<span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span>collect <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span> el prev-el<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span>A <span style="color: #007719;">NIL</span><span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span>B A<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span>C B<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span>D C<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span>E D<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span></pre></div></div>

</div>
<p><a name="4"></a></p>
<h3>4. Documentation</h3>
<p>There are many resources out there for learning to use LOOP.  The <a href="http://gigamonkeys.com/book/loop-for-black-belts.html">LOOP for Black Belts chapter of Practical Common Lisp</a> is my favourite.  The <a href="http://clqr.berlios.de/index.php">Common Lisp Quick Reference</a> is also excellent in its treatment of LOOP.</p>
<p>For ITERATE, there really is only the <a href="http://common-lisp.net/project/iterate/iterate-manual.pdf">manual</a>, but it&#8217;s thorough.  It includes <a href="http://common-lisp.net/project/iterate/doc/Don_0027t-Loop-Iterate.html">another comparison of LOOP and ITERATE</a>.  There&#8217;s also a <code>DISPLAY-ITERATE-CLAUSES</code> function which comes with the package and can provide dynamic assistance:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">display-iterate-clauses</span><span style="color: #005a89;">&#41;</span>
INITIALLY    Lisp forms to execute before <span style="color: #007719;">loop</span> starts
AFTER-EACH   Lisp forms to execute after each iteration
ELSE         Lisp forms to execute <span style="color: #007719;">if</span> <span style="color: #007719;">loop</span> is <span style="color: #007719;">not</span> entered
FINALLY      Lisp forms to execute after <span style="color: #007719;">loop</span> ends
<span style="color: #808080; font-style: italic;">;; ...</span>
&nbsp;
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">display-iterate-clauses</span> 'multiply<span style="color: #005a89;">&#41;</span>
MULTIPLY &amp;OPTIONAL INTO   Multiply into a variable</pre></div></div>

<p>It&#8217;d be great to see this integrated into SLIME.</p>
<p><a name="5"></a></p>
<h3>5. Obtaining ITERATE</h3>
<p>It&#8217;s easiest to do this with <a href="http://common-lisp.net/project/asdf-install">ASDF-Install</a>.  To get ITERATE working in a repl or scratch buffer:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; (Example below in SBCL.)</span>
CL-USER<span style="color: #66cc66;">&gt;</span> <span style="color: #005a89;">&#40;</span>asdf:<span style="color: #007719;">oos</span> 'asdf:load-op :<span style="color: #007719;">ASDF-INSTALL</span><span style="color: #005a89;">&#41;</span>
<span style="color: #808080; font-style: italic;">;; stuff</span>
&nbsp;
CL-USER<span style="color: #66cc66;">&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">asdf-install</span>:<span style="color: #007719;">install</span> <span style="color: #ff0000;">&quot;ITERATE&quot;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #808080; font-style: italic;">;; lots of stuff</span>
<span style="color: #808080; font-style: italic;">;; (only needs to be done once.)</span>
&nbsp;
CL-USER<span style="color: #66cc66;">&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">defpackage</span> <span style="color: #ff0000;">&quot;MY-PACKAGE&quot;</span> <span style="color: #005a89;">&#40;</span>:use <span style="color: #ff0000;">&quot;CL&quot;</span> <span style="color: #ff0000;">&quot;ITERATE&quot;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
#<span style="color: #66cc66;">&lt;</span>PACKAGE <span style="color: #ff0000;">&quot;MY-PACKAGE&quot;</span><span style="color: #66cc66;">&gt;</span>
CL-USER<span style="color: #66cc66;">&gt;</span> <span style="color: #005a89;">&#40;</span>in-package <span style="color: #ff0000;">&quot;MY-PACKAGE&quot;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
#<span style="color: #66cc66;">&lt;</span>PACKAGE <span style="color: #ff0000;">&quot;MY-PACKAGE&quot;</span><span style="color: #66cc66;">&gt;</span>
MY-PACKAGE<span style="color: #66cc66;">&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">iter</span> ...<span style="color: #005a89;">&#41;</span>  <span style="color: #808080; font-style: italic;">;; etc.</span></pre></div></div>

<p><a name="6"></a></p>
<h3>6. Conclusion</h3>
<p>In almost every case, ITERATE is more convenient to use and more powerful than LOOP.  If you&#8217;re in control of your own project and aren&#8217;t religiously partial to DO (or functional programming), ITERATE is worth a try.</p>
<p>Thanks to Richard Kreuter, Alec Berryman, and John O&#8217;Laughlin for their comments and suggestions.</p>
]]></content:encoded>
			<wfw:commentRss>http://items.sjbach.com/211/comparing-loop-and-iterate/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Writing a Vim plugin</title>
		<link>http://items.sjbach.com/97/writing-a-vim-plugin</link>
		<comments>http://items.sjbach.com/97/writing-a-vim-plugin#comments</comments>
		<pubDate>Sat, 11 Oct 2008 19:44:08 +0000</pubDate>
		<dc:creator>Stephen Bach</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://items.sjbach.com/?p=97</guid>
		<description><![CDATA[
Table of contents
1. Vim Script
2. Alternative plugin languages
3. Choosing a language

Vim is fairly extensible.  Unlike Emacs or Eclipse, it&#8217;s just an editor, not a platform.  It is, however, very featureful, and includes its own slightly eccentric domain language as well as bindings into a few others.  Note: this isn&#8217;t a HOWTO, it&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<ul class="toc">
<li class="title">Table of contents</li>
<li><a href="#vim_script">1. Vim Script</a></li>
<li><a href="#alternative">2. Alternative plugin languages</a></li>
<li><a href="#choosing">3. Choosing a language</a></li>
</ul>
<p><a href="http://www.vim.org/scripts/index.php">Vim is fairly extensible</a>.  Unlike Emacs or Eclipse, it&#8217;s just an editor, not a platform.  It is, however, very featureful, and includes its own slightly eccentric domain language as well as bindings into a few others.  Note: this isn&#8217;t a HOWTO, it&#8217;s just a few things to consider before jumping in.</p>
<p><a name="vim_script"></a></p>
<h3>1. Vim Script</h3>
<p>Here is a neat lineage: the Unix line editor <code>ed</code> evolved into the more advanced <code>ex</code> which was used as the basis for the command mode in <code>vi</code> and extended into the roughly Turing-complete mini-language of Vim.  And development continues; in his latest release, <a href="http://groups.google.com/group/vim_announce/browse_thread/thread/2c89671dd928812f">Bram Moolenaar has added native support for floating point numbers</a>.</p>
<p>Vim Script supports many regular programming concepts: loops, lists, dictionaries, exceptions, etc.  But the language is odd.  Here&#8217;s some code showing the hoops one must jump through to map a bit of functionality to a key:</p>

<div class="wp_syntax"><div class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #007719;">function!</span> s:doSomething<span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#41;</span>
  <span style="color: #808080; font-style: italic;">&quot; stuff</span>
<span style="color: #007719;">endfunction</span>
&nbsp;
<span style="color: #007719;">command</span> DoSomething <span style="color: #007719;">:call</span> <span style="color: #ff0000;">&lt;SID&gt;</span>doSomething<span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #007719;">nmap</span> k :DoSomething</pre></div></div>

<ul>
<li>A trailing <code>!</code> on <code>function</code> enables redefinition.</li>
<li>The <code>s:</code> in the function definition and the <code>&lt;SID&gt;</code> in the command declaration are a thin but credible form of namespace management.  They&#8217;ll expand to a unique name at read time so that similarly named functions in other files are not clobbered.</li>
<li><code>function</code> could be replaced equivalently with <code>fu</code>, <code>fun</code>, <code>func</code>, etc.  This follows for all Vim commands.  As long as a token can uniquely complete into a keyword, it is valid.</li>
</ul>
<p>As in many other languages, statements can be wrapped using a <code>\</code> character.  Unlike in those languages, in Vim Script it must appear at the beginning of the succeeding line:</p>

<div class="wp_syntax"><div class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #007719;">if</span> <span style="color: #005a89;">some_exceedingly_long_expression</span> <span style="color: #ff0000;">||</span>
   \ <span style="color: #005a89;">a_second_expression</span>
  <span style="color: #007719;">echo</span> 'Success'
<span style="color: #007719;">endif</span></pre></div></div>

<p><a name="alternative"></a></p>
<h3>2. Alternative plugin languages</h3>
<p>It isn&#8217;t widely known that Vim has interfaces into several popular scripting languages: Python, Ruby, Perl, Scheme, and Tcl.  These are more powerful than Vim Script but have certain drawbacks in use.</p>
<ol>
<li><strong>Debugging is difficult.</strong> Foreign code is interpreted by what is essentially one giant eval.  If you misplace a close parenthesis or an <code>end</code> keyword, you will have to track it down yourself.</li>
<li><strong>Integration with Vim is slight.</strong> The calling interface is in a table below.  Most interaction with the editor is tunneled through <code>Vim::evaluate</code> or <code>Vim::command</code> (or equivalent) as a string argument.</li>
<li><strong>Many Vim installations don&#8217;t include external language support by default.</strong> It&#8217;s an easy fix for a user running a <code>deb</code> or <code>rpm</code>-based Linux distribution, but will require a recompile on Windows, something a Windows user is not wont to do.</li>
</ol>
<p>Point (3) is particularly unfortunate if you are making an extension you intend to distribute.  I wrote <a href="http://www.vim.org/scripts/script.php?script_id=1890">perhaps the largest Ruby-based plugin for Vim available</a>; the majority of people who contact me about it are only looking for installation help!</p>
<p>This is what it looks like to interact with Vim from Ruby:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#808080; font-style:italic;"># Setting options inside the editor is pretty</span>
<span style="color:#808080; font-style:italic;"># straightforward.</span>
<span style="color:#CC00FF;">VIM</span>::<span style="color:#CC0066;">set_option</span> <span style="color:#996600;">&quot;noinsertmode&quot;</span>
<span style="color:#CC00FF;">VIM</span>::<span style="color:#CC0066;">set_option</span> <span style="color:#996600;">&quot;hlsearch&quot;</span>
&nbsp;
<span style="color:#808080; font-style:italic;"># ...unless you want to set an option local to a buffer.</span>
<span style="color:#808080; font-style:italic;"># There is no API call for this, so we must go up one</span>
<span style="color:#808080; font-style:italic;"># layer of abstraction:</span>
<span style="color:#CC00FF;">VIM</span>::<span style="color:#CC0066;">command</span> <span style="color:#996600;">&quot;setlocal nowrap&quot;</span>
<span style="color:#CC00FF;">VIM</span>::<span style="color:#CC0066;">command</span> <span style="color:#996600;">&quot;setlocal spell&quot;</span>
<span style="color:#CC00FF;">VIM</span>::<span style="color:#CC0066;">command</span> <span style="color:#996600;">&quot;setlocal foldcolumn=0&quot;</span></pre></div></div>

<p>If we&#8217;re going to do an odd call in multiple places, it makes sense to add some glue:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC;">def</span> <span style="color:#CC00FF;">VIM</span>::<span style="color:#007719;">has_syntax?</span>
  <span style="color:#808080; font-style:italic;"># All return values from `evaluate` are strings, and</span>
  <span style="color:#808080; font-style:italic;"># &quot;0&quot; evaluates to true in ruby.</span>
  <span style="color:#CC00FF;">VIM</span>::<span style="color:#CC0066;">evaluate</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'has(&quot;syntax&quot;)'</span><span style="color:#006600; font-weight:bold;">&#41;</span> != <span style="color:#996600;">&quot;0&quot;</span>
<span style="color:#9966CC;">end</span></pre></div></div>

<p>See below the partial interfaces for a few languages.  Obviously there&#8217;s room for improvement in the API:</p>
<table border="0">
<tbody>
<tr>
<th>Editor concept</th>
<th>Ruby</th>
<th>Python</th>
<th>MzScheme</th>
</tr>
<tr>
<td><em>eval</em></td>
<td><code>VIM::evaluate</code></td>
<td><code>vim.eval</code></td>
<td><code>(eval)</code></td>
</tr>
<tr>
<td><em>command</em></td>
<td><code>VIM::command</code></td>
<td><code>vim.command</code></td>
<td><code>(command)</code></td>
</tr>
<tr>
<td><em>option</em></td>
<td><code>VIM::set_option</code></td>
<td align="center">&#8211;</td>
<td><code>(get-option)</code>,<br />
<code>(set-option)</code></td>
</tr>
<tr>
<td><em>output</em></td>
<td><code>VIM::message</code></td>
<td><code>sys.stdout</code></td>
<td align="center">&#8211;</td>
</tr>
<tr>
<td><em>buffer</em></td>
<td><code>VIM::Buffer</code></td>
<td><code>vim.buffers</code></td>
<td><code>(get-next-buff)</code>,<br />
<code>(get-prev-buff)</code></td>
</tr>
<tr>
<td><em>window</em></td>
<td><code>VIM::Window</code></td>
<td><code>vim.windows</code></td>
<td><code>(get-win-list)</code></td>
</tr>
<tr>
<td><em>current<br />
buffer</em></td>
<td><code>$curbuf</code></td>
<td><code>vim.current.buffer</code></td>
<td><code>(curr-buff)</code></td>
</tr>
<tr>
<td><em>current<br />
window</em></td>
<td><code>$curwin</code></td>
<td><code>vim.current.window</code></td>
<td><code>(curr-win)</code></td>
</tr>
<tr>
<td><em>range</em></td>
<td align="center">&#8211;</td>
<td><code>vim.current.range</code></td>
<td><code>(range-start)</code>,<br />
<code>(range-end)</code></td>
</tr>
<tr>
<td></td>
<td align="center"><a href="http://vim.sourceforge.net/htmldoc/if_ruby.html">Manual</a></td>
<td align="center"><a href="http://vim.sourceforge.net/htmldoc/if_pyth.html">Manual</a></td>
<td align="center"><a href="http://wwwcdf.pd.infn.it/vim/if_mzsch.html">Manual</a></td>
</tr>
</tbody>
</table>
<p>(<a href="http://vim.sourceforge.net/htmldoc/if_tcl.html">Tcl</a> and <a href="http://vim.sourceforge.net/htmldoc/if_perl.html">Perl</a> not shown.)</p>
<p>For some strange reason, the Scheme interface also offers <code>(beep)</code>, and the Tcl interface, <code>::vim::beep</code>.</p>
<p>As an interesting sidenote, these extension languages have access to window handles within Vim, allowing deterministic window management.  Vim Script doesn&#8217;t seem to support this, so using an alternative language may offer a superset of functionality.</p>
<p><a name="choosing"></a></p>
<h3>3. Choosing a language</h3>
<p>This can be summarized like so:</p>
<h5 class="proscons">Vim Script</h5>
<blockquote class="proscons"><p>Pros:</p>
<ul class="proscons">
<li class="pro">Great integrated <code>:help</code> system.</li>
<li class="pro">Lots of other plugins you can crib from.</li>
</ul>
<p>Cons:</p>
<ul class="proscons">
<li class="con">The language is awkward.</li>
</ul>
</blockquote>
<p>&nbsp;<br />
</p>
<h5 class="proscons">Other (Perl, Python, Ruby, Tcl, Scheme)</h5>
<blockquote class="proscons"><p>Pros:</p>
<ul class="proscons">
<li class="pro">Strong languages.</li>
<li class="pro">Experience carries over into other pursuits.</li>
</ul>
<p>Cons:</p>
<ul class="proscons">
<li class="con">Debugging is hard.</li>
<li class="con">Interface to Vim is slight.</li>
<li class="con">May require the user to install language libraries.</li>
<li class="con">Syntax highlighting in a <code>.vim</code> file is easily confused.</li>
<li class="con">Neglected by both plugin writers and Vim developers.</li>
</ul>
</blockquote>
<p>&nbsp;<br />
<br />
Using Vim Script means giving up the good stuff: closures, object-orientation, higher-order functions, reflection, and metaprogramming.  So despite the extra work, selecting an alternative language is recommended for non-trivial extensions.  Perhaps support within Vim will improve as more plugin writers follow this route.</p>
<p>Thanks to Jesse Funaro and Chris Gaal for their comments and suggestions.</p>
]]></content:encoded>
			<wfw:commentRss>http://items.sjbach.com/97/writing-a-vim-plugin/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Some notes about Clojure</title>
		<link>http://items.sjbach.com/16/some-notes-about-clojure</link>
		<comments>http://items.sjbach.com/16/some-notes-about-clojure#comments</comments>
		<pubDate>Sat, 04 Oct 2008 21:36:21 +0000</pubDate>
		<dc:creator>Stephen Bach</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://items.sjbach.com/?p=16</guid>
		<description><![CDATA[
Table of contents
1. Implicit destructuring in binding forms
2. Unnamed arguments for short lambdas
3. Data structures as functions
4. Implicit gensyms
5. &#8220;[...]&#8221; syntax
6. SLIME integration

1. Only single-value function returns
2. Recursion-looking iteration construct


Last week, Rich Hickey came to speak at the monthly Boston Lisp Meeting about his new-ish programming language, Clojure.  His presentation went double length, but [...]]]></description>
			<content:encoded><![CDATA[<ul class="toc">
<li class="title">Table of contents</li>
<li><a href="#1">1. Implicit destructuring in binding forms</a></li>
<li><a href="#2">2. Unnamed arguments for short lambdas</a></li>
<li><a href="#3">3. Data structures as functions</a></li>
<li><a href="#4">4. Implicit gensyms</a></li>
<li><a href="#5">5. &#8220;<code>[...]</code>&#8221; syntax</a></li>
<li><a href="#6">6. SLIME integration</a></li>
<p></p>
<li><a href="#1_">1. Only single-value function returns</a></li>
<li><a href="#2_">2. Recursion-looking iteration construct</a></li>
</ul>
<p>
Last week, <a href="http://clojure.blip.tv/file/1313398">Rich Hickey came to speak at the monthly Boston Lisp Meeting</a> about his new-ish programming language, Clojure.  His presentation went double length, but few left early.  Rich showed an impressive breadth of knowledge, fielding some tough questions and making a strong case for his language.
</p>
<p>
I fooled around with Clojure a little and wanted to share some of the things which haven&#8217;t gotten much play so far.
</p>
<p><a name="1"></a></p>
<h3>1. Implicit destructuring in binding forms</h3>
<p>See examples below:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">def</span> flat <span style="color: #ff0000;">&quot;flat&quot;</span><span style="color: #005a89;">&#41;</span>
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">def</span> tree '<span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span><span style="color: #ff0000;">&quot;one&quot;</span> <span style="color: #005a89;">&#40;</span><span style="color: #ff0000;">&quot;two&quot;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span> <span style="color: #ff0000;">&quot;three&quot;</span> <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span><span style="color: #ff0000;">&quot;four&quot;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; Simple binding (like Common Lisp's LET*).</span>
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">let</span> <span style="color: #005a89;">&#91;</span>var1 flat
      var2 tree<span style="color: #005a89;">&#93;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span> var1 var2<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
-<span style="color: #66cc66;">&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #ff0000;">&quot;flat&quot;</span> <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span><span style="color: #ff0000;">&quot;one&quot;</span> <span style="color: #005a89;">&#40;</span><span style="color: #ff0000;">&quot;two&quot;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span> <span style="color: #ff0000;">&quot;three&quot;</span> <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span><span style="color: #ff0000;">&quot;four&quot;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; Full destructuring.</span>
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">let</span> <span style="color: #005a89;">&#91;</span>var1 flat
      <span style="color: #005a89;">&#91;</span><span style="color: #005a89;">&#91;</span>a <span style="color: #005a89;">&#91;</span>b<span style="color: #005a89;">&#93;</span><span style="color: #005a89;">&#93;</span> c <span style="color: #005a89;">&#91;</span><span style="color: #005a89;">&#91;</span><span style="color: #005a89;">&#91;</span>d<span style="color: #005a89;">&#93;</span><span style="color: #005a89;">&#93;</span><span style="color: #005a89;">&#93;</span><span style="color: #005a89;">&#93;</span> tree<span style="color: #005a89;">&#93;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span> var1 a b c d<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
-<span style="color: #66cc66;">&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #ff0000;">&quot;flat&quot;</span> <span style="color: #ff0000;">&quot;one&quot;</span> <span style="color: #ff0000;">&quot;two&quot;</span> <span style="color: #ff0000;">&quot;three&quot;</span> <span style="color: #ff0000;">&quot;four&quot;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; Partial destructuring.</span>
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">let</span> <span style="color: #005a89;">&#91;</span><span style="color: #005a89;">&#91;</span><span style="color: #005a89;">&#91;</span>a <span style="color: #005a89;">&#91;</span>b<span style="color: #005a89;">&#93;</span><span style="color: #005a89;">&#93;</span> &amp; leftover :as all<span style="color: #005a89;">&#93;</span> tree<span style="color: #005a89;">&#93;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span> a b leftover all<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
-<span style="color: #66cc66;">&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #ff0000;">&quot;one&quot;</span> <span style="color: #ff0000;">&quot;two&quot;</span> <span style="color: #005a89;">&#40;</span><span style="color: #ff0000;">&quot;three&quot;</span> <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span><span style="color: #ff0000;">&quot;four&quot;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
    <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span><span style="color: #ff0000;">&quot;one&quot;</span> <span style="color: #005a89;">&#40;</span><span style="color: #ff0000;">&quot;two&quot;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span> <span style="color: #ff0000;">&quot;three&quot;</span> <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span><span style="color: #ff0000;">&quot;four&quot;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; Works on strings, too.</span>
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">let</span> <span style="color: #005a89;">&#91;</span><span style="color: #005a89;">&#91;</span>a b c &amp; leftover<span style="color: #005a89;">&#93;</span> <span style="color: #ff0000;">&quot;123go&quot;</span><span style="color: #005a89;">&#93;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span> a b c leftover<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
-<span style="color: #66cc66;">&gt;</span> <span style="color: #005a89;">&#40;</span>\<span style="color: #cc66cc;">1</span> \<span style="color: #cc66cc;">2</span> \<span style="color: #cc66cc;">3</span> <span style="color: #005a89;">&#40;</span>\g \o<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p><a name="2"></a></p>
<h3>2. Unnamed arguments for short lambdas</h3>
<p>Pretty straightforward:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">map</span> #<span style="color: #005a89;">&#40;</span><span style="color: #007719;">+</span> % <span style="color: #cc66cc;">4</span><span style="color: #005a89;">&#41;</span> '<span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span> 
&nbsp;
-<span style="color: #66cc66;">&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">7</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; Multiple arguments.</span>
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">map</span> #<span style="color: #005a89;">&#40;</span><span style="color: #007719;">*</span> %<span style="color:#cc66cc;">1</span> %<span style="color:#cc66cc;">2</span><span style="color: #005a89;">&#41;</span> '<span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #005a89;">&#41;</span> '<span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">6</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
-<span style="color: #66cc66;">&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">10</span> <span style="color: #cc66cc;">18</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>This is roughly equivalent to <a href="http://www.arclanguage.org">Arc</a>&#8217;s <code>[+ _ 4]</code> form, though allows for more than one argument.  The standard lambda form is also similar to Arc&#8217;s:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">map</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">fn</span> <span style="color: #005a89;">&#91;</span>x<span style="color: #005a89;">&#93;</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">+</span> x <span style="color: #cc66cc;">4</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span> '<span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #005a89;">&#41;</span>
&nbsp;
-<span style="color: #66cc66;">&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">7</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p><a name="3"></a></p>
<h3>3. Data structures as functions</h3>
<p>When a map or vector is called as a function, its argument is used as a value lookup.</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">let</span> <span style="color: #005a89;">&#91;</span>myvec <span style="color: #005a89;">&#91;</span><span style="color: #cc66cc;">100</span> <span style="color: #cc66cc;">200</span> <span style="color: #cc66cc;">300</span><span style="color: #005a89;">&#93;</span>
      mymap <span style="color: #005a89;">&#123;</span>:a <span style="color: #cc66cc;">1</span> :b <span style="color: #cc66cc;">2</span> :c <span style="color: #cc66cc;">3</span><span style="color: #005a89;">&#125;</span><span style="color: #005a89;">&#93;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span> <span style="color: #005a89;">&#40;</span>myvec <span style="color: #cc66cc;">1</span><span style="color: #005a89;">&#41;</span>
        <span style="color: #005a89;">&#40;</span>mymap :c<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
-<span style="color: #66cc66;">&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">200</span> <span style="color: #cc66cc;">3</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>For maps, symbols and keywords can also work in a functional context:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span>:a <span style="color: #005a89;">&#123;</span>:a <span style="color: #cc66cc;">1</span> :b <span style="color: #cc66cc;">2</span> :c <span style="color: #cc66cc;">3</span><span style="color: #005a89;">&#125;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
-<span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">1</span>
&nbsp;
<span style="color: #005a89;">&#40;</span>'b <span style="color: #005a89;">&#123;</span>'a <span style="color: #cc66cc;">1</span> 'b <span style="color: #cc66cc;">2</span> 'c <span style="color: #cc66cc;">3</span><span style="color: #005a89;">&#125;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
-<span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">2</span></pre></div></div>

<p><a name="4"></a></p>
<h3>4. Implicit gensyms</h3>
<p>Perhaps a minor point, but handy for cleaner macros.</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">defmacro</span> inc-safe <span style="color: #005a89;">&#91;</span>var<span style="color: #005a89;">&#93;</span>
  `<span style="color: #005a89;">&#40;</span><span style="color: #007719;">let</span> <span style="color: #005a89;">&#91;</span>var# ~var<span style="color: #005a89;">&#93;</span>
     <span style="color: #005a89;">&#40;</span><span style="color: #007719;">if</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">integer?</span> var#<span style="color: #005a89;">&#41;</span>
       <span style="color: #005a89;">&#40;</span><span style="color: #007719;">inc</span> var#<span style="color: #005a89;">&#41;</span>
       <span style="color: #cc66cc;">0</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>In a backquoted form, <code>~var</code> means to unquote (evaluate) <code>var</code> (like <code>,var</code> in other Lisps) and <code>var#</code> creates a gensym whose name persists within the form.  Compare to the equivalent in Common Lisp:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">defmacro</span> <span style="color: #cc66cc;">1</span>+-safe <span style="color: #005a89;">&#40;</span>var<span style="color: #005a89;">&#41;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">let</span> <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span>gvar <span style="color: #005a89;">&#40;</span><span style="color: #007719;">gensym</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
    `<span style="color: #005a89;">&#40;</span><span style="color: #007719;">let</span> <span style="color: #005a89;">&#40;</span><span style="color: #005a89;">&#40;</span>,gvar ,var<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
       <span style="color: #005a89;">&#40;</span><span style="color: #007719;">if</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">integerp</span> ,gvar<span style="color: #005a89;">&#41;</span>
           <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">1</span>+ ,gvar<span style="color: #005a89;">&#41;</span>
           <span style="color: #cc66cc;">0</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p><a name="5"></a></p>
<h3>5. &#8220;<code>[...]</code>&#8221; syntax when code is not in an executing context</h3>
<p>
Look at the argument lists in any of the excerpts above to see what I mean.  I&#8217;m kind-of on the fence on this one &#8212; on one hand it is semantically clearer, on the other it overloads the vector syntax.  Also, I think round parentheses are just nicer looking.
</p>
<p><a name="6"></a></p>
<h3>6. SLIME integration</h3>
<p><a href="http://common-lisp.net/project/slime/">SLIME</a> is the Superior Lisp Interaction Mode for Emacs, and it&#8217;s something you can&#8217;t live without once you&#8217;ve tried it.  Clone <a href="http://github.com/jochu/swank-clojure/tree/master">this git repository</a> and follow the instructions in the <code>README</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #9966CC;">git</span> <span style="color: #008000;">clone</span> <span style="color: #9966CC;">git</span>:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>jochu<span style="color: #000000; font-weight: bold;">/</span>swank-clojure.git</pre></div></div>

<p>&nbsp;<br />
</p>
<hr />
<p>
&nbsp;<br />
</p>
<p>
There are, however, a couple design choices which are less appealing, perhaps especially to Common Lisp programmers.
</p>
<p><a name="1_"></a></p>
<h3>1. Only single-value function returns</h3>
<p>
I believe this was briefly explained as being limited by the rules of stack allocation in the JVM.  The restriction can be mitigated somewhat by returning a list of values which are then destructured:
</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">defn</span> foo <span style="color: #005a89;">&#91;</span><span style="color: #005a89;">&#93;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span> 'aaa 'bbb<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
<span style="color: #005a89;">&#40;</span><span style="color: #007719;">let</span> <span style="color: #005a89;">&#91;</span><span style="color: #005a89;">&#91;</span>a b<span style="color: #005a89;">&#93;</span> <span style="color: #005a89;">&#40;</span>foo<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#93;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span> b a<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
-<span style="color: #66cc66;">&gt;</span> <span style="color: #005a89;">&#40;</span>bbb aaa<span style="color: #005a89;">&#41;</span></pre></div></div>

<p>Common Lisp emulation in Emacs Lisp follows this path.  It is a leaky abstraction, though, because <code>foo</code> in the example will always return a list.  Even if we only care about the first value, we must acknowledge in every call that it returns others.  Compare with Common Lisp&#8217;s <code><a href="http://www.lispworks.com/documentation/HyperSpec/Body/f_gethas.htm">GETHASH</a></code>: it has two return values, yet in most contexts it&#8217;s treated as having only one.</p>
<p><a name="2_"></a></p>
<h3>2. Iteration construct that looks more like recursion</h3>
<p>
Clojure is a functional language with immutable types.  One might expect it would then tend toward recursive programming.  Unfortunately, there&#8217;s no tail-call optimization in Clojure currently, as it isn&#8217;t supported by the JVM; since it&#8217;s certainly a sought feature, likely this will change soon.  In the meantime, where efficiency matters one can use <code>loop ... recur</code>, which looks a lot like intra-function recursion.
</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">defn</span> loop-test <span style="color: #005a89;">&#91;</span><span style="color: #005a89;">&#93;</span>
  <span style="color: #005a89;">&#40;</span><span style="color: #007719;">loop</span> <span style="color: #005a89;">&#91;</span>collection <span style="color: #005a89;">&#40;</span><span style="color: #007719;">list</span><span style="color: #005a89;">&#41;</span>
         i <span style="color: #cc66cc;">0</span><span style="color: #005a89;">&#93;</span>
    <span style="color: #005a89;">&#40;</span><span style="color: #007719;">if</span> <span style="color: #005a89;">&#40;</span><span style="color: #66cc66;">&lt;</span> i <span style="color: #cc66cc;">5</span><span style="color: #005a89;">&#41;</span>
      <span style="color: #005a89;">&#40;</span><span style="color: #007719;">recur</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">conj</span> collection i<span style="color: #005a89;">&#41;</span> <span style="color: #005a89;">&#40;</span><span style="color: #007719;">inc</span> i<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
      collection<span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span><span style="color: #005a89;">&#41;</span>
&nbsp;
-<span style="color: #66cc66;">&gt;</span> <span style="color: #005a89;">&#40;</span><span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">0</span><span style="color: #005a89;">&#41;</span></pre></div></div>

<p>
For simple iterative patterns, there are also <code>for</code> and <code>doseq</code>.
</p>
<p>&nbsp;<br />
</p>
<hr />
<p>
&nbsp;<br />
</p>
<p>
All in all, Clojure is very interesting.  I should note I&#8217;ve only mentioned superficial stuff; the larger part of the second half of the presentation was Rich expounding on the ease of concurrency and the value of software transactional memory in the language, but these are larger topics (and over my head).
</p>
<p>Thanks to Alec Berryman and George Polak for their comments and suggestions.</p>
]]></content:encoded>
			<wfw:commentRss>http://items.sjbach.com/16/some-notes-about-clojure/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 6/9 queries in 0.036 seconds using disk

Served from: items.sjbach.com @ 2010-07-29 13:53:25 -->