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

<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>
	<pubDate>Thu, 18 Dec 2008 02:10:52 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
	<language>en</language>
			<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[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 quick overview.
1. What has changed
In a sentence, fewer backslashes.  The notation is now [...]]]></description>
			<content:encoded><![CDATA[<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>
<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 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 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 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 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>

<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>
<h5>Ruby and Perl 5</h5>

<div class="wp_syntax"><div class="code"><pre class="text 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>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>

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

<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>

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

<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 text" style="font-family:monospace;">&quot;&lt;img\\s+src=\&quot;\\w+/\\d+/(\\w+)&quot;</pre></div></div>

<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 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 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>

<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 joining it 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 did not include a patch.</p>
]]></content:encoded>
			<wfw:commentRss>http://items.sjbach.com/406/clojures-new-regex-syntax/feed</wfw:commentRss>
		</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[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 have 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 [...]]]></description>
			<content:encoded><![CDATA[<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 have 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>
<h3>Essential <code>.vimrc</code> configuration items</h3>
<p>For whatever reason, the following options are not set by default, but they should be.</p>
<ol>
<li class="vimlist"><b class="header">Turn on <code>hidden</code></b>
<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 vim2" style="font-family:monospace;"><span style="color: #007719;">set</span> <span style="color: #005a89;">hidden</span></pre></div></div>

</li>
<li class="vimlist"><b class="header">Remap <code>`</code> to <code>'</code></b>
<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 is more useful in any case I can imagine, but it is 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 vim2" style="font-family:monospace;"><span style="color: #007719;">nnoremap</span> ' `
<span style="color: #007719;">nnoremap</span> ` '</pre></div></div>

</li>
<li class="vimlist"><b class="header">Map <code>leader</code> to <code>,</code></b>
<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 is not located standardly on all keyboards and requires a pinky stretch in any case.
</p>

<div class="wp_syntax"><div class="code"><pre class="vim2 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.
</li>
<li class="vimlist"><b class="header">Keep a longer history</b>
<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 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>

</li>
<li class="vimlist"><b class="header">Enable extended <code>%</code> matching</b>
<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 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.
</li>
<li class="vimlist"><b class="header">Make file/command completion useful</b>
<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 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 vim2" style="font-family:monospace;"><span style="color: #007719;">set</span> <span style="color: #005a89;">wildmode</span>=list:longest</pre></div></div>

</li>
</ol>
<h3>Recommended <code>.vimrc</code> configuration items</h3>
<p>Most people like these.</p>
<ol>
<li class="vimlist"><b class="header">Use case-smart searching</b>
<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 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.
</li>
<li class="vimlist"><b class="header">Set the terminal title</b>
<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 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> |.
</li>
<li class="vimlist"><b class="header">Maintain more context around the cursor</b>
<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 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>!)
</li>
<li class="vimlist"><b class="header">Store temporary files in a central spot</b>
<p>
Swap files and backups are annoying but can save you a lot of trouble.  Rather than spreading them all around your filesystem, isolate them to a single directory:
</p>

<div class="wp_syntax"><div class="code"><pre class="vim2 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 will be easier to clobber concurrent modifications, as other users&#8217; Vim processes processes will not see your swaps.
</li>
<li class="vimlist"><b class="header">Scroll the viewport faster</b>
<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 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>

</li>
<li class="vimlist"><b class="header">Enable limited line numbering</b>
<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 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.
</li>
<li class="vimlist"><b class="header">A bunch of stuff your OS should already do</b>
<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 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 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>

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

<div class="wp_syntax"><div class="code"><pre class="vim2 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>.
</li>
<li class="vimlist"><b class="header">Stifle many interruptive prompts</b>
<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 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.
</li>
<li class="vimlist"><b class="header">Stop distracting your co-workers</b>
<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 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.
</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 input on a draft of this article.</p>
]]></content:encoded>
			<wfw:commentRss>http://items.sjbach.com/319/configuring-vim-right/feed</wfw:commentRss>
		</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[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, for accumulating or reducing an expression;
Drivers, introduced with for or generate, for changing a value in [...]]]></description>
			<content:encoded><![CDATA[<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 did not appear in the article comparing LOOP and ITERATE (it is wholly a feature of ITERATE), so here first is a short introduction.</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 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 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>

<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 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 is 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 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 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 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 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>

<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 is a function which will return all leaves:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp 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 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 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>

<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 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 t <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 input on a draft of this article.</p>
]]></content:encoded>
			<wfw:commentRss>http://items.sjbach.com/280/extending-the-iterate-macro/feed</wfw:commentRss>
		</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[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 will explore two options within Common Lisp:

LOOP is a standard macro with an expressive syntax and built-in support for several iterative patterns.  It is lauded and criticized in [...]]]></description>
			<content:encoded><![CDATA[<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 will 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 is 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 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 are widely used but can be terse and obscure, and they are not the subject of this article.  </p>
<h3>1. Views on LOOP</h3>
<p>The following are the three most frequent criticisms of LOOP:</p>
<ol>
<li>
<p>It does not 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 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 is not a definite improvement:</p>

<div class="wp_syntax"><div class="code"><pre class="commonlisp 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 do not auto-indent LOOP well.  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 is 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 exhibit it.</li>
</ol>
<p>For these reasons and others, <a href="http://www.paulgraham.com">Paul Graham</a> does not recommend LOOP, and <a href="http://gigamonkeys.com/book">Peter Seibel</a> remains neutral about it.  But it does have 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>
<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 is 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 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 is 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 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 is 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 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 is 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>
<h3>3. Comparing looping clauses</h3>
<p>In most cases, ITERATE is a superset of functionality.</p>
<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 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 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 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 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 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 is a major inconvenience of LOOP that it does not 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 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 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* does not, and it is 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 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 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>

<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 is 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 is 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 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 would be great to see this integrated into SLIME.</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 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>

<h3>6. Conclusion</h3>
<p>In almost every case, ITERATE is more convenient to use and more powerful than LOOP.  If you are in control of your own project and are not religiously partial to DO (or functional programming), ITERATE it is worth a try.</p>
<p>Thanks to Richard Kreuter, Alec Berryman, and John O&#8217;Laughlin for their input on a draft of this article.</p>
]]></content:encoded>
			<wfw:commentRss>http://items.sjbach.com/211/comparing-loop-and-iterate/feed</wfw:commentRss>
		</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[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 is not a HOWTO, it&#8217;s just a few things to consider before jumping in.
Vim [...]]]></description>
			<content:encoded><![CDATA[<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 is not a HOWTO, it&#8217;s just a few things to consider before jumping in.</p>
<h3>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 is 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 vim" style="font-family:monospace;"><span style="color: #007719;">function!</span> s:<span style="color: #000ecb;">doSomething</span><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;">:<span style="color: #000ecb;">call</span></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 :<span style="color: #000ecb;">DoSomething</span></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 will 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 vim" style="font-family:monospace;"><span style="color: #007719;">if</span> <span style="color: #005a89;">some_exceedingly_long_expression</span> ||
   \ <span style="color: #005a89;">a_second_expression</span>
  <span style="color: #007719;">echo</span> <span style="color: #ff0000;">'Success'</span>
<span style="color: #007719;">endif</span></pre></div></div>

<h3>Alternative plugin languages</h3>
<p>It is not 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 do not include external language support by default.</strong> It is 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 ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Setting options inside the editor is pretty</span>
<span style="color:#008000; 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:#008000; font-style:italic;"># ...unless you want to set an option local to a buffer.</span>
<span style="color:#008000; font-style:italic;"># There is no API call for this, so we must go up one</span>
<span style="color:#008000; 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 ruby" style="font-family:monospace;"><span style="color:#9966CC;">def</span> <span style="color:#CC00FF;">VIM</span>::has_syntax?
  <span style="color:#008000; font-style:italic;"># All return values from `evaluate` are strings, and</span>
  <span style="color:#008000; 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>Obviously there is room for improvement in the API.  See below the partial interfaces for a few languages:</p>
<table class="T" border="0">
<tbody>
<tr>
<th></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),<br />
(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),<br />
(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),<br />
(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>
<h3>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 input on a draft of this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://items.sjbach.com/97/writing-a-vim-plugin/feed</wfw:commentRss>
		</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[
Last week, Rich Hickey came to speak at the monthly Boston Lisp Meeting about his new-ish programming language.  He gave a great presentation about Clojure; it went double length, but few of us left early.  Rich showed an impressive breadth of knowledge, fielding some tough questions and making a strong case for his [...]]]></description>
			<content:encoded><![CDATA[<p>
Last week, Rich Hickey came to speak at the monthly Boston Lisp Meeting about his new-ish programming language.  He gave a <a href="http://clojure.blip.tv/file/1313398">great presentation about Clojure</a>; it went double length, but few of us 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>
<h3>1. Implicit destructuring in binding forms</h3>
<p>See examples below:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure 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> <span style="color: #66cc66;">&amp;</span> leftover :<span style="color: #555;">as</span> 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 <span style="color: #66cc66;">&amp;</span> 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>

<h3>2. Unnamed arguments for short lambdas</h3>
<p>Pretty straightforward:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure 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 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>

<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 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>:<span style="color: #555;">a</span> <span style="color: #cc66cc;">1</span> :<span style="color: #555;">b</span> <span style="color: #cc66cc;">2</span> :<span style="color: #555;">c</span> <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 :<span style="color: #555;">c</span><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 clojure" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span>:<span style="color: #555;">a</span> <span style="color: #005a89;">&#123;</span>:<span style="color: #555;">a</span> <span style="color: #cc66cc;">1</span> :<span style="color: #555;">b</span> <span style="color: #cc66cc;">2</span> :<span style="color: #555;">c</span> <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>

<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 clojure" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">defmacro</span> inc<span style="color: #66cc66;">-</span>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 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>

<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>
<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 bash" style="font-family:monospace;">$ git clone git:<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>
<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 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 is treated as having only one.</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 is no tail-call optimization in Clojure currently, as it is not supported by the JVM; since it is 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 clojure" style="font-family:monospace;"><span style="color: #005a89;">&#40;</span><span style="color: #007719;">defn</span> loop<span style="color: #66cc66;">-</span>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 input on a draft of this article.</p>
]]></content:encoded>
			<wfw:commentRss>http://items.sjbach.com/16/some-notes-about-clojure/feed</wfw:commentRss>
		</item>
	</channel>
</rss>
