<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Mike McClurg</title>
	<atom:link href="http://mcclurmc.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://mcclurmc.wordpress.com</link>
	<description></description>
	<lastBuildDate>Sun, 21 Mar 2010 22:33:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='mcclurmc.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Mike McClurg</title>
		<link>http://mcclurmc.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://mcclurmc.wordpress.com/osd.xml" title="Mike McClurg" />
	<atom:link rel='hub' href='http://mcclurmc.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Creating Stream Combinators in Haskell&#8217;s Stream Fusion Library</title>
		<link>http://mcclurmc.wordpress.com/2010/03/21/creating-stream-combinators/</link>
		<comments>http://mcclurmc.wordpress.com/2010/03/21/creating-stream-combinators/#comments</comments>
		<pubDate>Sun, 21 Mar 2010 21:42:12 +0000</pubDate>
		<dc:creator>mcclurmc</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Stream Fusion]]></category>

		<guid isPermaLink="false">http://mcclurmc.wordpress.com/?p=72</guid>
		<description><![CDATA[So I took a look at the Haskell Stream Fusion library the other day, and got the idea to write a new append combinator that would merge the two streams in sort order. This seemed simple enough to code directly using Streams, but my first instinct is always to write the code using lists, and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mcclurmc.wordpress.com&amp;blog=11098563&amp;post=72&amp;subd=mcclurmc&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>So I took a look at the Haskell <a href="http://www.cse.unsw.edu.au/~dons/papers/CLS07.html">Stream Fusion</a> <a href="http://hackage.haskell.org/packages/archive/stream-fusion/0.1.2.2/doc/html/Data-Stream.html">library</a> the other day, and got the idea to write a new <code>append</code> combinator that would merge the two streams in sort order. This seemed simple enough to code directly using Streams, but my first instinct is always to write the code using lists, and then translate it into the more complicated syntax. Here&#8217;s what a sorting merge function looks like over lists:</p>
<p><code>
<pre>merge :: Ord a =&gt; [a] -&gt; [a] -&gt; [a]
merge []     bs                 = bs
merge as     []                 = as
merge (a:as) (b:bs) | a &lt; b     = a : merge as (b:bs)
                    | otherwise = b : merge (a:as) bs
</pre>
<p></code></p>
<p>We have two base cases where either one of the argument lists may be null, in which case we just return the other. For the recursive case, we just <code>cons</code> the lesser of the two list heads onto the rest of the list, and leave the other list head attached to its list in-place. Simple and elegant.</p>
<p>So the Stream version should be just as easy, right? Let&#8217;s see.</p>
<p><code>
<pre>mergeS_wrong :: Ord a =&gt; Stream a -&gt; Stream a -&gt; Stream a
mergeS_wrong (Stream nexta sa0) (Stream nextb sb0) = Stream next (sa0, sb0)
    where
      next (sa0, sb0) =
          case (nexta sa0, nextb sb0) of
            (Done, sb) -&gt;
                case sb of
                  Done        -&gt; Done
                  Skip sb'    -&gt; Skip    (sa0, sb')
                  Yield b sb' -&gt; Yield b (sa0, sb')
            (sa, Done) -&gt;
                case sa of
                  Done        -&gt; Done
                  Skip sa'    -&gt; Skip    (sa', sb0)
                  Yield a sa' -&gt; Yield a (sa', sb0)
            (sa, sb) -&gt;
                case sa of
                  Done        -&gt; Done -- shouldn't happen
                  Skip sa'    -&gt; Skip    (sa', sb0)
                  Yield a sa' -&gt;
                      case sb of
                        Done                    -&gt; Done -- shouldn't happen
                        Skip sb'                -&gt; Yield a (sa', sb')
                        Yield b sb' | a &lt; b     -&gt; Yield a (sa', sb0)
                                    | otherwise -&gt; Yield b (sa0, sb')
</pre>
<p></code></p>
<p>Looks like a wordier version of the first. We take the first element of each stream, and use a case expression to check each of our cases. The first two base cases are a little longer this time because we can&#8217;t just return the other stream, but instead have to either <code>Skip</code> or <code>Yield</code> over the remainder of the Stream. In the third case, we must <code>Skip</code> over the first Stream until we <code>Yield</code> a value, and then do the same for the second stream. We compare the two values, <code>Yield</code> the lesser, and return the two remaining Streams.</p>
<p>The only problem is that this won&#8217;t compile. <code>GHCi</code> gives us the following error message:</p>
<p><code>
<pre>*Main&gt; :load "/home/mike/Projects/Haskell_SVN/NumWords.hs"
[1 of 1] Compiling Main             ( /home/mike/Projects/Haskell_SVN/NumWords.hs, interpreted )

/home/mike/Projects/Haskell_SVN/NumWords.hs:59:53:
    Could not deduce (Data.Stream.Unlifted (s, s1))
      from the context (Data.Stream.Unlifted s1)
      arising from a use of `Stream'
</pre>
<p></code></p>
<p>What&#8217;s this <code>Data.Stream.Unlifted</code> type? Turns out that our Stream data type is encapsulated by a universally quantified type <code>s</code> that is an instance of the hidden type class <code>Unlifted</code>. The standard Haskell pair type <code>(,)</code> isn&#8217;t, unfortunately, an exposed instance of this class. Which means that we can&#8217;t make a Stream out of a pair of Streams, as we did on the second line of code with <code>Stream next (sa0, sb0)</code>.</p>
<p>Or so I thought. That is, until I realized (after much hand wringing) that the library did expose a data type that would allow us to use our own types &#8212; or, indeed, all of the standard Haskell types, such as pair. The type we need is</p>
<p><code>
<pre>data L a = L a
instance Unlifted (L a) where
  expose (L _) s = s
</pre>
<p></code></p>
<p>Now we have a wrapper data type that acts as a dummy instance of class <code>Unlifted</code>! So (after about four hours of head scratching), we can make the following small changes to our code:</p>
<p><code>
<pre>mergeS :: Ord a =&gt; Stream a -&gt; Stream a -&gt; Stream a
mergeS (Stream nexta sa0) (Stream nextb sb0) = Stream next <strong>(L (sa0, sb0))</strong>
    where
      next <strong>(L (sa0, sb0))</strong> =
          case (nexta sa0, nextb sb0) of
            (Done, sb) -&gt;
                case sb of
                  Done        -&gt; Done
                  Skip sb'    -&gt; Skip    <strong>(L (sa0, sb'))</strong>
                  Yield b sb' -&gt; Yield b <strong>(L (sa0, sb'))</strong>
            (sa, Done) -&gt;
                case sa of
                  Done        -&gt; Done
                  Skip sa'    -&gt; Skip    <strong>(L (sa', sb0))</strong>
                  Yield a sa' -&gt; Yield a <strong>(L (sa', sb0))</strong>
            (sa, sb) -&gt;
                case sa of
                  Done        -&gt; Done -- shouldn't happen
                  Skip sa'    -&gt; Skip    <strong>(L (sa', sb0))</strong>
                  Yield a sa' -&gt;
                      case sb of
                        Done                    -&gt; Done -- Shouldn't happen
                        Skip sb'                -&gt; Yield a <strong>(L (sa', sb'))</strong>
                        Yield b sb' | a &lt; b     -&gt; Yield a <strong>(L (sa', sb0))</strong>
                                    | otherwise -&gt; Yield b <strong>(L (sa0, sb'))</strong>
</pre>
<p></code></p>
<p>All we had to do was wrap our Stream pairs in the type constructor <code>L</code> to give our Stream pairs access to &#8220;free&#8221; instance deriving from the <code>Unlifted</code> class. Easy? Well, once you notice that unassuming <code>data L a = L a</code> in the documentation. But hey, it sure beats trying to do something like this in C!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mcclurmc.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mcclurmc.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mcclurmc.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mcclurmc.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mcclurmc.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mcclurmc.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mcclurmc.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mcclurmc.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mcclurmc.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mcclurmc.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mcclurmc.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mcclurmc.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mcclurmc.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mcclurmc.wordpress.com/72/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mcclurmc.wordpress.com&amp;blog=11098563&amp;post=72&amp;subd=mcclurmc&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mcclurmc.wordpress.com/2010/03/21/creating-stream-combinators/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4ea9afaf51f26a5f7171f950f6272f4d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mcclurmc</media:title>
		</media:content>
	</item>
		<item>
		<title>Java Concurrency Annotations</title>
		<link>http://mcclurmc.wordpress.com/2010/02/12/java-concurrency-annotations/</link>
		<comments>http://mcclurmc.wordpress.com/2010/02/12/java-concurrency-annotations/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 20:21:54 +0000</pubDate>
		<dc:creator>mcclurmc</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Thoughts]]></category>
		<category><![CDATA[Concurrency]]></category>
		<category><![CDATA[Contracts]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Types]]></category>

		<guid isPermaLink="false">http://mcclurmc.wordpress.com/?p=38</guid>
		<description><![CDATA[I&#8217;ve been reading a series of papers by Chandrasekhar Boyapati on extensions to the Java type system. I found his papers on ensuring race-free programs by specifying that objects are either immutable thread local, or referenced by a unique pointer. There&#8217;s also the paper A Type and Effect System for Deterministic Parallel Java, from OOPSLA [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mcclurmc.wordpress.com&amp;blog=11098563&amp;post=38&amp;subd=mcclurmc&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been reading a series of papers by <a href="http://www.citeulike.org/user/mcclurmc/author/Boyapati:C">Chandrasekhar Boyapati</a> on extensions to the Java type system. I found his papers on ensuring race-free programs by specifying that objects are either immutable thread local, or referenced by a unique pointer. There&#8217;s also the paper <a href="http://www.citeulike.org/user/mcclurmc/article/6658634">A Type and Effect System for Deterministic Parallel Java</a>, from OOPSLA 2009. I&#8217;m fascinated by the idea of creating a type system that could ease the burden of writing threaded program, and this seems like a really promising idea to me.</p>
<p>I&#8217;d like to combine this approach to concurrency with <a href="http://www.citeulike.org/user/mcclurmc/article/6658568">Hybrid Type Checking</a>. While I&#8217;d prefer to do as much at compile time as possible, I have a suspicion that we&#8217;ll always need to do some locking and unlocking at compile time, and that a system using both static types and runtime contracts might be our best bet.</p>
<p>I&#8217;m taking a stab at implementing these ideas in Java &#8212; but instead of modifying the compiler itself, I&#8217;ll be writing an annotation processor that will run before the compiler. The idea is that we could extend the Java type system using annotations. We could even go so far as to generate code at compile time that could do either runtime contract checking, or even lock and thread management. This is similar to the project <a href="http://sourceforge.net/projects/jdefprog/">Java Defensive Programming</a>. If I can get this project off the ground, I&#8217;ll have to see if Federico would like to incorporate some of my code in his project.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mcclurmc.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mcclurmc.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mcclurmc.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mcclurmc.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mcclurmc.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mcclurmc.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mcclurmc.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mcclurmc.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mcclurmc.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mcclurmc.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mcclurmc.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mcclurmc.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mcclurmc.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mcclurmc.wordpress.com/38/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mcclurmc.wordpress.com&amp;blog=11098563&amp;post=38&amp;subd=mcclurmc&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mcclurmc.wordpress.com/2010/02/12/java-concurrency-annotations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4ea9afaf51f26a5f7171f950f6272f4d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mcclurmc</media:title>
		</media:content>
	</item>
		<item>
		<title>XML for Resumes</title>
		<link>http://mcclurmc.wordpress.com/2010/02/09/xml-for-resumes/</link>
		<comments>http://mcclurmc.wordpress.com/2010/02/09/xml-for-resumes/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 00:01:53 +0000</pubDate>
		<dc:creator>mcclurmc</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Thoughts]]></category>
		<category><![CDATA[Semantic Web]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://mcclurmc.wordpress.com/?p=46</guid>
		<description><![CDATA[I hate writing resumes because there are no good tools for the job. I would prefer to use an open source word processor, such as Open Office, but companies often ask for MS Word format, which OO doesn&#8217;t do particularly well. I&#8217;ve thought about LaTeX, but unless you&#8217;re doing a lot of math that&#8217;s kind [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mcclurmc.wordpress.com&amp;blog=11098563&amp;post=46&amp;subd=mcclurmc&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I hate writing resumes because there are no good tools for the job. I would prefer to use an open source word processor, such as Open Office, but companies often ask for MS Word format, which OO doesn&#8217;t do particularly well. I&#8217;ve thought about LaTeX, but unless you&#8217;re doing a lot of math that&#8217;s kind of overkill. Besides, I want to be able to keep the content of my resume separate from the formatting of the resume. I also want good revision control, and I want to be able to select content for my resume based on the particular job I&#8217;m applying for (such as leaving off my DoD clearance from non-DoD job applications).</p>
<p>All of this sounds like a job for XML – though I&#8217;m sure some would find this more overkill than LaTeX. But with a simple XML language, or perhaps even XHTML with class and span tags, I think I can make this happen. There are already two open “standards” for the job, <a href="http://xmlresume.sourceforge.net/">XML Resume</a> and <a href="http://microformats.org/wiki/hresume">hResume</a> microformats, but I use the word “standards” very loosely. The XML Resume project on SourceForge hasn&#8217;t been updated since 2005, and I&#8217;m not convinced that the microformats solution completely solves the problem of keeping content and formatting orthogonal – though it gives us the great benefit of having a fully indexable online resume.</p>
<p>My idea is to use either XML Resume or hResume (or some bastard of the two) to write the content of the resume, but then use an OO plugin to import the resume language into OO, where we can use it&#8217;s great formatting tools. If I can get this rolling, we could also write a corresponding plugin for MS Word, so that I can send off resumes to Microsoft shops.</p>
<p>Now that I think about it, this is basically the model-view-controller pattern. The model is the XML representation of the resume, and all the different data points that you may want to include at some point in a real resume. The word processor is the view, where you can make your resume look as pretty as you want. The controller is the plugin that allows you to have your pretty resume template backed by data in the XML file – and, if the plugin does what I want it to do, display certain bullet points while hiding others, change the order of items, etc.</p>
<p>I&#8217;d really like to get this project started, but every time I think about it it&#8217;s because I need to work on my resume. And so I find myself in the programmer&#8217;s endless dilemma: do I build a tool to solve the problem for me, or do I just solve the problem? My resume is looking pretty good, so I hope I&#8217;ll find some time to work on this plugin.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mcclurmc.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mcclurmc.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mcclurmc.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mcclurmc.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mcclurmc.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mcclurmc.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mcclurmc.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mcclurmc.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mcclurmc.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mcclurmc.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mcclurmc.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mcclurmc.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mcclurmc.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mcclurmc.wordpress.com/46/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mcclurmc.wordpress.com&amp;blog=11098563&amp;post=46&amp;subd=mcclurmc&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mcclurmc.wordpress.com/2010/02/09/xml-for-resumes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4ea9afaf51f26a5f7171f950f6272f4d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mcclurmc</media:title>
		</media:content>
	</item>
		<item>
		<title>Emacs!</title>
		<link>http://mcclurmc.wordpress.com/2010/01/19/emacs/</link>
		<comments>http://mcclurmc.wordpress.com/2010/01/19/emacs/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 06:43:00 +0000</pubDate>
		<dc:creator>mcclurmc</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Emacs]]></category>
		<category><![CDATA[Lisp]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I love Emacs. I&#8217;ve been using it ever since I took my first programming language theory class in college. I had played around with it before then, but it took my professor&#8217;s recommendation and half a class worth of a tutorial and I was hooked. We studied Scheme in that class, which is why my [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mcclurmc.wordpress.com&amp;blog=11098563&amp;post=1&amp;subd=mcclurmc&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I love Emacs. I&#8217;ve been using it ever since I took my first programming language theory class in college. I had played around with it before then, but it took my professor&#8217;s recommendation and half a class worth of a tutorial and I was hooked.</p>
<p>We studied Scheme in that class, which is why my prof recommended that we all use Emacs. Scheme is a variant of Lisp, which is the language that Emacs is built around. And Elisp, as it&#8217;s called, is a great embedded language for customizing Emacs. You could think of it as Visual Basic for Applications, which is Microsoft&#8217;s language for customizing Office products, but much more simple and powerful to use (as long as parentheses don&#8217;t scare you).</p>
<p>I&#8217;ve only got one or two friends that might consider themselves to be Emacs gurus, and one of them wrote me the other day with a challenge. &#8220;I believe this problem is trivial,&#8221; he said, which is never a good sign, &#8220;but I&#8217;ve only had 20 minutes to think about and I&#8217;ve got to run.&#8221; He had been working on an Emacs library to help him configure a massive Ant build script. Part of his build process involved him logging into numerous test and dev machines, and he wanted to automate that in Emacs. He had written a list of interactive functions that would allow him to <tt>rlogin</tt> into each machine by typing <tt>M-x machine</tt> (<tt>M</tt> stands for <tt>Alt</tt>, or <tt>Meta</tt>, in Emacs, and is used to specify commands from the keyboard). These functions were each defined with syntax like the following:</p>
<p><code></p>
<pre>(defun machine1 ()
  (interactive)
  (rlogin "machine1" "*machine1*"))</pre>
<p></code> </p>
<p>This list of functions had grown quite a bit in the months since he implemented this feature, and he wanted to find a way to automatically define a function that would log him into a specified machine by simply typing <tt>M-x machine</tt>. Easy, right?</p>
<h2>A first attempt</h2>
<p>My first approach to this problem was to create an association list of functions and their names. We would create a single interactive function that would select and run the appropriate generated function.</p>
<div></div>
<p><code>
<pre>(defun my-login (fn)
  (interactive "sWhich function? ")
  (funcall
    (cdr (assoc (read fn) my-fnassoc)))))</pre>
<p></code></p>
<p>There are a few things going on in this function. The <tt>(interactive "sWhich function? ")</tt> statement tells Emacs that this function can be called interactively using the <tt>M-x my-login</tt> syntax. The strange string &#8220;sWhich function? &#8221; is just a format string telling Emacs to prompt the user for a string argument (the &#8220;s&#8221;) with the prompt &#8220;Which function? &#8220;. If this were a multivariate function, we would simply break each format string with a &#8220;\n&#8221; character in order to prompt for each of our arguments. The <tt>read</tt> function just turns our argument string into a symbol, and the call <tt>(cdr (assoc (read fn) fnassoc))</tt> looks the symbol up in the association list <tt>my-fnassoc</tt>. <tt>funcall</tt> executes the returned function. Now we just need to build <tt>my-fnassoc</tt>.</p>
<p><code>
<pre>(defun mkfunls (name)
  (let* ((host (prin1-to-string name))
         (buff (format "*%s*" host)))
    `(,name . (lambda () (rlogin ,host ,buff)))))</pre>
<p></code></p>
<p>This function creates a single association pair of the form <tt>(host . login-function)</tt>. Since our <tt>name</tt> parameter is a symbol, we need to convert it to a string using <tt>prin1-to-string</tt>. The Emacs <tt>rlogin</tt> command allows us to specify the name of the buffer in which we spawn the remote shell. We can use the  <tt>format</tt> function to create a standard buffer name for each <tt>rlogin</tt> session we create.</p>
<p>The final line of <tt>mkfunls</tt> takes advantage of Lisp&#8217;s backquoting feature. The syntax looks a little strange at first, but if you can put up with Lisp&#8217;s other oddities then there&#8217;s no sense not learning about backquoting too. Quoting in Lisp, accomplished with either the <tt>quote</tt> special form or the abbreviation <tt>'</tt> (a single apostrophe). Quoting prevents evaluation of the form being quoted, so evaluating <code>(+ 1 2)</code> returns the value 3, while evaluating <code>(quote (+ 1 2))</code>, or the equivalent <code>'(+ 1 2)</code>, returns the value <code>(+ 1 2)</code> instead.</p>
<p>Backquoting accomplishes the same thing, but allows the programmer to  selectively specify which parts of a list are to be evaluated and which parts aren&#8217;t. It&#8217;s called backquoting because we use a backwards apostrophe (<code>`</code> &#8212; found under the tilde) instead of <code>quote</code> or the regular apostrophe. We select the forms we want to evaluate using a comma. So the line <code>`(,name . (lambda () (rlogin ,host ,buff)))</code> evaluates the <code>name</code>, <code>host</code>, and <code>buff</code> variables but quotes all the rest of the symbols. So the call <code>(mkfunls 'machine1)</code> evaluates to <code>(machine1 lambda nil (rlogin "machine1" "*machine1*"))</code>. Now all that&#8217;s left is to create our host/rlogin association list and we&#8217;ll be able to log in to remote hosts with a single command.</p>
<p><code>(setq my-fnassoc (mapcar 'mkfunls '(m1 m2 m3)))</code></p>
<p>This creates an association list with the host names <code>m1, m2, m3</code> and sets the variable <code>my-fnassoc</code>. The function <code>mapcar</code> applies it&#8217;s first argument, in the case the function <code>mkfunls</code>, to it&#8217;s second argument, which must be a list.</p>
<p>Now when we type <code>M-x my-login</code> we receive the prompt &#8220;Which host?&#8221;, to which we can reply with any of the hosts we specified in the <code>my-fnassoc</code> list above.</p>
<p>But this doesn&#8217;t quite solve my buddy&#8217;s problem, does it? His list of login functions, while a little cumbersome to maintain, defined interactive functions at the top level, which allowed him to run <code>M-x machine1</code> instead of the more indirect call to <code>M-x my-login</code> above.</p>
<h2>A better solution</h2>
<p>What we want is to automatically generate named, interactive functions. My initial thought was that this was a perfect task for macros, but it turns out that we don&#8217;t even have to bother with macros to make this work. Instead, we&#8217;ll use the backquoting syntax from above to build up <code>defuns</code> and then evaluate them.</p>
<p><code>
<pre>(defun mkdefun (name)
  (let* ((host (prin1-to-string name))
         (buff (format "*%s*" host)))
    (eval
     `(defun ,name () (interactive) (rlogin ,host ,buff)))))</pre>
<p></code></p>
<p>This is very similar to our <code>mkfunls</code> function above. Instead of returning an association list, we&#8217;re evaluating a <code>defun</code>. We use the backquote syntax to selectively evaluate the <code>name</code>, <code>host</code>, and <code>buff</code> variables. <code>eval</code> then evaluates the <code>defun</code> for us and creates a new top level definition. We can then map over a list of function names like we did before:</p>
<p><code>(mapc 'mkdefun '(m1 m2 m3))</code></p>
<p><code>mapc</code> is like <code>mapcar</code>, except that it doesn&#8217;t return the a list of the results of the function evaluations. This is useful when we only care about a function&#8217;s side effect and not its results.</p>
<p>So now we have a simple method to automatically generate repetitive Emacs commands. All we have to do is append new function names to the list in the <code>mapc</code> command and we have a new function!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mcclurmc.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mcclurmc.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mcclurmc.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mcclurmc.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mcclurmc.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mcclurmc.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mcclurmc.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mcclurmc.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mcclurmc.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mcclurmc.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mcclurmc.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mcclurmc.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mcclurmc.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mcclurmc.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mcclurmc.wordpress.com&amp;blog=11098563&amp;post=1&amp;subd=mcclurmc&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mcclurmc.wordpress.com/2010/01/19/emacs/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4ea9afaf51f26a5f7171f950f6272f4d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mcclurmc</media:title>
		</media:content>
	</item>
	</channel>
</rss>
