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

<channel>
	<title>Artful Code &#187; lisp</title>
	<atom:link href="http://www.artfulcode.net/tags/lisp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.artfulcode.net</link>
	<description>Resources and tips for dynamic, interactive languages.</description>
	<lastBuildDate>Fri, 09 Sep 2011 02:15:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>In defense of newLISP</title>
		<link>http://www.artfulcode.net/articles/in-defense-newlisp/</link>
		<comments>http://www.artfulcode.net/articles/in-defense-newlisp/#comments</comments>
		<pubDate>Tue, 15 Jul 2008 15:25:51 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Soap box]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[newlisp]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/in-defense-newlisp/</guid>
		<description><![CDATA[newLISP receives an unexpected level of hostility from lispers. Languages like Arc and newLISP share an enmity that stems from the assumption that these languages are in some way attempting to replace Common Lisp. This is not the case. It should be pointed out that newLISP is an interpreted language with primary emphases on low [...]]]></description>
			<content:encoded><![CDATA[<p>newLISP receives an unexpected level of hostility from lispers.  Languages like Arc and newLISP share an enmity that stems from the assumption that these languages are in some way attempting to replace Common Lisp.  This is not the case.<span id="more-19"></span></p>
<p>It should be pointed out that newLISP is an interpreted language with primary emphases on low memory usage, short start-up time, and efficient implementation.  In that, newLISP is exceptionally successful; the <a href="http://www.newlisp.org/downloads/newlisp_manual.html#functions">amount of functionality</a> that has been packed into an executable of just over 250 Kb is impressive.  Automatic memory management is <a href="http://www.newlisp.org/MemoryManagement.html">fast and simple</a> and requires little overhead.  In terms of <a href="http://www.newlisp.org/benchmarks/">speed</a>, newLISP compares favorably to other interpreted languages (e.g. Perl and Python).</p>
<p>It also needs to be mentioned that newLISP is <em>not</em> Common Lisp.  The name newLISP seems to indicate that newLISP modernizes and replaces Common Lisp.  The <a href="http://www.newlisp.org/index.cgi?FAQ">FAQ</a> on newLISP.org does take a decidedly populist tone with regard to other lisps.  This is unfortunate, because newLISP is a very <a href="http://www.newlisp.org/index.cgi?page=Differences_to_Other_LISPs">different language</a> than CL or Scheme and is not truly in competition with them.</p>
<p>I&#8217;d like to discuss of a few of the more significant sticking points for potential users.</p>
<h4>Macros</h4>
<p>newLISP is a purely interpreted language.  This has some important implications in its implementation.  Macros are disimilar to compiled lisps.  Most importantly, there is no compile-time expansion.  All expansion is performed at runtime and with the overhead of a function all.  In fact, newLISP macros are more like functions with lazy evaluation.</p>
<p>However, this does not completely diminish the power of newLISP macros.  newLISP&#8217;s operator, <code>letex</code>, may be used to both expand and evaluate a block of code.  Using the <code>args</code> function and <a href="file://localhost/usr/share/doc/newlisp/newlisp_manual.html#indexing">implicit indexing</a>, variable capture may be avoided in macros.  Here is an extremely basic macro to iterate over the lines of a file using both <code>letex</code> and <code>args</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define-macro <span style="color: #66cc66;">&#40;</span>dolines<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>letex <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>var <span style="color: #66cc66;">&#40;</span>args <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                    <span style="color: #66cc66;">&#40;</span>file <span style="color: #66cc66;">&#40;</span>open <span style="color: #66cc66;">&#40;</span>args <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;read&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                    <span style="color: #66cc66;">&#40;</span>body <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> 'begin <span style="color: #66cc66;">&#40;</span>rest <span style="color: #66cc66;">&#40;</span>args<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>while <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> 'var <span style="color: #66cc66;">&#40;</span>read-line file<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            body<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>close file<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>dolines <span style="color: #66cc66;">&#40;</span>line <span style="color: #ff0000;">&quot;/path/to/file&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>println line<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h4>Variable capture</h4>
<p>This is a real issue when writing newLISP.  The function <code>args</code> can prevent capture by functions&#8217; parameters, but there is no equivalent to Common Lisp&#8217;s <code>gensym</code> to dynamically create unique symbols.  Fortunately, it is not difficult to write our own:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define _gensym<span style="color: #66cc66;">:</span>_gensym<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">gensym</span><span style="color: #66cc66;">:</span><span style="color: #b1b100;">gensym</span> <span style="color: #66cc66;">&#40;</span>ctx MAIN<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>_gensym <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">name</span> ctx<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>new-sym <span style="color: #66cc66;">&#40;</span>string <span style="color: #ff0000;">&quot;gensym-&quot;</span> <span style="color: #66cc66;">&#40;</span>_gensym <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">name</span> ctx<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>+ <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#40;</span>_gensym <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">name</span> ctx<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">or</span> <span style="color: #66cc66;">&#40;</span>sym new-sym ctx<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">gensym</span> ctx<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span>begin
                <span style="color: #66cc66;">&#40;</span>_gensym <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">name</span> ctx<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">gensym</span> ctx<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This demonstrates two other features of newLISP, contexts and dictionaries, which I will go into in more detail shortly.  For now, it will suffice to explain that contexts are lexical namespaces (context symbols are typically denoted in the form <code>context:symbol</code>, similarly to CL packages) and dictionaries are a way to use contexts as an associative namespace.</p>
<p>Note that this implementation of gensym is dependent on the maximum integer value on the system.  newLISP will wrap into negatives after the max integer, so the maximum number of symbols possible to create is generally twice the value of the max integer.  However, symbols that have been deleted will be recycled.</p>
<h4>Hash tables</h4>
<p>newLISP does not use hash tables (see <a href="http://enfranchisedmind.com/blog/2008/02/25/problems-with-hash-tables/">here</a> for an explanation of why).  This is a show-stopper for many.  Without joining the (often heated) debate over this, newLISP does provide the same functionality as a hash through its dictionaries:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define dict<span style="color: #66cc66;">:</span><span style="color: #555;">dict</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; create the dictionary</span>
<span style="color: #66cc66;">&#40;</span>dict <span style="color: #ff0000;">&quot;foo&quot;</span> <span style="color: #ff0000;">&quot;bar&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; associates &quot;foo&quot; to &quot;bar&quot; in dict</span>
<span style="color: #66cc66;">&#40;</span>dict <span style="color: #ff0000;">&quot;foo&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; retrieves &quot;foo&quot; from dict</span></pre></div></div>

<p>Or, programmatically:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>context dict <span style="color: #ff0000;">&quot;foo&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; =&gt; &quot;bar&quot;</span>
<span style="color: #66cc66;">&#40;</span>context dict <span style="color: #ff0000;">&quot;foo&quot;</span> <span style="color: #ff0000;">&quot;baz&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; sets foo:bar &quot;baz&quot;</span></pre></div></div>

<h4>Dynamic scope</h4>
<p>newLISP is dynamically scoped.  Because it is an interpreted language, much computation and memory is saved by avoiding the complexities of lexical scoping.</p>
<p>newLISP&#8217;s contexts do implement static scopes that can be used to create lexically enclosed modules and closures.  Within the context, though, variable lookups are dynamic to the context.  In the example above (gensym), <code>_gensym</code> is a context dictionary and <code>gensym</code> is a default functor (a function assigned to a symbol with the same name as its context is defined as a functor in newLISP).  Within <code>gensym:gensym</code>, scope is dynamic to the gensym namespace, which is lexically isolated from the default namespace, labeled MAIN.</p>
<h4>Garbage collection</h4>
<p>newLISP uses a form of memory management called <a href="http://www.newlisp.org/MemoryManagement.html">ORO</a> (one reference only).  From the newLISP documentation:</p>
<blockquote><p>Memory management in newLISP does not rely on a garbage collection algorithm. Memory is not marked or reference-counted. Instead, a decision whether to delete a newly created memory object is made right after the memory object is created.</p>
<p>Empirical studies of LISP have shown that most LISP cells are not shared and so can be reclaimed during the evaluation process. Aside from some optimizations for primitives like set, define, and eval, newLISP deletes memory objects containing intermediate evaluation results once it reaches a higher evaluation level. newLISP does this by pushing a reference to each created memory object onto a result stack. When newLISP reaches a higher evaluation level, it removes the last evaluation result&#8217;s reference from the result stack and deletes the evaluation result&#8217;s memory object. This should not be confused with one-bit reference counting. ORO memory management does not set bits to mark objects as sticky.</p>
<p>newLISP follows a one reference only (ORO) rule. Every memory object not referenced by a symbol or context reference is obsolete once newLISP reaches a higher evaluation level during expression evaluation. Objects in newLISP (excluding symbols and contexts) are passed by value to other functions. As a result, each newLISP object only requires one reference.</p></blockquote>
<p>The intuitive assumption is that this results in slow evaluation, but it does not.  Nor is newLISP&#8217;s evaluation speed burdened by a garbage collector.</p>
<blockquote><p>newLISP&#8217;s ORO rule forces newLISP to constantly allocate and then free LISP cells. newLISP optimizes this process by allocating large chunks of cell memory from the host operating system. newLISP will request LISP cells from a free cell list and then recycle those cells back into that list. As a result, only a few CPU instructions (pointer assignments) are needed to unlink a free cell or to re-insert a deleted cell.</p>
<p>The overall effect of ORO memory management is a faster evaluation time and a smaller memory and disk footprint than traditional interpreted LISP&#8217;s can offer. The lack of garbage collection in newLISP more than compensates for its high frequency of cell creation/deletion. Note that under error conditions, newLISP will employ a mark and sweep algorithm to free un-referenced cells.</p></blockquote>
<h4>Implicit indexing</h4>
<p>Implicit indexing is syntactic sugar for indexed access to elements in a list:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>lst '<span style="color: #66cc66;">&#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: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>lst <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #cc66cc;">4</span></pre></div></div>

<p>It also works for nested lists:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>lst '<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>lst <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #cc66cc;">5</span></pre></div></div>

<p>Prospective users often remark that this syntax breaks conventional semantics and makes it far more difficult to do meta-programming.  The function <code>nth</code> exists in the language and may still be used for this purpose:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>lst '<span style="color: #66cc66;">&#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: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nth</span> <span style="color: #cc66cc;">3</span> lst<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #cc66cc;">4</span></pre></div></div>

<h4>Concurrency and distributed computing</h4>
<p>newLISP is not threaded.  This in particular was a challenge for me.  However, I have found that due to newLISP&#8217;s small size and quick start-up, forking new processes is quite painless.  There are few programs where one would want to start hundreds or thousands of threads, and newLISP is able to launch a large number of processes in less space than a single instance of the Python interpreter.</p>
<p>Using the new <a href="http://supertech.csail.mit.edu/cilk/">Cilk</a>-inspired <a href="http://www.newlisp.org/downloads/newlisp_manual.html#multi_processing">API</a>, concurrent programming is simple, cheap, and expressive.  Additionally, there are few of the challenges associated with threaded programming.</p>
<p>newLISP also comes with <a href="http://www.newlisp.org/downloads/newlisp_manual.html#net-eval">built-in functions</a> for distributed computing, permitting forms to be easily sent to other nodes over TCP/IP for evaluation:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>net-<span style="color: #b1b100;">eval</span> remote-server-ip expr-to-evaluate timeout response-handler<span style="color: #66cc66;">&#41;</span></pre></div></div>

<h4>Conclusion</h4>
<p>newLISP packs a lot of power into its small size.  Some of its more powerful built-in features include:</p>
<ul>
<li> a fast, simple concurrency API</li>
<li> regular expressions (PCRE)</li>
<li> native C library access</li>
<li> XML parsing</li>
<li> pattern matching</li>
<li> HTTP networking</li>
<li> sockets</li>
<li> cross platform GUI server (using Java)</li>
<li> bayesian training and probability analysis</li>
<li> cross platform support &#8211; newLISP uses only standard libraries</li>
</ul>
<p>newLISP does not deserve the pariah status to which many relegate it. While newLISP cannot replace a compiled language for the most intensive tasks, it remains a fun, <em>artful</em> language and is excellent for exploratory programming and rapid prototyping, while remaining fast and powerful enough for the final product.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fin-defense-newlisp%2F&amp;title=In+defense+of+newLISP" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fin-defense-newlisp%2F&amp;title=In+defense+of+newLISP" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=In+defense+of+newLISP&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fin-defense-newlisp%2F&amp;title=In+defense+of+newLISP" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fin-defense-newlisp%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fin-defense-newlisp%2F&amp;title=In+defense+of+newLISP" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fin-defense-newlisp%2F&amp;title=In+defense+of+newLISP" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fin-defense-newlisp%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+In+defense+of+newLISP+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fin-defense-newlisp%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/in-defense-newlisp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Profiling a Lisp application</title>
		<link>http://www.artfulcode.net/articles/profiling-lisp-application/</link>
		<comments>http://www.artfulcode.net/articles/profiling-lisp-application/#comments</comments>
		<pubDate>Tue, 01 Jul 2008 21:28:38 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[emacs]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[slime]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/profiling-lisp-application/</guid>
		<description><![CDATA[Profiling is one of the most important steps in writing software. Once a package is written, profiling greatly helps to identify bottlenecks and inefficiencies by showing how an application spends its time. How to profile a lisp application is a common question among those new to lisp. Luckily, profiling lisp is simple with emacs and [...]]]></description>
			<content:encoded><![CDATA[<p>Profiling is one of the most important steps in writing software.  Once a package is written, profiling greatly helps to identify bottlenecks and inefficiencies by showing how an application spends its time.  How to profile a lisp application is a common question among those new to lisp.  Luckily, profiling lisp is simple with emacs and <a href="http://common-lisp.net/project/slime/">slime</a>.<span id="more-22"></span></p>
<p>There are several slime profiling functions, but the simplest is <code>slime-profile-package</code> (<code>M-x slime-profile-package</code>), which profiles all functions in a package.  You will be asked for the package name to profile.  All functions in the package will be profiled. <code>slime-profile-functions</code> gives you more control over which functions to profile.  The profiling of a single function can be controlled with <code>slime-toggle-profile-fdefinition</code>.</p>
<p>After entering the package name, you are asked whether to record the most common callers and whether to profile methods as well as typical functions.  To view the results, you use <code>slime-profile-report</code>, which displays a table of the most commonly called functions, time spent in each, amount of consing in each, calls, total time, etc.</p>
<p>Before the next run, be sure to use <code>M-x slime-profile-reset</code> to reset the profiling statistics so you get a fresh and accurate report.</p>
<h3>Optimizing functions</h3>
<p>Generally, you should attempt to write a more efficient algorithm before depending on compiler optimization declarations.  The slime profiler will tell you which functions are taking the majority of your application&#8217;s time, and making these more efficient will serve you better than tuning the manner in which a function is compiled.  However, once your function is written to your satisfaction, you may wish to further tune it by sacrificing speed for lower memory usage (or vice versa) or trading debug information for extra speed.</p>
<p>Lisp compilers accept a few optimization declarations that can earn you significant speed-ups.  The compiler generally optimizes code based on four variables: speed, safety, debug, and compilation-speed.  These can be set between 0 and 3 with a declaration:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> foo <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#40;</span>declare <span style="color: #66cc66;">&#40;</span>optimize <span style="color: #66cc66;">&#40;</span>speed <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
                        <span style="color: #66cc66;">&#40;</span>safety <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>
                        <span style="color: #66cc66;">&#40;</span>debug <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>
                        <span style="color: #66cc66;">&#40;</span>compilation-speed <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;~&amp;amp;Hello world~%&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Generally, lowering the debug value of a function makes trouble-shooting your application extremely difficult.  For example, the name of the function may disappear from backtraces.  Therefore, lowering this value is usually only worthwhile once the program has been suitably tested and deployed.  Use the profiler to determine which portions of your code will provide the most benefit.</p>
<h4>Macros and optimization</h4>
<p>Macros are sometimes touted as a way to optimize code.  In a compiled lisp program, they can cause some calculations and expansions to be performed at compile time, saving run-time.  In situations where much of the information is known ahead of time, this can be an effective technique, although overuse can be dangerous.  For example, rewriting a function as a macro will make debugging more difficult, since the macro is expanded in the compiled code.  A problem in a macro may only be shown as a problem in the function that utilizes the macro.</p>
<p>Note that in interpreted lisps, especially those without any form of compilation (such as newLISP), macros provide no speed-up.  In fact, in newLISP, macros are just functions that do not evaluate their arguments.</p>
<h4>More info</h4>
<p><a href="http://common-lisp.net/project/slime/doc/html/Profiling.html#Profiling">slime profiling documentation</a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fprofiling-lisp-application%2F&amp;title=Profiling+a+Lisp+application" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fprofiling-lisp-application%2F&amp;title=Profiling+a+Lisp+application" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Profiling+a+Lisp+application&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fprofiling-lisp-application%2F&amp;title=Profiling+a+Lisp+application" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fprofiling-lisp-application%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fprofiling-lisp-application%2F&amp;title=Profiling+a+Lisp+application" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fprofiling-lisp-application%2F&amp;title=Profiling+a+Lisp+application" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fprofiling-lisp-application%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Profiling+a+Lisp+application+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fprofiling-lisp-application%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/profiling-lisp-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create/update function for CLSQL</title>
		<link>http://www.artfulcode.net/articles/create-update-function-clsql/</link>
		<comments>http://www.artfulcode.net/articles/create-update-function-clsql/#comments</comments>
		<pubDate>Fri, 09 May 2008 19:51:32 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[clsql]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/create-update-function-clsql/</guid>
		<description><![CDATA[Django has a very handy model method in its database library called get_or_create, which either creates a new record using the keyword arguments passed as field values or gets a record using the arguments passed. from models import Person person = Person.objects.get_or_create&#40;first_name=&#34;John&#34;, last_name=&#34;Johnson&#34;&#41; It adds syntactic sugar to the common case of checking if an [...]]]></description>
			<content:encoded><![CDATA[<p>Django has a very handy model method in its database library called get_or_create, which either creates a new record using the keyword arguments passed as field values or gets a record using the arguments passed.<span id="more-25"></span></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> models <span style="color: #ff7700;font-weight:bold;">import</span> Person
person = Person.<span style="color: black;">objects</span>.<span style="color: black;">get_or_create</span><span style="color: black;">&#40;</span>first_name=<span style="color: #483d8b;">&quot;John&quot;</span>, last_name=<span style="color: #483d8b;">&quot;Johnson&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>It adds syntactic sugar to the common case of checking if an entry exists, creating it if it does not, then updating (or creating with new) values.  The problem is that its <em>slow</em>.</p>
<p>I offload our most expensive database work on our server to a Lisp-based XML RPC server using <a href="http://clsql.b9.com/">CLSQL</a>.  I wrote an analog of get_or_create for CLSQL.  It does not have the advantage of Django&#8217;s keyword searches; all searches use <code>=</code> and pull the first row that matches the value.</p>
<p>The first function, <code>get-instance</code> is a helper to get a row in a view-class using a field/value pair.<br />
<code>create/update</code> takes the first three arguments and passes them to <code>get-instance</code> to get any existing match.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> get-instance <span style="color: #66cc66;">&#40;</span>cls pk val<span style="color: #66cc66;">&#41;</span>
  #<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#40;</span>clsql<span style="color: #66cc66;">:</span><span style="color: #555;">locally-enable-sql-reader-syntax</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>inst <span style="color: #66cc66;">&#40;</span>clsql<span style="color: #66cc66;">:</span><span style="color: #555;">select</span> cls <span style="color: #66cc66;">:</span><span style="color: #555;">flatp</span> t <span style="color: #66cc66;">:</span><span style="color: #555;">refresh</span> t <span style="color: #66cc66;">:</span><span style="color: #555;">limit</span> <span style="color: #cc66cc;">1</span>
                            <span style="color: #66cc66;">:</span><span style="color: #555;">where</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>clsql<span style="color: #66cc66;">:</span><span style="color: #555;">sql-expression</span> <span style="color: #66cc66;">:</span><span style="color: #555;">attribute</span> pk<span style="color: #66cc66;">&#41;</span> val<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    #<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#40;</span>clsql<span style="color: #66cc66;">:</span><span style="color: #555;">restore-sql-reader-syntax-state</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> inst <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> inst<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> create/update <span style="color: #66cc66;">&#40;</span>cls pk val <span style="color: #66cc66;">&amp;</span>rest args<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>inst <span style="color: #66cc66;">&#40;</span>get-instance cls pk val<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> inst
            <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">apply</span> #'reinitialize-instance inst args<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">apply</span> #'make-instance cls pk val args<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h4>Usage:</h4>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>clsql<span style="color: #66cc66;">:</span><span style="color: #555;">def-view-class</span> person <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>id
        <span style="color: #66cc66;">:</span><span style="color: #555;">reader</span> person-id
        <span style="color: #66cc66;">:</span><span style="color: #555;">db-kind</span> <span style="color: #66cc66;">:</span><span style="color: #555;">key</span>
        <span style="color: #66cc66;">:</span><span style="color: #555;">db-constraints</span> <span style="color: #66cc66;">:</span><span style="color: #555;">not-</span><span style="color: #b1b100;">null</span>
        <span style="color: #66cc66;">:</span><span style="color: #555;">type</span> <span style="color: #b1b100;">integer</span>
        <span style="color: #66cc66;">:</span><span style="color: #555;">initarg</span> <span style="color: #66cc66;">:</span><span style="color: #555;">id</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>last-<span style="color: #b1b100;">name</span>
        <span style="color: #66cc66;">:</span><span style="color: #555;">accessor</span> person-last-<span style="color: #b1b100;">name</span>
        <span style="color: #66cc66;">:</span><span style="color: #555;">db-constraints</span> <span style="color: #66cc66;">:</span><span style="color: #555;">not-</span><span style="color: #b1b100;">null</span>
        <span style="color: #66cc66;">:</span><span style="color: #555;">type</span> <span style="color: #66cc66;">&#40;</span>clsql<span style="color: #66cc66;">:</span><span style="color: #555;">varchar</span> <span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">:</span><span style="color: #555;">initarg</span> <span style="color: #66cc66;">:</span><span style="color: #555;">last-</span><span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>first-<span style="color: #b1b100;">name</span>
        <span style="color: #66cc66;">:</span><span style="color: #555;">accessor</span> person-first-<span style="color: #b1b100;">name</span>
        <span style="color: #66cc66;">:</span><span style="color: #555;">db-constraints</span> <span style="color: #66cc66;">:</span><span style="color: #555;">not-</span><span style="color: #b1b100;">null</span>
        <span style="color: #66cc66;">:</span><span style="color: #555;">type</span> <span style="color: #66cc66;">&#40;</span>clsql<span style="color: #66cc66;">:</span><span style="color: #555;">varchar</span> <span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">:</span><span style="color: #555;">initarg</span> <span style="color: #66cc66;">:</span><span style="color: #555;">first-</span><span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defvar john <span style="color: #66cc66;">&#40;</span>create/update 'person <span style="color: #66cc66;">:</span><span style="color: #555;">id</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>If there is row with id 3 in the database, it will grab that (or, if the key is not unique, it will grab the first matching row.)  If there is no entry with that id, it will create a new one, that can then be updated the normal way:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">&#40;</span>person-last-<span style="color: #b1b100;">name</span> john<span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;John&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">&#40;</span>person-first-<span style="color: #b1b100;">name</span> john<span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;Johnson&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The other alternative is to get and set all in one go:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defvar john <span style="color: #66cc66;">&#40;</span>create/update 'person
                            <span style="color: #66cc66;">:</span><span style="color: #555;">id</span> <span style="color: #cc66cc;">3</span>
                            <span style="color: #66cc66;">:</span><span style="color: #555;">last-</span><span style="color: #b1b100;">name</span> <span style="color: #ff0000;">&quot;Johnson&quot;</span>
                            <span style="color: #66cc66;">:</span><span style="color: #555;">first-</span><span style="color: #b1b100;">name</span> <span style="color: #ff0000;">&quot;John&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>It&#8217;s pretty simple but it is very useful.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcreate-update-function-clsql%2F&amp;title=Create%2Fupdate+function+for+CLSQL" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcreate-update-function-clsql%2F&amp;title=Create%2Fupdate+function+for+CLSQL" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Create%2Fupdate+function+for+CLSQL&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcreate-update-function-clsql%2F&amp;title=Create%2Fupdate+function+for+CLSQL" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcreate-update-function-clsql%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcreate-update-function-clsql%2F&amp;title=Create%2Fupdate+function+for+CLSQL" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcreate-update-function-clsql%2F&amp;title=Create%2Fupdate+function+for+CLSQL" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcreate-update-function-clsql%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Create%2Fupdate+function+for+CLSQL+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcreate-update-function-clsql%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/create-update-function-clsql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Variable-arity functions in Lisp</title>
		<link>http://www.artfulcode.net/articles/variable-arity-functions-lisp/</link>
		<comments>http://www.artfulcode.net/articles/variable-arity-functions-lisp/#comments</comments>
		<pubDate>Wed, 27 Feb 2008 20:36:00 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[newlisp]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/variable-arity-functions-lisp/</guid>
		<description><![CDATA[One of the handier features of modern functional languages like Erlang and OCaml is the use of unification to match arity against function definitions. The ability to define a function in terms of the type and number of arguments passed is both expressive and useful. Common Lisp&#8217;s destructuring-bind uses unification to bind local variables, much [...]]]></description>
			<content:encoded><![CDATA[<p>One of the handier features of modern functional languages like Erlang and OCaml is the use of unification to match arity against function definitions.  The ability to define a function in terms of the type and number of arguments passed is both expressive and useful.<span id="more-31"></span></p>
<p>Common Lisp&#8217;s <code>destructuring-bind</code> uses unification to bind local variables, much like <code>let</code>.  Given a list of arbitrary depth, <code>destructuring-bind</code> can &#8220;extract&#8221; its elements and bind them in a lexical block.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> lst '<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;foo&quot;</span> <span style="color: #ff0000;">&quot;bar&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;baz&quot;</span> <span style="color: #ff0000;">&quot;bat&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>destructuring-bind <span style="color: #66cc66;">&#40;</span>a b c<span style="color: #66cc66;">&#41;</span> lst
    <span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;~A, ~A, and ~A&quot;</span> a b c<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">; =&gt; foo, bar, and (baz bat)</span></pre></div></div>

<p>newLISP has a unification function and a bind function that can do much the same thing.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> 'lst '<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;foo&quot;</span> <span style="color: #ff0000;">&quot;bar&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;baz&quot;</span> <span style="color: #ff0000;">&quot;bat&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>bind <span style="color: #66cc66;">&#40;</span>unify '<span style="color: #66cc66;">&#40;</span>A B C<span style="color: #66cc66;">&#41;</span> lst<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>println A <span style="color: #ff0000;">&quot;, &quot;</span> B <span style="color: #ff0000;">&quot;, and &quot;</span> C<span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">; =&gt; foo, bar, and (&quot;baz&quot; &quot;bat&quot;)</span></pre></div></div>

<p>Unfortunately, attempting to use a unification in a local scope does not work so easily:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span>unify '<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;foo&quot;</span> <span style="color: #ff0000;">&quot;bar&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;baz&quot;</span> <span style="color: #ff0000;">&quot;bat&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>println A <span style="color: #ff0000;">&quot;, &quot;</span> B <span style="color: #ff0000;">&quot;, and &quot;</span> C<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">; =&gt; symbol is protected in function let : unify</span></pre></div></div>

<p>This can be worked around with a macro and <code>letex</code>, which provides similar functionality to Common Lisp&#8217;s macro template syntax (except that it uses quoted, locally bound symbols, rather than backticks to insert values):</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define-macro <span style="color: #66cc66;">&#40;</span>destructuring-bind<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>letex <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>unifier <span style="color: #66cc66;">&#40;</span>args <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span>target <span style="color: #66cc66;">&#40;</span>args <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span>body <span style="color: #66cc66;">&#40;</span>rest <span style="color: #66cc66;">&#40;</span>rest <span style="color: #66cc66;">&#40;</span>args<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> unifier
      <span style="color: #66cc66;">&#40;</span>bind <span style="color: #66cc66;">&#40;</span>unify 'unifier 'target<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">dolist</span> <span style="color: #66cc66;">&#40;</span>expr 'body<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eval</span> expr<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This is a reasonable approximation of the CL <code>destructuring-bind</code> macro, although don&#8217;t try and pass <code>(args)</code> directly to it (this is a limitation of macros in newLISP; <code>(args)</code> would evaluate to a list of <code>destructuring-macro</code>&#8216;s arguments, not the calling function&#8217;s.</p>
<p>This implementation locally initializes the variables to be unified to <code>nil</code> and then binds them to the result of <code>unify</code>.  Once bound, it iterates over the expressions in the body of the form.</p>
<p><code>cond</code> can be used to match against various patterns of arguments passed to a function:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>foo<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>unify '<span style="color: #66cc66;">&#40;</span>A B C<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>args<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">do</span> something<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>unify '<span style="color: #66cc66;">&#40;</span>A B<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>args<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">do</span> something else<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This makes routing of different arities possible, but not convenient.  With a couple of macros, we can go a step further and create a <code>destructuring-case</code> conditional that accepts the target list for unification and a series of conditionals, as with <code>cond</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define-macro <span style="color: #66cc66;">&#40;</span>cond-form<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>letex <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>target <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eval</span> <span style="color: #66cc66;">&#40;</span>args <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span>unifier <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nth</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eval</span> <span style="color: #66cc66;">&#40;</span>args <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span>body <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nth</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eval</span> <span style="color: #66cc66;">&#40;</span>args <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
         '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>unify 'unifier 'target<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span>destructuring-bind unifier target
              body<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>define-macro <span style="color: #66cc66;">&#40;</span>destructuring-<span style="color: #b1b100;">case</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>letex <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>target <span style="color: #66cc66;">&#40;</span>first <span style="color: #66cc66;">&#40;</span>args<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>conditions <span style="color: #66cc66;">&#40;</span>rest <span style="color: #66cc66;">&#40;</span>args<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eval</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> <span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span>map <span style="color: #66cc66;">&#40;</span>fn <span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>cond-form target c<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                               'conditions<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> 'lst '<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;foo&quot;</span> <span style="color: #ff0000;">&quot;bar&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;baz&quot;</span> <span style="color: #ff0000;">&quot;bat&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>destructuring-<span style="color: #b1b100;">case</span> lst
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>A B C<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>println A <span style="color: #ff0000;">&quot;, &quot;</span> B <span style="color: #ff0000;">&quot;, and &quot;</span> C<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>A B<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>println A <span style="color: #ff0000;">&quot; and &quot;</span> B<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>A<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>println A<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">; =&gt; foo, bar, and (&quot;baz&quot; &quot;bat&quot;)</span></pre></div></div>

<p>Although this does not permit a function to be defined multiple times in terms of its arity, it does enable equivalent functionality, albeit within the function body:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>test<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>params <span style="color: #66cc66;">&#40;</span>args<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>destructuring-<span style="color: #b1b100;">case</span> params
      <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>A B C<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>println <span style="color: #ff0000;">&quot;Case 1: &quot;</span> A <span style="color: #ff0000;">&quot;, &quot;</span> B <span style="color: #ff0000;">&quot;, and &quot;</span> C<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>A B<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>println <span style="color: #ff0000;">&quot;Case 2: &quot;</span> A <span style="color: #ff0000;">&quot; and &quot;</span> B<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>A<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>println <span style="color: #ff0000;">&quot;Case 3: &quot;</span> A<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>test <span style="color: #ff0000;">&quot;foo&quot;</span> <span style="color: #ff0000;">&quot;bar&quot;</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;baz&quot;</span> <span style="color: #ff0000;">&quot;bat&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">; =&gt; foo, bar, and (&quot;baz&quot; &quot;bat&quot;)</span></pre></div></div>

<p>Links:</p>
<ul>
<li> <a href="http://en.wikipedia.org/wiki/Unification">unification</a></li>
<li> <a href="http://www.newlisp.org/downloads/newlisp_manual.html#unify">unify (newLISP)</a></li>
<li> <a href="http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node100.html">destructuring-bind</a></li>
</ul>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fvariable-arity-functions-lisp%2F&amp;title=Variable-arity+functions+in+Lisp" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fvariable-arity-functions-lisp%2F&amp;title=Variable-arity+functions+in+Lisp" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Variable-arity+functions+in+Lisp&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fvariable-arity-functions-lisp%2F&amp;title=Variable-arity+functions+in+Lisp" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fvariable-arity-functions-lisp%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fvariable-arity-functions-lisp%2F&amp;title=Variable-arity+functions+in+Lisp" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fvariable-arity-functions-lisp%2F&amp;title=Variable-arity+functions+in+Lisp" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fvariable-arity-functions-lisp%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Variable-arity+functions+in+Lisp+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fvariable-arity-functions-lisp%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/variable-arity-functions-lisp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wrapping a C library in lisp (part 2)</title>
		<link>http://www.artfulcode.net/articles/wrapping-c-library-lisp-part-2/</link>
		<comments>http://www.artfulcode.net/articles/wrapping-c-library-lisp-part-2/#comments</comments>
		<pubDate>Wed, 04 Jul 2007 00:53:00 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/wrapping-c-library-lisp-part-2/</guid>
		<description><![CDATA[Once the library has been defined and loaded into the Lisp image, work can begin on defining C routines in Lisp. Before that can be done, however, a note for those who do not know C. Functions in C have a declared type, as do all of their arguments. When reading the docs for a [...]]]></description>
			<content:encoded><![CDATA[<p>Once the library has been defined and loaded into the Lisp image, work can begin on defining C routines in Lisp.<span id="more-52"></span></p>
<p>Before that can be done, however, a note for those who do not know C.  Functions in C have a declared type, as do all of their arguments.  When reading the docs for a particular library, you will want to know description conventions for C routines.  Let&#8217;s use the example of mysql_init() to begin with.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">MYSQL <span style="color: #339933;">*</span>mysql_init<span style="color: #009900;">&#40;</span>MYSQL <span style="color: #339933;">*</span>mysql<span style="color: #009900;">&#41;</span></pre></div></div>

<p>The first part, <code>MYSQL *mysql_init</code>, defines the function&#8217;s return type and name.  MYSQL is a type created by the library that stores a struct.  This is actually completely irrelevant at this point, because of the asterisk.  The asterisk means that the function is actually allocating a pointer.  Although we can pass an already existing MYSQL object pointer (that&#8217;s the second part), generally we will just use this function to create the internal struct needed to run the program.  We will need to store the pointer that is returned.</p>
<p>The second part, <code>MYSQL *mysql</code>, states this- namely, that a pointer (named mysql, and we know it&#8217;s a pointer because it&#8217;s labelled with an asterisk- *mysql) will be returned.  We store it because this will be passed to most of the other routines in the library.</p>
<p>Technically, we do not really need to define the routine in Lisp.  The function, foreign-funcall, allows us to call the foreign function directly.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>foreign-<span style="color: #b1b100;">funcall</span> <span style="color: #ff0000;">&quot;mysql_init&quot;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">pointer</span> <span style="color: #66cc66;">&#40;</span>null-pointer<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">pointer</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>In this, we apply mysql_init to a null pointer (an empty pointer argument), created using the CFFI function (null-pointer).  The last symbol states we will get a pointer back.</p>
<p>In order to wrap this as a pretty Lisp function, we do:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defcfun <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mysql_init&quot;</span> libmysql-init<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">pointer</span>
  <span style="color: #66cc66;">&#40;</span>mysql <span style="color: #66cc66;">:</span><span style="color: #555;">pointer</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This points the Lisp symbol libmysql-init to &#8220;mysql_init&#8221; in the C library.  The next argument, :pointer, states that this returns a pointer.  Next, we can have an &amp;rest list of (parameter-name :type), to define the parameters to pass to the function, in this case, (mysql :pointer).  Available types are listed in the <a href="http://common-lisp.net/project/cffi/manual/cffi-manual.html#Built_002dIn-Types">CFFI documentation</a>.</p>
<p>However, we don&#8217;t want to have to run (null-pointer) every time we use this function.  We also need to store the return value for future use.  So let&#8217;s wrap it in another function to make it more expressive:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> mysql-init <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> *mysql* <span style="color: #66cc66;">&#40;</span>libmysql-init <span style="color: #66cc66;">&#40;</span>null-pointer<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">or</span> *mysql* <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> *mysql* <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Now, we can simply run (mysql-init) and we will have the symbol *mysql* pointing to the returned pointer or nil in the event of a failure.</p>
<p>If we do have a failure, we can use the following definitions to get at the error text:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defcfun <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mysql_error&quot;</span> libmysql-<span style="color: #b1b100;">error</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">string</span>
    <span style="color: #66cc66;">&#40;</span>mysql <span style="color: #66cc66;">:</span><span style="color: #555;">pointer</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> mysql-<span style="color: #b1b100;">error</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>libmysql-<span style="color: #b1b100;">error</span> *mysql*<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Of course, we will want to add error handling and some other niceties, but having these declared makes writing and testing those completed functions that much easier.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-2%2F&amp;title=Wrapping+a+C+library+in+lisp+%28part+2%29" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-2%2F&amp;title=Wrapping+a+C+library+in+lisp+%28part+2%29" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Wrapping+a+C+library+in+lisp+%28part+2%29&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-2%2F&amp;title=Wrapping+a+C+library+in+lisp+%28part+2%29" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-2%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-2%2F&amp;title=Wrapping+a+C+library+in+lisp+%28part+2%29" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-2%2F&amp;title=Wrapping+a+C+library+in+lisp+%28part+2%29" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-2%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Wrapping+a+C+library+in+lisp+%28part+2%29+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-2%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/wrapping-c-library-lisp-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wrapping a C library in lisp (part 1)</title>
		<link>http://www.artfulcode.net/articles/wrapping-c-library-lisp-part-1/</link>
		<comments>http://www.artfulcode.net/articles/wrapping-c-library-lisp-part-1/#comments</comments>
		<pubDate>Mon, 02 Jul 2007 19:06:00 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/wrapping-c-library-lisp-part-1/</guid>
		<description><![CDATA[One of the more significant difficulties I face when developing Lisp applications is the apparent lack of generally compatable database libraries for the various lisp dialects. CLSQL is a nice solution, but it does not get along well on OSX, my development OS, or with CLISP, which is the primary lisp dialect I have available [...]]]></description>
			<content:encoded><![CDATA[<p>One of the more significant difficulties I face when developing Lisp applications is the apparent lack of generally compatable database libraries for the various lisp dialects. <a href="http://clsql.b9.com/">CLSQL</a> is a nice solution, but it does not get along well on OSX, my development OS, or with CLISP, which is the primary lisp dialect I have available on my <a href="http://www.nearlyfreespeech.com">host</a>.<span id="more-53"></span></p>
<p>My database of choice (and the one available to me on my host) is MySQL. Unfortunately, this is the piece of CLSQL that I can&#8217;t seem to get running on OSX.  In every dialect I&#8217;ve tried (sbcl, cmucl, clisp, openmcl, ad infinitum), CFFI/UFFI cannot find the libmysqlclient library- even when it has been pushed onto the library search path&#8230; but that is a rant for a different blog (or Usenet).</p>
<p><a href="http://common-lisp.net/project/cffi/">CFFI</a> is generally available via ASDF and plays well with GNU CLISP.  It has a fairly simple, intuitive syntax for making C routines available to lisp (as a side note, I have not encountered anything as easy as newLisp&#8217;s C library importer, although this is made easier in that newLisp is implemented in C).</p>
<p>Using CFFI, we can fairly easily create mappings for the most important <a href="http://dev.mysql.com/doc/refman/5.0/en/c.html">MySQL C api</a> functions.  This entry will walk through the process of importing the library; future entries will go one to defining a few of the most basic routines for our lisp image.</p>
<p>Let&#8217;s begin by defining our mysql wrapper package.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defpackage <span style="color: #66cc66;">:</span><span style="color: #555;">mysql-api</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">use</span> <span style="color: #66cc66;">:</span><span style="color: #555;">common-lisp</span> <span style="color: #66cc66;">:</span><span style="color: #555;">cffi</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>in-package <span style="color: #66cc66;">:</span><span style="color: #555;">mysql-api</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>To load the library itself, we use the function, define-foreign-library, in the CFFI package.  The first argument to define-foreign-library is the symbol we wish to point to the library.  Next, we create list similar in syntax to cond, where we define the names of our libraries based on, for example, the host OS.  Since I happen to know that CFFI does find the library path even when explicitly specified, we can conveniently specify a list of full paths to try for each OS.  The full list of platform symbols available <a href="http://common-lisp.net/project/cffi/manual/cffi-manual.html#Platform_002dspecific-features">here</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define-foreign-library libmysqlclient
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">darwin</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #b1b100;">or</span> <span style="color: #ff0000;">&quot;/usr/local/mysql/lib/libmysqlclient.dylib&quot;</span>
                <span style="color: #ff0000;">&quot;/sw/lib/libmysqlclient.dylib&quot;</span>
                <span style="color: #ff0000;">&quot;/usr/local/lib/libmysqlclient.dylib&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>t <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">default</span> <span style="color: #ff0000;">&quot;libmysqlclient&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The last line is a catch-all, and will format the filename based on the OS (i.e., libmysqlclient.so for unix, .dll for Windows).  Although I know my library is located in /usr/local/mysql/lib, I have included various other commonly used paths for MySQL libraries in OSX.</p>
<p>Then, to load the library:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>use-foreign-library libmysqlclient<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This will return a pointer to a foreign address (for the C-challenged, a pointer is a variable to points to a value&#8217;s address in memory; it is used to access a value by reference, rather than by value) or, in the event that it did not work, it will throw up an error.  Generally, the error will be caused by CFFI not being able to find the library. If this is the case, verify that your account has access to the file and that the file is included in one of the paths above.</p>
<p>Next, we will begin defining our C routines in lisp.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-1%2F&amp;title=Wrapping+a+C+library+in+lisp+%28part+1%29" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-1%2F&amp;title=Wrapping+a+C+library+in+lisp+%28part+1%29" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Wrapping+a+C+library+in+lisp+%28part+1%29&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-1%2F&amp;title=Wrapping+a+C+library+in+lisp+%28part+1%29" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-1%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-1%2F&amp;title=Wrapping+a+C+library+in+lisp+%28part+1%29" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-1%2F&amp;title=Wrapping+a+C+library+in+lisp+%28part+1%29" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-1%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Wrapping+a+C+library+in+lisp+%28part+1%29+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-1%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/wrapping-c-library-lisp-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Evolving lisp</title>
		<link>http://www.artfulcode.net/articles/evolving-lisp/</link>
		<comments>http://www.artfulcode.net/articles/evolving-lisp/#comments</comments>
		<pubDate>Fri, 08 Jun 2007 01:58:00 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Soap box]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[newlisp]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/evolving-lisp/</guid>
		<description><![CDATA[Paul Graham notes that, &#8220;A popular recipe for new programming languages in the past 20 years has been to take the C model of computing and add to it, piecemeal, parts taken from the Lisp model, like runtime typing and garbage collection.&#8221; This is what has made Python such a wonderful, elegant, and concise language. [...]]]></description>
			<content:encoded><![CDATA[<p>Paul Graham <a href="http://www.paulgraham.com/rootsoflisp.html">notes</a> that, &#8220;A popular recipe for new programming languages in the past 20 years has been to take the C model of computing and add to it, piecemeal, parts taken from the Lisp model, like runtime typing and garbage collection.&#8221;  This is what has made Python such a wonderful, elegant, and concise language.  Why say with iteration what can be done in one easily readable line with a list comprehension?<span id="more-58"></span></p>
<p>This has also kept many imperative languages moving forward.  Python may be the most lispy, but there is also Ruby, which conceptually borrows heavily from languages like lisp and Smalltalk.  Even Javascript has lambda functions (although in JS they are called function literals), which has made possible powerful libraries such as <a href="http://www.prototypejs.org">Prototype</a>.</p>
<p>Few lisps have ever borrowed from imperative languages, though, as if the borrowers have absolutely nothing to offer themselves.  A few concepts have made it in.  OOP, via CLOS, although OOP is often just extra weight in lisp.  Storing functions as data makes much of OOP needless in lisp.</p>
<p><a href="http://www.newlisp.org">newLisp</a>, a dialect of lisp that uses s-expressions in a high level, interpreted language, has borrowed from imperative languages, and has done an excellent job of maintaining its lispy character despite it.  Regex is built into the interpreter and is used in many functions:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>regexp <span style="color: #66cc66;">&#123;</span>brown fox$<span style="color: #66cc66;">&#125;</span> <span style="color: #ff0000;">&quot;the quick brown fox&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>replace <span style="color: #66cc66;">&#123;</span>brown fox$<span style="color: #66cc66;">&#125;</span> <span style="color: #ff0000;">&quot;the quick brown fox&quot;</span> <span style="color: #66cc66;">&#40;</span>upper-<span style="color: #b1b100;">case</span> $<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Iterative control structures are built into the interpreter, rather than be implemented through macros.  Common Lisp&#8217;s LOOP is very powerful but not very idiomatic of lisp and not very elegant to use (in fact, many of those involved in the original development of the language did not like LOOP, feeling that it was not lispy enough).  In newLisp, iterators definitely have a lispy feel to them:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">dotimes</span> <span style="color: #66cc66;">&#40;</span>i <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>println i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">dolist</span> <span style="color: #66cc66;">&#40;</span>n '<span style="color: #66cc66;">&#40;</span>foo bar baz bat<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>println n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Other similar looping functions in newLisp are dolist, doargs, and dotree.  There are also while, until, unless, and do functions, which all work about as you would imagine in a lisp.</p>
<p>No language will survive in a pure form.  Concepts may &#8211; s-expressions and lists &#8211; but lisp must be willing to evolve on a larger scale.  Lisp was built to evolve, so it&#8217;s a shame that so few of the people who continue to plough ahead in the language want it to.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fevolving-lisp%2F&amp;title=Evolving+lisp" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fevolving-lisp%2F&amp;title=Evolving+lisp" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Evolving+lisp&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fevolving-lisp%2F&amp;title=Evolving+lisp" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fevolving-lisp%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fevolving-lisp%2F&amp;title=Evolving+lisp" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fevolving-lisp%2F&amp;title=Evolving+lisp" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fevolving-lisp%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Evolving+lisp+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fevolving-lisp%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/evolving-lisp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Macros</title>
		<link>http://www.artfulcode.net/articles/macros/</link>
		<comments>http://www.artfulcode.net/articles/macros/#comments</comments>
		<pubDate>Thu, 24 May 2007 01:30:00 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[newlisp]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/macros/</guid>
		<description><![CDATA[A common point of confusion for lisp beginners is the macro. Few lispers can resist the opportunity to expound the beauty and elegance of the macro. However, as with many lisp concepts, most explanations are outside the range of the hobbyist&#8217;s experience. In short, a macro is a type of function that allows the programmer [...]]]></description>
			<content:encoded><![CDATA[<p>A common point of confusion for lisp beginners is the macro.  Few lispers can resist the opportunity to expound the beauty and elegance of the macro.  However, as with many lisp concepts, most explanations are outside the range of the hobbyist&#8217;s experience.<span id="more-62"></span></p>
<p>In short, a macro is a type of function that allows the programmer to define new syntax for use in their programs.  For example, I often hear Rubyists decry the lack of code blocks and Enumerable in other languages.  So, let&#8217;s create a macro called &#8220;each&#8221; and bring this functionality to newLisp.</p>
<p>In Ruby, iteration is performed using the &#8220;each&#8221; method on an object.  Let&#8217;s say we have the list, ["one", "two", "three"].  We want to print out &#8220;I can count to x&#8221; for each item in the list.  We might use:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;one&quot;</span>, <span style="color:#996600;">&quot;two&quot;</span>, <span style="color:#996600;">&quot;three&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;I can count to #{x}.&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>While there are many ways to do this already in newLisp, such as:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>map <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>println <span style="color: #ff0000;">&quot;I can count to &quot;</span> x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</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: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>&#8230;let&#8217;s see how to extend our syntax so we can write this as we would in Ruby.  Macro syntax works almost exactly as it would in a regular function.  The big difference, though, is that arguments passed to your macro are not evaluated.  When you run <code>(foo bar)</code> in lisp, foo is applied to the <em>value</em> of bar.  With a macro, the symbol bar itself is passed, rather than its value, and the macro must evaluate the symbol to its value manually.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define-macro <span style="color: #66cc66;">&#40;</span>each object <span style="color: #b1b100;">do</span> iter<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> 'iter <span style="color: #66cc66;">&#40;</span>trim <span style="color: #66cc66;">&#40;</span>string iter<span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;|&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">dolist</span> <span style="color: #66cc66;">&#40;</span>obj <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eval</span> object<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eval</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> <span style="color: #66cc66;">&#40;</span>sym <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eval</span> iter<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> obj<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>doargs <span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>unless <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> a 'end<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eval</span> a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Let&#8217;s see what&#8217;s going on here.  Our macro, each, accepts three argument: object, do, and iter.  Object refers to the object over which we wish to iterate.  Do is a placeholder for the &#8220;do&#8221; in the each syntax.  Iter is the pipe-encased variable name which we will iterate over.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> 'iter <span style="color: #66cc66;">&#40;</span>trim <span style="color: #66cc66;">&#40;</span>string iter<span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;|&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The first thing we do is trim off the pipe characters to get the variable symbol.</p>
<p>Next, we use a newLisp iterator, dolist, to iterate over object.  Notice that here we must evaluate object so that we are working with the list itself.</p>
<p>Inside our dolist, we assign obj to the symbolic representation of iter, (sym iter).  In our example above, this is the same as saying (set &#8216;x obj).  Now we can access each member of the list, object, using the symbol &#8216;x.</p>
<p>Next, we use the doargs function to iterate over the remaining arguments (in newLisp, all arguments not specified in the function definition are available through the command args, and are iterable using doargs).  The unless statement checks for and &#8216;end symbol.  If the argument is not &#8216;end, it evaluates it, subbing in obj for x.</p>
<p>Careful readers will note that if there are commands after &#8216;end, they will be executed for a given value of x as well, but hey, it&#8217;s a blog entry, and we aren&#8217;t writing a Ruby interpreter :).</p>
<p>Now, the final product is:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>each '<span style="color: #66cc66;">&#40;</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: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span> <span style="color: #66cc66;">|</span>x<span style="color: #66cc66;">|</span>
  <span style="color: #66cc66;">&#40;</span>println <span style="color: #ff0000;">&quot;I can count to &quot;</span> x<span style="color: #66cc66;">&#41;</span>
end<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Which will print:</p>
<pre><code>I can count to one
I can count to two
I can count to three
</code></pre>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmacros%2F&amp;title=Macros" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmacros%2F&amp;title=Macros" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Macros&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmacros%2F&amp;title=Macros" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmacros%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmacros%2F&amp;title=Macros" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmacros%2F&amp;title=Macros" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmacros%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Macros+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmacros%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/macros/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lisp: where to start</title>
		<link>http://www.artfulcode.net/articles/lisp-where-start/</link>
		<comments>http://www.artfulcode.net/articles/lisp-where-start/#comments</comments>
		<pubDate>Sun, 13 May 2007 18:06:00 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[newlisp]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/lisp-where-start/</guid>
		<description><![CDATA[When I first set out to learn lisp, I was faced with an intimidating number of choices. Which implementation of lisp should I choose? Common lisp? Scheme? I went with common lisp. Which common lisp? SBCL? MCL? Gnu clisp? Yeesh. This is enough to scare most lisp rookies off. Here is some advice: start off [...]]]></description>
			<content:encoded><![CDATA[<p>When I first set out to learn lisp, I was faced with an intimidating number of choices.  Which implementation of lisp should I choose?  Common lisp?  Scheme?  I went with common lisp.  Which common lisp?  SBCL?  MCL?  Gnu clisp?  Yeesh.  This is enough to scare most lisp rookies off.  Here is some advice: start off with Gnu clisp.  It&#8217;s available for most architectures, it&#8217;s free, it has decent documentation online, and it&#8217;s easy to install.<span id="more-66"></span></p>
<h4>A brief explanation of the lisp distribution</h4>
<p>Lisp began in the fifties and was the cool new thing.  After a few years, new features were needed, and by this time, there were already several groups that had written interpreters for the language (lisp is both an interpreted language, like Perl, and a compiled language).  Each had added features to the language in different ways.  Various groups got together to standardize the language.  This was a good thing at the time because it brought together all of those differences into one standard.  However, once a language is standardized by a large committee, it becomes that much more difficult to update it, especially if it means straying from the standards.  Lisp often gets around this because it has macros, which are functions that can create new syntaxes for the language.</p>
<p>The standard was called common lisp, and is supported by most major distributions of lisp.  There are other, lisp-like languages from before the standard, such as Scheme, that survived outside the standard because they were already well-established.</p>
<p>Other distributions, such as SBCL, MCL, Gnu, Allegro, et al., support the common lisp standard.  Each, however, has extras that come with them.  Think of them the same way you would think about Linux distros; each supports a basic core functionality, but some specialize in certain areas and have extra libraries and built-in functionality that express this.  For example, MCL is built for Macs, and has the ability to work in tandem with AppleScript and Apple&#8217;s windowing libraries.  I originally chose Gnu clisp because it had built in regex functions, whereas many other lisps require that you install a 3rd party library to support regex (and installing 3rd party libraries is a semester-long course in itself).</p>
<h4>How to learn lisp</h4>
<p>There are dozens of tutorials online that you could start with.  Hundreds, probably.  The problem is that lisp was designed when software was written by engineers and scientists; there was no such thing as the hobbyist.  Most online tutorials use scientific or mathematic examples that not everyone is familiar with.  I myself have a degree in Spanish; most of the examples were pretty meaningless to me.</p>
<p>I found the online book, <a href="http://www.gigamonkeys.com/book/">Practical Common Lisp</a>, which was helpful, but still a little above my head in some areas.  It jumps around a lot and is really for the established lisp programmer who wants to see how another advanced lisp programmer would ineffectively explain lisp to a rookie.</p>
<p>The best instruction I found was a book called, <a href="http://www.amazon.com/Common-Lisp-Introduction-Symbolic-Computation/dp/0805304924">Common Lisp: A Gentle Introduction to Symbolic Computation</a>.  It&#8217;s dumbed down pretty far, but because lisp is so different from other languages, it really is worth your time to go through the book to understand the concepts.  After that, online tutorials will make some sense and you will have a good foundation from which you can build.  At the very least, you won&#8217;t be confused when you see something like (eq (+ 2 2) 4).</p>
<h4>Enter newLISP</h4>
<p>If this is all a bit much for you and you just want a taste, give <a href="http://www.newlisp.org/">newLisp</a> a look.  It uses the same syntactic conventions as lisp, but it has a modern set of functions and is a completely interpreted scripting language, like Perl or Python.  Even if, as with me, you decide to stick with newLisp over other lisps, <a href="http://www.amazon.com/Common-Lisp-Introduction-Symbolic-Computation/dp/0805304924">A Gentle Introduction</a> will still benefit you immensely (even if you don&#8217;t stick with lisp at all, the book will make you a better programmer).</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Flisp-where-start%2F&amp;title=Lisp%3A+where+to+start" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Flisp-where-start%2F&amp;title=Lisp%3A+where+to+start" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Lisp%3A+where+to+start&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Flisp-where-start%2F&amp;title=Lisp%3A+where+to+start" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Flisp-where-start%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Flisp-where-start%2F&amp;title=Lisp%3A+where+to+start" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Flisp-where-start%2F&amp;title=Lisp%3A+where+to+start" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Flisp-where-start%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Lisp%3A+where+to+start+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Flisp-where-start%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/lisp-where-start/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

