<?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; functional</title>
	<atom:link href="http://www.artfulcode.net/tags/functional/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>Using newLISP&#8217;s find-all</title>
		<link>http://www.artfulcode.net/articles/using-newlisps-find-all/</link>
		<comments>http://www.artfulcode.net/articles/using-newlisps-find-all/#comments</comments>
		<pubDate>Tue, 12 Aug 2008 15:29:47 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[newlisp]]></category>
		<category><![CDATA[regexp]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/using-newlisps-find-all/</guid>
		<description><![CDATA[newLISP&#8217;s find-all utility is exceptionally powerful, especially when coupled with rich matching functions like match, regex, and unify. find-all combines search and substitution into a fast, comprehensive function for extracting data from lists and strings. Basic syntax Like many newLISP sequence functions, find-all is defined for both strings and lists. The basic syntax is: Strings: [...]]]></description>
			<content:encoded><![CDATA[<p>newLISP&#8217;s <code>find-all</code> utility is exceptionally powerful, especially when coupled with rich matching functions like <code>match</code>, <code>regex</code>, and <code>unify</code>. <code>find-all</code> combines search and substitution into a fast, comprehensive function for extracting data from lists and strings.<span id="more-15"></span></p>
<h4>Basic syntax</h4>
<p>Like many newLISP sequence functions, <code>find-all</code> is defined for both strings and lists.  The basic syntax is:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;">Strings: <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> pattern target <span style="color: #AF0500;">&#91;</span>expression <span style="color: #AF0500;">&#91;</span>option<span style="color: #AF0500;">&#93;</span><span style="color: #AF0500;">&#93;</span><span style="color: #AF0500;">&#41;</span>
Lists:   <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> pattern target <span style="color: #AF0500;">&#91;</span>expression <span style="color: #AF0500;">&#91;</span>comparison-function<span style="color: #AF0500;">&#93;</span><span style="color: #AF0500;">&#93;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<h4>String searches and regular expressions</h4>
<p>The most basic string search finds occurrences of one string inside of another.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'str <span style="color: #3AA43E;">&quot;Now is the time for all good men to come to the aid of their country.&quot;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> <span style="color: #3AA43E;">&quot;the&quot;</span> str<span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">; (&quot;the&quot; &quot;the&quot; &quot;the&quot;)</span></pre></div></div>

<p>There are three occurrences (including one within the word, &#8216;their&#8217;).  To find only occurrences of &#8216;the&#8217; as a complete word, a regular expression is used:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> <span style="color: #3AA43E;"><span style="color: #AF0500;">&#123;</span>\bthe\b<span style="color: #AF0500;">&#125;</span></span> str <span style="color: #2028B8;">$0</span> <span style="color: #675400;">0</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">; (&quot;the&quot; &quot;the&quot;)</span></pre></div></div>

<p>The first parameter is a regular expression, defined in curly braces to eliminate the need to double-escape entities (like b).  The final parameter, <code>0</code>, is the PCRE option (a full list of options is available in the newLISP documents for <a href="http://www.newlisp.org/downloads/newlisp_manual.html#regex">regex</a>.</p>
<p>The third parameter is key to one of the more powerful features of <code>find-all</code> &#8211; substitution.  The <code>expression</code> parameter is applied to each element found.  Inside of this expression, the global variable <code>$0</code> represents the entire matched element.  In a regular expression search, captured matches are available via subsequently enumerated variables: <code>$1</code>, <code>$2</code>, etc.</p>
<p>For example, to convert all occurrences of &#8216;the&#8217; to upper case in the resulting list:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> <span style="color: #3AA43E;"><span style="color: #AF0500;">&#123;</span>\bthe\b<span style="color: #AF0500;">&#125;</span></span> str <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">upper-case</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span> <span style="color: #675400;">0</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">;(&quot;THE&quot; &quot;THE&quot;)</span></pre></div></div>

<p>Here is a short program to count the number of occurrences of some common words in the text of War and Peace:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'text <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">read-file</span> <span style="color: #3AA43E;">&quot;war_and_peace.txt&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'words <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> <span style="color: #3AA43E;"><span style="color: #AF0500;">&#123;</span>\b\S+\b<span style="color: #AF0500;">&#125;</span></span> text <span style="color: #2028B8;">$0</span> <span style="color: #675400;">1</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; split into words</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">println</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;">&quot;%12s: %6d&quot;</span> <span style="color: #3AA43E;">&quot;Total words&quot;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">length</span> words<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">letn</span> <span style="color: #AF0500;">&#40;</span><span style="color: #AF0500;">&#40;</span>common-words '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;and&quot;</span> <span style="color: #3AA43E;">&quot;or&quot;</span> <span style="color: #3AA43E;">&quot;the&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
       <span style="color: #AF0500;">&#40;</span>counts <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">map</span> '<span style="color: #2028B8;">list</span> common-words <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">count</span> common-words words<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">dolist</span> <span style="color: #AF0500;">&#40;</span>word counts<span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">println</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;">&quot;%12s: %6d&quot;</span> <span style="color: #AF0500;">&#40;</span>word <span style="color: #675400;">0</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span>word <span style="color: #675400;">1</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>With the results:</p>
<pre><code> Total words: 564345
         and:  21023
          or:   1542
         the:  31702
</code></pre>
<p>This isn&#8217;t necessarily efficient; finding all words and then counting occurrences of three specific words in a list of more than half a million elements results in a lot of wasted time.  The regular expression used also includes opening and closing punctuation in the word (i.e. a quote that begins &#8220;The&#8230;&#8221; would result in a missed occurrence of &#8216;the&#8217;, because the list would contain <em>&#8220;The</em> instead.  It also does not account for case when counting occurrences.</p>
<p>Here is a more efficient version:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'common-words '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;and&quot;</span> <span style="color: #3AA43E;">&quot;or&quot;</span> <span style="color: #3AA43E;">&quot;the&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 're <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;"><span style="color: #AF0500;">&#123;</span>\b<span style="color: #AF0500;">&#40;</span>%s<span style="color: #AF0500;">&#41;</span>\b<span style="color: #AF0500;">&#125;</span></span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">join</span> common-words <span style="color: #3AA43E;">&quot;|&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'occurrences <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> re text <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">lower-case</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span> <span style="color: #675400;">1</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">println</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">map</span> '<span style="color: #2028B8;">list</span> common-words <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">count</span> common-words occurrences<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">; ((&quot;and&quot; 22267) (&quot;or&quot; 1582) (&quot;the&quot; 34619))</span></pre></div></div>

<p>Note that <code>find-all</code> is recursive and newLISP has a limited stack size, which can be set during execution with the -s switch:</p>
<pre><code>newlisp -s 500000 common_counts.lsp
</code></pre>
<p>The previous example would hit the default stack limit of 2,048.  This can be solved by using map instead of <code>find-all</code>&#8216;s substitution expression:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'common-words '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;and&quot;</span> <span style="color: #3AA43E;">&quot;or&quot;</span> <span style="color: #3AA43E;">&quot;the&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 're <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;"><span style="color: #AF0500;">&#123;</span>\b<span style="color: #AF0500;">&#40;</span>%s<span style="color: #AF0500;">&#41;</span>\b<span style="color: #AF0500;">&#125;</span></span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">join</span> common-words <span style="color: #3AA43E;">&quot;|&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'occurrences <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">map</span> '<span style="color: #2028B8;">lower-case</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> re text <span style="color: #2028B8;">$0</span> <span style="color: #675400;">1</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">println</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">map</span> '<span style="color: #2028B8;">list</span> common-words <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">count</span> common-words occurrences<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">; ((&quot;and&quot; 22267) (&quot;or&quot; 1582) (&quot;the&quot; 34619))</span></pre></div></div>

<p>The substitution expression is also useful to cause side effects, squeezing even more efficiency out of the algorithm.  Here, <code>find-all</code> increments a count of each word and stores it in a dictionary.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">define</span> counts:<span style="color: #2028B8;">counts</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'common-words '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;and&quot;</span> <span style="color: #3AA43E;">&quot;or&quot;</span> <span style="color: #3AA43E;">&quot;the&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 're <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;"><span style="color: #AF0500;">&#123;</span>\b<span style="color: #AF0500;">&#40;</span>%s<span style="color: #AF0500;">&#41;</span>\b<span style="color: #AF0500;">&#125;</span></span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">join</span> common-words <span style="color: #3AA43E;">&quot;|&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> re text <span style="color: #AF0500;">&#40;</span>counts <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">lower-case</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">+</span> <span style="color: #675400;">1</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">or</span> <span style="color: #AF0500;">&#40;</span>counts <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">lower-case</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span> <span style="color: #675400;">0</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span> <span style="color: #675400;">1</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">dolist</span> <span style="color: #AF0500;">&#40;</span>w common-words<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">println</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;">&quot;%4s: %6d&quot;</span> w <span style="color: #AF0500;">&#40;</span>counts w<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
 <span style="color: #808080; font-style: italic;">; and:  22267</span>
 <span style="color: #808080; font-style: italic;">;  or:   1582</span>
 <span style="color: #808080; font-style: italic;">; the:  34619</span></pre></div></div>

<p>This algorithm is quite fast, although still somewhat weighty in RAM, since it stores the entire text before doing its work.</p>
<h4>List searches</h4>
<p><code>find-all</code> list searches are (arguably) nearly as powerful as regular expressions.  By default, <code>find-all</code> compares elements of target with pattern using <code>match</code>, and the default expression is the entire matched element (<code>$0</code>):</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'target '<span style="color: #AF0500;">&#40;</span><span style="color: #AF0500;">&#40;</span>a <span style="color: #675400;">1</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span>b <span style="color: #675400;">2</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span>c <span style="color: #675400;">3</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span>d <span style="color: #675400;">4</span> <span style="color: #675400;">5</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">?</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span> target<span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">; ((a 1) (b 2) (c 3))</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> '<span style="color: #AF0500;">&#40;</span>d <span style="color: #2028B8;">*</span><span style="color: #AF0500;">&#41;</span> target<span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">; ((d 4 5))</span></pre></div></div>

<p>Using the substitution expression, lists may be unified or matched further:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">?</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span> target <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">unify</span> '<span style="color: #AF0500;">&#40;</span>Letter Number<span style="color: #AF0500;">&#41;</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">; (((Letter a) (Number 1)) ((Letter b) (Number 2)) ((Letter c) (Number 3)))</span></pre></div></div>

<p>Simple list searches using other comparators than <code>match</code> are straight-forward:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> <span style="color: #675400;">4</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #675400;">1</span> <span style="color: #675400;">2</span> <span style="color: #675400;">3</span> <span style="color: #675400;">4</span> <span style="color: #675400;">5</span><span style="color: #AF0500;">&#41;</span> <span style="color: #2028B8;">$0</span> &amp;gt<span style="color: #808080; font-style: italic;">;)</span>
<span style="color: #808080; font-style: italic;">; (1 2 3)</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> <span style="color: #675400;">4</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #675400;">1</span> <span style="color: #675400;">2</span> <span style="color: #675400;">3</span> <span style="color: #675400;">4</span> <span style="color: #675400;">5</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">+</span> <span style="color: #675400;">1</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span> &amp;gt<span style="color: #808080; font-style: italic;">;)</span>
<span style="color: #808080; font-style: italic;">; (2 3 4)</span></pre></div></div>

<p><code>find-all</code> is convenient for searching XML.  newLISP parses XML into a tree.  For example:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">xml-type-tags</span> <span style="color: #2028B8;">nil</span> <span style="color: #2028B8;">nil</span> <span style="color: #2028B8;">nil</span> <span style="color: #2028B8;">nil</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'xml <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">xml-parse</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">get-url</span> <span style="color: #3AA43E;">&quot;http://www.weather.gov/xml/current_obs/index.xml&quot;</span><span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">+</span> <span style="color: #675400;">1</span> <span style="color: #675400;">2</span> <span style="color: #675400;">4</span> <span style="color: #675400;">16</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>For more info on parsing XML in newLISP, see <a href="http://www.artfulcode.net/articles/working-xml-newlisp/">this article</a> and the <a href="http://www.newlisp.org/downloads/newlisp_manual.html#XML">newLISP documentation</a>.</p>
<p>The XML at the url above lists weather stations.  Here is an example station entry, under the root element, &#8220;wx_station_index&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;station<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;station_id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>TAPA<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/station_id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;state<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>AG<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/state<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;station_name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Vc Bird Intl Airport Antigua<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/station_name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;latitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>17.117<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/latitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;longitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-61.783<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/longitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://weather.noaa.gov/weather/current/TAPA.html<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rss_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://weather.gov/xml/current_obs/TAPA.rss<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/rss_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xml_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://weather.gov/xml/current_obs/TAPA.xml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xml_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/station<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><code>assoc</code> is useful for finding a path in a parsed XML list:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'stations-xml <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">assoc</span> <span style="color: #AF0500;">&#40;</span>xml <span style="color: #3AA43E;">&quot;wx_station_index&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>However, <code>assoc</code> only returns the first element of the list that matches.  In the XML article linked above, <code>pop-assoc</code> was used to collect elements iteratively:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'stations '<span style="color: #AF0500;">&#40;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">while</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">assoc</span> <span style="color: #AF0500;">&#40;</span>xml <span style="color: #3AA43E;">&quot;wx_station_index&quot;</span> <span style="color: #3AA43E;">&quot;station&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>That is a handy way of collecting all elements, especially in an irregular document.  With <code>find-all</code>, elements may be found just as easily and without modifying the original list:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;station&quot;</span> <span style="color: #2028B8;">*</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">assoc</span> <span style="color: #AF0500;">&#40;</span>xml <span style="color: #3AA43E;">&quot;wx_station_index&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>From there, it is just as simple to aggregate the values from each element:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'station-pattern
    '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;station&quot;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;station_id&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;state&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;station_name&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;latitude&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;longitude&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;html_url&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;rss_url&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;xml_url&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;station&quot;</span> <span style="color: #2028B8;">*</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">assoc</span> <span style="color: #AF0500;">&#40;</span>xml <span style="color: #3AA43E;">&quot;wx_station_index&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">match</span> station-pattern <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>This returns a list of the node values for each station.  The data can be applied to a different associative structure easily using <code>unify</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;station&quot;</span> <span style="color: #2028B8;">*</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">assoc</span> <span style="color: #AF0500;">&#40;</span>xml <span style="color: #3AA43E;">&quot;wx_station_index&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">unify</span> '<span style="color: #AF0500;">&#40;</span>Id State <span style="color: #2028B8;">Name</span> Lat Lon Html Rss Xml<span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">match</span> station-pattern <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>Each element in this list this creates looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #AF0500;">&#40;</span>Id <span style="color: #3AA43E;">&quot;TAPA&quot;</span><span style="color: #AF0500;">&#41;</span>
 <span style="color: #AF0500;">&#40;</span>State <span style="color: #3AA43E;">&quot;AG&quot;</span><span style="color: #AF0500;">&#41;</span>
 <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">Name</span> <span style="color: #3AA43E;">&quot;Vc Bird Intl Airport Antigua&quot;</span><span style="color: #AF0500;">&#41;</span>
 <span style="color: #AF0500;">&#40;</span>Lat <span style="color: #3AA43E;">&quot;17.117&quot;</span><span style="color: #AF0500;">&#41;</span>
 <span style="color: #AF0500;">&#40;</span>Lon <span style="color: #3AA43E;">&quot;-61.783&quot;</span><span style="color: #AF0500;">&#41;</span>
 <span style="color: #AF0500;">&#40;</span>Html <span style="color: #3AA43E;">&quot;http://weather.noaa.gov/weather/current/TAPA.html&quot;</span><span style="color: #AF0500;">&#41;</span>
 <span style="color: #AF0500;">&#40;</span>Rss <span style="color: #3AA43E;">&quot;http://weather.gov/xml/current_obs/TAPA.rss&quot;</span><span style="color: #AF0500;">&#41;</span>
 <span style="color: #AF0500;">&#40;</span>Xml <span style="color: #3AA43E;">&quot;http://weather.gov/xml/current_obs/TAPA.xml&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<h4>A completely impractical example</h4>
<p>There are further implications of <code>find-all</code>&#8216;s ability to cause side effects in the substitution expression.  To download the XML for each individual station, something like this could be done (<em>don&#8217;t really do this &#8211; it will attempt to spawn more than 2,000 processes</em>):</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'processes
    <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;station&quot;</span> <span style="color: #2028B8;">*</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">assoc</span> <span style="color: #AF0500;">&#40;</span>xml <span style="color: #3AA43E;">&quot;wx_station_index&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
      <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">spawn</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">sym</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">lookup</span> <span style="color: #3AA43E;">&quot;station_name&quot;</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span> 'WEATHER<span style="color: #AF0500;">&#41;</span>
             <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">get-url</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">lookup</span> <span style="color: #3AA43E;">&quot;xml_url&quot;</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">sync</span> <span style="color: #675400;">30000</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; wait 30 seconds for all downloads to finish</span></pre></div></div>

<p>Assuming that the application is able to download the XML file for more than 2,000 stations in 30 seconds (and that there is no hard limit to forked processes, as there is in OSX), the context <code>WEATHER</code> will contain the XML for all stations.</p>
<p>The infeasability of this example aside, it demonstrates, as an example, how easily a user-supplied XML file could be used to script a newLISP application.</p>
<h4>Further documentation</h4>
<ul>
<li> <a href="http://www.newlisp.org/downloads/newlisp_manual.html#find-all">find-all</a></li>
<li> <a href="http://www.newlisp.org/downloads/newlisp_manual.html#unify">unify</a></li>
<li> <a href="http://www.newlisp.org/downloads/newlisp_manual.html#match">match</a></li>
<li> <a href="http://www.newlisp.org/downloads/newlisp_manual.html#regex">regex</a></li>
<li> <a href="http://www.newlisp.org/downloads/newlisp_manual.html#xml-parse">xml-parse</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%2Fusing-newlisps-find-all%2F&amp;title=Using+newLISP%26%238217%3Bs+find-all" 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%2Fusing-newlisps-find-all%2F&amp;title=Using+newLISP%26%238217%3Bs+find-all" 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=Using+newLISP%26%238217%3Bs+find-all&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-newlisps-find-all%2F&amp;title=Using+newLISP%26%238217%3Bs+find-all" 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%2Fusing-newlisps-find-all%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%2Fusing-newlisps-find-all%2F&amp;title=Using+newLISP%26%238217%3Bs+find-all" 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%2Fusing-newlisps-find-all%2F&amp;title=Using+newLISP%26%238217%3Bs+find-all" 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%2Fusing-newlisps-find-all%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+Using+newLISP%26%238217%3Bs+find-all+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-newlisps-find-all%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/using-newlisps-find-all/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>SQL library for newLISP</title>
		<link>http://www.artfulcode.net/articles/sql-library-newlisp/</link>
		<comments>http://www.artfulcode.net/articles/sql-library-newlisp/#comments</comments>
		<pubDate>Mon, 14 Jul 2008 21:24:35 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[foop]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[newlisp]]></category>
		<category><![CDATA[releases]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/sql-library-newlisp/</guid>
		<description><![CDATA[The newLISP SQL library is a set of classes and functions to ease generation of SQL code in newLISP. The module is not yet feature-complete but is in a usable state. Much of the module uses the small convenience classes to &#8220;serialize&#8221; SQL expressions. Most of the module&#8217;s classes have :serialize methods that render the [...]]]></description>
			<content:encoded><![CDATA[<p>The newLISP SQL library is a set of classes and functions to ease generation of SQL code in newLISP.  The module is not yet feature-complete but is in a usable state.<span id="more-20"></span></p>
<p>Much of the module uses the small convenience classes to &#8220;serialize&#8221; SQL expressions.  Most of the module&#8217;s classes have <code>:serialize</code> methods that render the encapsulated data as a string.  For example, the <code>Field</code> class:</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>f <span style="color: #66cc66;">&#40;</span>Field <span style="color: #ff0000;">&quot;table&quot;</span> <span style="color: #ff0000;">&quot;fieldname&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><span style="color: #66cc66;">:</span><span style="color: #555;">serialize</span> f<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&amp;</span>gt<span style="color: #808080; font-style: italic;">; &quot;table.fieldname&quot;</span></pre></div></div>

<p>&#8230;or the <code>Condition</code> class:</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>c <span style="color: #66cc66;">&#40;</span>Condition <span style="color: #ff0000;">&quot;&amp;gt;&quot;</span> <span style="color: #ff0000;">&quot;salary&quot;</span> <span style="color: #cc66cc;">65000</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;">:</span><span style="color: #555;">serialize</span> c<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&amp;</span>gt<span style="color: #808080; font-style: italic;">; &quot;salary &amp;gt; '65000'&quot;</span></pre></div></div>

<p>Note that values are enclosed in single quotes for ANSI compliance.  The most interesting function in the module, however, is <code>sql:expr</code>, which uses the <code>match</code>-based primitives in the <span style="text-decoration: line-through;">functional</span> module to generate various types of expressions:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>sql<span style="color: #66cc66;">:</span><span style="color: #555;">expr</span> <span style="color: #ff0000;">&quot;employees&quot;</span> <span style="color: #ff0000;">&quot;first_name&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&amp;</span>gt<span style="color: #808080; font-style: italic;">; &quot;employees.first_name&quot;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>sql<span style="color: #66cc66;">:</span><span style="color: #555;">expr</span> <span style="color: #ff0000;">&quot;LIKE&quot;</span> <span style="color: #ff0000;">&quot;first_name&quot;</span> <span style="color: #ff0000;">&quot;Stev%&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&amp;</span>gt<span style="color: #808080; font-style: italic;">; &quot;first_name LIKE 'Stev%'&quot;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>sql<span style="color: #66cc66;">:</span><span style="color: #555;">expr</span> <span style="color: #ff0000;">&quot;OR&quot;</span> <span style="color: #66cc66;">&#40;</span>sql<span style="color: #66cc66;">:</span><span style="color: #555;">expr</span> <span style="color: #ff0000;">&quot;LIKE&quot;</span> <span style="color: #ff0000;">&quot;first_name&quot;</span> <span style="color: #ff0000;">&quot;Stev%&quot;</span><span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#40;</span>sql<span style="color: #66cc66;">:</span><span style="color: #555;">expr</span> <span style="color: #ff0000;">&quot;LIKE&quot;</span> <span style="color: #ff0000;">&quot;last_name&quot;</span> <span style="color: #ff0000;">&quot;John%&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&amp;</span>gt<span style="color: #808080; font-style: italic;">; &quot;((first_name LIKE 'Stev%') OR (last_name LIKE 'John%'))&quot;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>sql<span style="color: #66cc66;">:</span><span style="color: #555;">expr</span> <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&amp;</span>gt<span style="color: #808080; font-style: italic;">;&quot;6&quot;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>sql<span style="color: #66cc66;">:</span><span style="color: #555;">expr</span> 'myapp<span style="color: #66cc66;">:</span><span style="color: #555;">employees</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&amp;</span>gt<span style="color: #808080; font-style: italic;">; &quot;employees&quot;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>sql<span style="color: #66cc66;">:</span><span style="color: #555;">expr</span> <span style="color: #ff0000;">&quot;CONV&quot;</span> <span style="color: #ff0000;">&quot;AF&quot;</span> <span style="color: #cc66cc;">16</span> <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&amp;</span>gt<span style="color: #808080; font-style: italic;">; &quot;CONV('AF',16,10)&quot;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>sql<span style="color: #66cc66;">:</span><span style="color: #555;">expr</span> <span style="color: #ff0000;">&quot;LIKE&quot;</span> <span style="color: #66cc66;">&#40;</span>sql<span style="color: #66cc66;">:</span><span style="color: #555;">expr</span> <span style="color: #ff0000;">&quot;employees&quot;</span> <span style="color: #ff0000;">&quot;first_name&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;Stev%&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&amp;</span>gt<span style="color: #808080; font-style: italic;">; &quot;employees.first_name LIKE 'Stev%'&quot;</span></pre></div></div>

<p>Additional functions directly express <code>select</code>, <code>update</code>, <code>insert</code>, and <code>delete</code> statements or handle data type conversions (such as parsing SQL datetimes).</p>
<h4>Functional</h4>
<p>Another addition to Artful Code&#8217;s module list is the <span style="text-decoration: line-through;">functional</span> module.  This library provides some basic conditionals that make use of <code>match</code> to process data.  These macros express program logic by associating blocks of code with the structure of data.</p>
<p>Here is an example using <code>match-case</code>. <code>match-case</code> accepts a single expression and a series of forms that describe what to do based on its structure.  Each case form consists of a <a href="http://www.newlisp.org/downloads/newlisp_manual.html#match">match expression</a>, a list of variables to be locally bound to the result of the match, and an individual form to evaluate in a local scope with the matched variables bound:</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>x '<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>match-<span style="color: #b1b100;">case</span> x
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>? ? ?<span style="color: #66cc66;">&#41;</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;a b and c do not get bound here, because they do not match x&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>? ? ? *<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>a b c d<span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#40;</span>println <span style="color: #ff0000;">&quot;a = 1, b = 2, c = 3, and d = '(4 5)&quot;</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>Here, the second block would match because <code>(? ? ? *)</code> matches against <code>(1 2 3 4 5)</code> and creates the binding association, <code>((a 1) (b 2) (c 3) (d (4 5)))</code>.</p>
<p>Also included in the module is <code>match-cond</code>, which is more powerful than <code>match-case</code>. <code>match-cond</code> works like <code>cond</code>, except that instead of a user-defined conditional, the first form in each case is a list of <code>pattern</code>, <code>symbol-list</code>, and <code>target</code>.  See the <span style="text-decoration: line-through;">documentation</span> for more details.</p>
<p><strong>Edit (2009-02-16): the functional module in mentioned in this post has been replaced with the <a href="http://static.artfulcode.net/newlisp/matching.lsp.html">matching</a> module.</strong></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%2Fsql-library-newlisp%2F&amp;title=SQL+library+for+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%2Fsql-library-newlisp%2F&amp;title=SQL+library+for+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=SQL+library+for+newLISP&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fsql-library-newlisp%2F&amp;title=SQL+library+for+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%2Fsql-library-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%2Fsql-library-newlisp%2F&amp;title=SQL+library+for+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%2Fsql-library-newlisp%2F&amp;title=SQL+library+for+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%2Fsql-library-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+SQL+library+for+newLISP+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fsql-library-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/sql-library-newlisp/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>Partial application and currying</title>
		<link>http://www.artfulcode.net/articles/partial-application-and-currying/</link>
		<comments>http://www.artfulcode.net/articles/partial-application-and-currying/#comments</comments>
		<pubDate>Tue, 04 Dec 2007 18:33:00 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/partial-application-and-currying/</guid>
		<description><![CDATA[Currying, known in Python land as partial application, is a technique in which a function taking multiple arguments composes a function that takes fewer arguments (in most languages, reducing to one, although this is not the case in Python) by partially applying it to given parameters. For example, a function, sum, might be used to [...]]]></description>
			<content:encoded><![CDATA[<p>Currying, known in Python land as partial application, is a technique in which a function taking multiple arguments composes a function that takes fewer arguments (in most languages, reducing to one, although this is not the case in Python) by partially applying it to given parameters.  For example, a function, sum, might be used to compose a new function called &#8220;plus_one&#8221; by currying it with the value of one.  The composed function is not evaluated; it is returned as a function object which may then be applied to other parameters.<span id="more-41"></span></p>
<p>Python&#8217;s partial() is contained in the <a href="http://docs.python.org/lib/module-functools.html">functools module</a> (included since Python 2.5, I believe).  The first argument passed must be the function to be curried, and the rest are positional or keyword arguments that will be used to curry the passed function.</p>
<p>A common case where partials are useful is in defining a compare function for a list.  It often happens that the list must be sorted according to rules defined at runtime.  A partial application can simplify the process, especially if there is a complex sort algorithm.  Assume a list of Items, items.  The list will be sorted according to get_sort_attribute(), which returns the name of the attribute of Item which will be used to perform the sort, and get_sort_direction, which returns either &#8220;desc&#8221; or &#8220;asc.&#8221;  Rather than using a long series of if/else statements and calling the sort() method in various ways, partial() can be used to progressively modify the sort.</p>
<p>To sort a list this way, we might have something like:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">def</span> compare<span style="color: black;">&#40;</span>a, b<span style="color: black;">&#41;</span>:
    attr = get_sort_attribute<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #008000;">dir</span> = get_sort_direction<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    value_a = <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span>a, attr<span style="color: black;">&#41;</span>
    value_b = <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span>b, attr<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">dir</span> == <span style="color: #483d8b;">'desc'</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">cmp</span><span style="color: black;">&#40;</span>value_b, value_a<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">cmp</span><span style="color: black;">&#40;</span>value_a, value_b<span style="color: black;">&#41;</span>
items.<span style="color: black;">sort</span><span style="color: black;">&#40;</span>compare<span style="color: black;">&#41;</span></pre></div></div>

<p>This can get pretty long-winded, especially if our function, compare, performs complex operations before performing the comparison.  Here is the same thing using partial applications to compose our function instead:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> functools <span style="color: #ff7700;font-weight:bold;">import</span> partial
&nbsp;
<span style="color: #808080; font-style: italic;"># Wrap getattr in a lambda so that can accept keyword arguments</span>
attribute_getter = partial<span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> <span style="color: #008000;">object</span>, name:
    <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span><span style="color: #008000;">object</span>, name<span style="color: black;">&#41;</span>, name=get_sort_attribute<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
sort_fn = partial<span style="color: black;">&#40;</span>items.<span style="color: black;">sort</span>, key=attribute_getter<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> get_sort_direction<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> == <span style="color: #483d8b;">'desc'</span>:
    sort_fn = partial<span style="color: black;">&#40;</span>sort_fn, <span style="color: #008000;">reversed</span>=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
sort_fn<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>This is certainly more concise.  I found a nice little currying function for Javascript <a href="http://www.dustindiaz.com/javascript-curry/">here</a> (<strong>edit:</strong> it was pointed out in a reader&#8217;s comment (here and at <a href="http://www.dzone.com/links/partial_application_and_currying.html">dzone</a>) that this version of curry does not work on previously curried functions; below it is a modified version which will function as expected):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
function curry(fn, scope) {
    var scope = scope || window;
    var args = [];
    for (var i=2, len = arguments.length; i &amp;lt; len; ++i) {
        args.push(arguments[i]);
    };
    return function() {
        /* one big problem here is that the following statement
        is not returning the applied function. */</span>
        fn.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>scope<span style="color: #339933;">,</span> args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">*/</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> curry<span style="color: #009900;">&#40;</span>fn<span style="color: #339933;">,</span> scope<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> scope <span style="color: #339933;">=</span> scope <span style="color: #339933;">||</span> window<span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> args <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> arguments.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        args.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>arguments<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// this takes care of the arguments problem</span>
        <span style="color: #003366; font-weight: bold;">var</span> fn_args <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> args.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            fn_args.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> arguments.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            fn_args.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>arguments<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #006600; font-style: italic;">// this takes care of the null return problem</span>
        <span style="color: #000066; font-weight: bold;">return</span> fn.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>scope<span style="color: #339933;">,</span> fn_args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I often use this in my Django web projects.  I will include a basic error function which inserts an error message into an element with a particular id on the base template.  Note that I use jQuery in the following examples.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> err<span style="color: #009900;">&#40;</span>target<span style="color: #339933;">,</span> msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span> <span style="color: #339933;">+</span> target<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In templates that extend that template, I can then use currying to modify that for a particular location defined in this template:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> err <span style="color: #339933;">=</span> curry<span style="color: #009900;">&#40;</span>err<span style="color: #339933;">,</span> window<span style="color: #339933;">,</span> <span style="color: #3366CC;">'err_div_in_this_template'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I can further use this to create custom error callbacks for ajax functions:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> err404 <span style="color: #339933;">=</span> curry<span style="color: #009900;">&#40;</span>err<span style="color: #339933;">,</span> window<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;The server could not be contacted.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, err404 is the equivalent of:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> err404<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'err_div_in_this_template'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The server could not be contacted.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Obviously, this is a pretty trivial example, but it does a good job of showing a real-world use for currying.</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%2Fpartial-application-and-currying%2F&amp;title=Partial+application+and+currying" 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%2Fpartial-application-and-currying%2F&amp;title=Partial+application+and+currying" 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=Partial+application+and+currying&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fpartial-application-and-currying%2F&amp;title=Partial+application+and+currying" 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%2Fpartial-application-and-currying%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%2Fpartial-application-and-currying%2F&amp;title=Partial+application+and+currying" 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%2Fpartial-application-and-currying%2F&amp;title=Partial+application+and+currying" 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%2Fpartial-application-and-currying%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+Partial+application+and+currying+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fpartial-application-and-currying%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/partial-application-and-currying/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java from a functional perspective</title>
		<link>http://www.artfulcode.net/articles/java-functional-perspective/</link>
		<comments>http://www.artfulcode.net/articles/java-functional-perspective/#comments</comments>
		<pubDate>Mon, 15 Oct 2007 16:11:00 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Soap box]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[oop]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/java-functional-perspective/</guid>
		<description><![CDATA[Not long ago a project required me to learn a little Java. As a long-time elitist functional programmer, I was not extremely excited. Due to the low quality of Java software I have used, I assumed, like many, that Java produced slow, bloated, and buggy software. Some languages just feel clunky. My previous experience with [...]]]></description>
			<content:encoded><![CDATA[<p>Not long ago a project required me to learn a little Java.  As a long-time elitist functional programmer, I was not extremely excited.  Due to the low quality of Java software I have used, I assumed, like many, that Java produced slow, bloated, and buggy software.  Some languages just feel clunky.<span id="more-45"></span></p>
<p>My previous experience with Java was as a less experienced programmer and I had some difficulty with the type system.  I learned on dynamically typed languages.  My first experience with static typing was in modern functional languages, where type was inferred automatically by the compiler.  Explicit type declarations felt archaic and overly wordy.</p>
<p>My experience this time around, however, was more positive.  This is primarily due to three factors.</p>
<h4>NetBeans</h4>
<p>NetBeans is Sun&#8217;s Java IDE.  Because it is mostly limited to Java, its syntax formating is excellent.  It takes a lot of code to abstract the ability of an IDE to format multiple languages (in particular, the ability to format both C-style languages and Lisp-style languages using the same application primitives presents difficulty).  NetBeans has the Apple advantage in this; one language to support means that it can have excellent support for that language.  Of course, this means that projects that use multiple languages are more difficult to develop using NetBeans, so perhaps this tradeoff is not worth it.</p>
<p>Java&#8217;s self-documentation is very nice, too.  NetBean&#8217;s code-hinting is superior to most, although it can get in the way.</p>
<p>The GUI builder is also extremely nice.  Swig and AWT can be a real trial in a language as wordy as Java (although I think Sun prefers us to call it &#8220;explicit.&#8221;)</p>
<h4>Classes</h4>
<p>I initially had some difficulty writing entire programs in objects.  Most languages I use that are object oriented are multi-paradigm.  Classes are available, but then utilized within functional or procedural code.  Because of this, I tend to regard classes as a sort of extra-powerful type struct, only to be used when a type becomes complex enough that the extra work of encapsulated it within a class becomes advantageous.</p>
<p>It&#8217;s (unfortunately) common to see new Lisp programmers try to simulate OO in Lisp using closures and hashes (especially if they&#8217;ve read a few Paul Graham essays).  I don&#8217;t particularly see that OO improves Lisp; closures and templates provide roughly the same functionality.  Exactly replicating closures in OOP or objects in functional programming would be moot.  They are different styles; Java-style programming is as uncomfortable in Lisp as Lisp-style programming is in Java.</p>
<p>While writing my initial applications, though, I realized something: classes are a formalized closure syntax.  They are a function that builds functions and closes over their variables.</p>
<p>This freed me to use a style of abstraction in Java that felt much more comfortable to me without the drawback of attempting to use the wrong paradigm for the language.  I could now program using classes and objects in a manner consistent with how I mentally model my applications.</p>
<p>Something I missed quite a bit was pattern matching.  It is very helpful to declare local variables based on the pattern of the argument(s) passed to a function, but that would not be useful in Java, since the primary type mechanism in Java is the class rather than the list.  Method overloading provides much of the same functionality, but idiomatic of Java.</p>
<h4>Write once, run everywhere</h4>
<p>Lisp pioneered the virtual machine.  Lisp is compiled into code that is run within the Lisp interpreter.  But Lisp is not commonly installed on computers, so distribution of Lisp software becomes a real challenge.  One of the advantages of having a huge amount of money behind a language is that Java is installed on virtually every computer a program may end up on.</p>
<p>It&#8217;s common in Lisp to have to code various parts of the program differently depending on the operating system and Lisp distribution.  For example, very few Lisps support multi-threading on OSX.  It makes it agonizing to write threaded software for a multi-platform application.</p>
<p>Java does not have this problem.  A feature is available on all platforms or none at all.  While this means that the language sometimes suffers from the lack of a particular feature, it also means that there is a guarantee against discovering too late that a feature used in the core of your program will not limit its utility on other operating systems.</p>
<h4>Conclusion</h4>
<p>Java is better than I had given it credit for.  With NetBeans taking care of checking your work for you, I am beginning to suspect that the many buggy Java programs are due to poor programming rather than a problem inherent in Java itself.</p>
<p>I still have some doubts as to how useful it would be for a very large project; my experience has been that object orientation and inheritance results in unnecessary complexity.</p>
<p>For small projects and GUI applications, though, Java seems to be a fair choice.</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%2Fjava-functional-perspective%2F&amp;title=Java+from+a+functional+perspective" 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%2Fjava-functional-perspective%2F&amp;title=Java+from+a+functional+perspective" 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=Java+from+a+functional+perspective&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fjava-functional-perspective%2F&amp;title=Java+from+a+functional+perspective" 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%2Fjava-functional-perspective%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%2Fjava-functional-perspective%2F&amp;title=Java+from+a+functional+perspective" 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%2Fjava-functional-perspective%2F&amp;title=Java+from+a+functional+perspective" 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%2Fjava-functional-perspective%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+Java+from+a+functional+perspective+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fjava-functional-perspective%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/java-functional-perspective/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Functional programming</title>
		<link>http://www.artfulcode.net/articles/functional-programming/</link>
		<comments>http://www.artfulcode.net/articles/functional-programming/#comments</comments>
		<pubDate>Mon, 10 Sep 2007 13:34:00 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[functional]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/functional-programming/</guid>
		<description><![CDATA[A recent post on the newLISP forum suggested that it would be helpful to have a short primer on the basics of functional programming for the imperative programmer coming to newLISP (although this also applies to other Lisps). What is functional programming? Functional programming is a programming technique based on lambda calculus that stresses application [...]]]></description>
			<content:encoded><![CDATA[<p>A <a href="http://www.alh.net/newlisp/phpbb/viewtopic.php?t=1891" class="broken_link">recent post on the newLISP forum</a> suggested that it would be helpful to have a short primer on the basics of functional programming for the imperative programmer coming to newLISP (although this also applies to other Lisps).<span id="more-47"></span></p>
<h4>What is functional programming?</h4>
<p>Functional programming is a programming technique based on lambda calculus that stresses application of functions to data (or other functions) and side-effect-free programming style.  This contrasts with imperative programming, the style used in C, Java, PHP, Python (well, sometimes) and others, which emphasizes declarations that change the state of data (known as destructive functions in functional programming).</p>
<h4>Immutable state</h4>
<p>In an imperative program, a variable is declared, and then the value of that variable is modified to produce the changes desired.  For example, in PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$x</span><span style="color: #339933;">++;</span> <span style="color: #666666; font-style: italic;">// x is now equal to 1</span>
<span style="color: #000088;">$x</span><span style="color: #339933;">++;</span> <span style="color: #666666; font-style: italic;">// x is now equal to 1</span></pre></div></div>

<p>In this example, $x is a variable, = is an assignment operator (meaning a sort of function that changes the state of the left-hand-side expression, which typically must be a variable of some sort), and ++ is the postfix increment operator (an operator that increases the value of variable by one).</p>
<p>In strict functional programming, the value assigned to x would be immutable.  Attempting to change it would cause an error.  The only way to represent a value that is based on the value of x would be to use a function that accepts x as a parameter.  For example, using Erlang:</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml" style="font-family:monospace;">n_plus_two<span style="color: #6c6;">&#40;</span>N<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">-&gt;</span> N <span style="color: #a52a2a;">+</span> 2<span style="color: #a52a2a;">.</span>
<span style="color: #060;">X</span> <span style="color: #a52a2a;">=</span> 0<span style="color: #a52a2a;">.</span>
<span style="color: #060;">Y</span> <span style="color: #a52a2a;">=</span> n_plus_two<span style="color: #6c6;">&#40;</span>X<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">.</span></pre></div></div>

<p>Although a bit contrived, this example shows how a new variable must be created if the incremented value of X is to be stored.  However, in many cases, storing a variable is not necessary.  Often, temporary storage inside a local variable (such as a let expression or function scope) is all that is necessary.</p>
<h4>Application of functions</h4>
<p>Functional languages are often expression-based.  This means that everything is an expression, as opposed to imperative languages in which expressions are typically permitted only on the right-hand side of an operator.  In functional programming, functions are applied to their arguments.  For example, the following expressions produce the same result in newLISP:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><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: #808080; font-style: italic;">; =&gt; 15</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">apply</span><span style="color: #66cc66;"> + </span>'<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: #808080; font-style: italic;">; =&gt; 15</span></pre></div></div>

<p>In fact, what apply is actually doing is breaking down the list into smaller chunks and applying the function, +, to each set:</p>

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

<p>Another useful function is map.  Map applies a function to all elements in a list.  Lambdas are extremely helpful when mapping to a list.  For example:</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>fn <span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>+ <span style="color: #cc66cc;">1</span> n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #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: #808080; font-style: italic;">; =&gt; '(2 3 4 5 6)</span></pre></div></div>

<p>Applicative programming is helpful for collecting and filtering from lists as well.  The newLISP function filter removes all elements from a list for which the passed function does not evaluate non-nil.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>filter <span style="color: #66cc66;">&#40;</span>fn <span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>string? n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #ff0000;">&quot;three&quot;</span> <span style="color: #ff0000;">&quot;four&quot;</span> <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; =&gt; (&quot;three&quot; &quot;four&quot;)</span></pre></div></div>

<h4>Currying / partial applications</h4>
<p>Another handy technique common to functional languages is currying.  Curry is a function that returns a function (or a higher order function, as it is known in functional languages).  In many functional languages, currying is built in.  Here is an example in OCaml:</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">let</span> add_numbers x y <span style="color: #a52a2a;">=</span> x <span style="color: #a52a2a;">+</span> y<span style="color: #a52a2a;">;;</span>
<span style="color: #06c; font-weight: bold;">let</span> increment_by_ten <span style="color: #a52a2a;">=</span> add_numbers <span style="color: #c6c;">10</span><span style="color: #a52a2a;">;;</span>
<span style="color: #06c; font-weight: bold;">let</span> x <span style="color: #a52a2a;">=</span> increment_by_ten <span style="color: #c6c;">5</span><span style="color: #a52a2a;">;;</span> <span style="color: #5d478b; font-style: italic;">(* =&gt; 15 *)</span></pre></div></div>

<p>In this example, the function add_numbers takes two numeric arguments (in OCaml, they must specifically be integers).  increment_by_ten takes the function, add_numbers, and associates the first argument with 10.  The function returned by this currying operation looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">let</span> increment_by_ten <span style="color: #c6c;">10</span> y <span style="color: #a52a2a;">=</span> <span style="color: #c6c;">10</span> <span style="color: #a52a2a;">+</span> y<span style="color: #a52a2a;">;;</span></pre></div></div>

<p>This is called implicit currying, or partial application.  In languages like newLISP, currying is 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>add-numbers x y<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>+ x y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> 'increment-by-ten <span style="color: #66cc66;">&#40;</span>curry add-numbers <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h4>Recursion</h4>
<p>Recursion is another technique commonly used in functional programming.  A recursive function calls itself.  This is the equivalent of a loop in an imperative language (arguments about efficiency aside).  To add a series of numbers using imperative syntax, iteration would be necessary.  For example, in Python:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args<span style="color: black;">&#41;</span>:
  n = <span style="color: #ff4500;">0</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> args:
    n = n + x
  <span style="color: #ff7700;font-weight:bold;">return</span> n
<span style="color: #008000;">sum</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">2</span>,<span style="color: #ff4500;">3</span>,<span style="color: #ff4500;">4</span>,<span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># =&gt; 15</span></pre></div></div>

<p>Here is an example in newLISP of a sum function that uses recursion to add a list of arguments:</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>sum list-of-ints<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><span style="color: #b1b100;">null</span>? list-of-ints<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>true <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>first list-of-ints<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>sum <span style="color: #66cc66;">&#40;</span>rest list-of-ints<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>sum '<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: #808080; font-style: italic;">; =&gt; 15</span></pre></div></div>

<p>The important thing to remember with recursion is that each application of the function must test its arguments.  For example, if a function applies itself to every argument in a list, it needs to test the list for length with every recursion (the cond, <code>(null? list-of-ints)</code>, in the example).  This is called the base case.  The base case stops the function from an infinite loop by defining the conditions under which the function stops.  Recursion is a powerful and elegant technique that can seriously reduce the amount of code it takes to create an outcome.</p>
<p>To get a better hold on these concepts, here are some concrete examples.  First, we will show the imperative solution, then we will show how to implement the same result using functional techniques.</p>
<h4>Growing a string</h4>
<p>This is a common task in scripting languages, especially web programming and database operations.  Let&#8217;s build a simple SQL query using the global constant, &#8216;table&#8217; and a list of strings, &#8216;fields&#8217;.</p>
<p>PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$table</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;address_book&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fields</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;first_name&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;last_name&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;phone&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;email&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fields</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sql</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;`<span style="color: #006699; font-weight: bold;">$table</span>`.`<span style="color: #006699; font-weight: bold;">$field</span>`&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fields</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sql</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;, &quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sql</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; FROM <span style="color: #006699; font-weight: bold;">$table</span>&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>newLISP:<br />
<</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>table <span style="color: #ff0000;">&quot;address_book&quot;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span>fields '<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;id&quot;</span> <span style="color: #ff0000;">&quot;first_name&quot;</span> <span style="color: #ff0000;">&quot;last_name&quot;</span> <span style="color: #ff0000;">&quot;phone&quot;</span> <span style="color: #ff0000;">&quot;email&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>format <span style="color: #ff0000;">&quot;SELECT %s FROM %s&quot;</span>
    <span style="color: #66cc66;">&#40;</span>join <span style="color: #66cc66;">&#40;</span>map <span style="color: #66cc66;">&#40;</span>fn <span style="color: #66cc66;">&#40;</span>field<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>format <span style="color: #ff0000;">&quot;`%s`.`%s`&quot;</span> table field<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> fields<span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;, &quot;</span><span style="color: #66cc66;">&#41;</span>
    table<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h4>Formatted output from a list</h4>
<p>We want to build an HTML table from the results of the query we just formed.  We have the data in an array (using mysql_fetch_array() in PHP, for example), $results.</p>
<p>PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;table&gt;&lt;tr&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fields</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;th&gt;<span style="color: #006699; font-weight: bold;">$field</span>&lt;/th&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;/tr&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;tr&gt;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;td&gt;<span style="color: #006699; font-weight: bold;">{$value}</span>&lt;/td&gt;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;/table&gt;&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>newLISP:</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>row-format <span style="color: #66cc66;">&#40;</span>curry format <span style="color: #ff0000;">&quot;&lt;tr&gt;%s&lt;/tr&gt;&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>field-format <span style="color: #66cc66;">&#40;</span>curry format <span style="color: #ff0000;">&quot;&lt;th&gt;%s&lt;/th&gt;&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>value-format <span style="color: #66cc66;">&#40;</span>curry format <span style="color: #ff0000;">&quot;&lt;td&gt;%s&lt;/td&gt;&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 <span style="color: #66cc66;">&#40;</span>format
       <span style="color: #ff0000;">&quot;&lt;table&gt;%s%s&lt;/table&gt;&quot;</span>
       <span style="color: #66cc66;">&#40;</span>row-format <span style="color: #66cc66;">&#40;</span>join <span style="color: #66cc66;">&#40;</span>map field-format fields<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>join
         <span style="color: #66cc66;">&#40;</span>map row-format
           <span style="color: #66cc66;">&#40;</span>map join
             <span style="color: #66cc66;">&#40;</span>map <span style="color: #66cc66;">&#40;</span>curry map value-format<span style="color: #66cc66;">&#41;</span> results<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Notice the currying of map to the already-curried value-format in the second example.</p>
<h4>Common pitfalls</h4>
<p><strong>Infinite recursion</strong></p>
<p>Always remember the base case in recursive functions.  There has to be a stopping point!</p>
<p><strong>Efficiency</strong></p>
<p>In functional languages, functions are first class objects that can be manipulated, curried, returned from other functions and passed as arguments to other functions.  Because calling a user-defined function incurs a certain amount of overhead, using a lambda with a function such as map can create efficiency issues.  The cost of applying a first class object over and over is typically higher than that of an imperative loop.</p>
<p>Different languages have different solutions to this.  Many functional languages (in particular, Schemes and MLs) optimize tail recursion (this is recursion where the final action of a function is the recursive call) by converting it into a loop in the lower level language.  newLISP does not do this.  Instead, it offers iterative expressions (such as for, while, dolist, et al) that avoid the problem altogether, since imperative loops can be used in almost all tail recursions.</p>
<h4>Expressiveness</h4>
<p>Map is a very expressive function.<br />
<code>(map println '("Hello" "world"))</code> is certainly more expressive than <code>foreach ($words as $word) { echo $word . "\n"; }</code>.  However, in deeply nested lists, mapping a function deeply can create difficult-to-read code.  In these situations, it is better to create either locally curried function definitions or use an imperative control structure instead.</p>
<p>Remember the mantra: expressive, efficient, elegant, then idiomatic.</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%2Ffunctional-programming%2F&amp;title=Functional+programming" 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%2Ffunctional-programming%2F&amp;title=Functional+programming" 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=Functional+programming&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Ffunctional-programming%2F&amp;title=Functional+programming" 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%2Ffunctional-programming%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%2Ffunctional-programming%2F&amp;title=Functional+programming" 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%2Ffunctional-programming%2F&amp;title=Functional+programming" 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%2Ffunctional-programming%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+Functional+programming+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Ffunctional-programming%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/functional-programming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

