<?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>So Jake Says &#187; Tutorial</title>
	<atom:link href="http://www.jakevoytko.com/blog/category/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jakevoytko.com/blog</link>
	<description>Ye Olde Computer Science Blogge</description>
	<lastBuildDate>Sun, 17 Jan 2010 15:16:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Everything You Need To Get Started With Common Lisp</title>
		<link>http://www.jakevoytko.com/blog/2008/12/29/everything-you-need-to-get-started-with-common-lisp/</link>
		<comments>http://www.jakevoytko.com/blog/2008/12/29/everything-you-need-to-get-started-with-common-lisp/#comments</comments>
		<pubDate>Mon, 29 Dec 2008 05:00:23 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[asdf-install]]></category>
		<category><![CDATA[emacs]]></category>
		<category><![CDATA[getting started with lisp]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[lisp implementation]]></category>
		<category><![CDATA[Paul Graham]]></category>
		<category><![CDATA[SBCL]]></category>
		<category><![CDATA[SLIME]]></category>
		<category><![CDATA[Superior Lisp Interaction Mode for Emacs]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=343</guid>
		<description><![CDATA[Quick-Links for the TL;DR Crowd About Lisp: Describes Lisp as an overview. High Level Overview: Describes in general what is needed to program with Lisp. All you need SBCL Emacs SLIME asdf-install Code examples: A few rudimentary Lisp snippets. Other Lisp Features: Features that I did not feel like explaining Resources About Lisp Note: For [...]]]></description>
			<content:encoded><![CDATA[<blockquote>
<h2>Quick-Links for the TL;DR Crowd</h2>
<p><span style="text-decoration: underline;"><a href="#about">About Lisp</a>:</span> Describes Lisp as an overview.<br />
<span style="text-decoration: underline;"><a href="#high-level">High Level Overview</a>:</span> Describes in general what is needed to program with Lisp.<br />
<span style="text-decoration: underline;"><a href="#what-you-need">All you need</a></span></p>
<ul>
<li><span style="text-decoration: underline;"><a href="#sbcl">SBCL</a></span></li>
<li><span style="text-decoration: underline;"><a href="#emacs">Emacs</a></span></li>
<li><span style="text-decoration: underline;"><a href="#slime">SLIME</a></span></li>
<li><span style="text-decoration: underline;"><a href="#asdf-install">asdf-install</a></span></li>
</ul>
<p><span style="text-decoration: underline;"><a href="#starting">Code examples</a></span>: A few rudimentary Lisp snippets.<br />
<span style="text-decoration: underline;"><a href="#other-features">Other Lisp Features</a>:</span> Features that I did not feel like explaining <img src='http://www.jakevoytko.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<span style="text-decoration: underline;"><a href="#resources">Resources</a></span></p></blockquote>
<p><a name="about"></a></p>
<h2>About Lisp</h2>
<p><em><strong>Note:</strong> For a complete introduction, I recommend this <a href="http://www.gigamonkeys.com/book/">e-book</a>. If you only want to learn enough to get started, read on!</em></p>
<h3>Interactive</h3>
<p>Most languages take an input file and produce an executable or run a script. Lisp is interactive: all input is entered into the REPL, and the computation&#8217;s result is printed to the screen.</p>
<blockquote><p><strong>REPL:</strong> <strong>R</strong>ead <strong>E</strong>valuate <strong>P</strong>rint <strong>L</strong>oop. A C++ coder can think of it like this:</p>
<pre lang="c++">void lisp()
{
    while(true){
        cout&lt;&lt;"&gt;"; // prompt
        cout&lt;&lt;evaluate(read())&lt;&lt;endl;
    }
}</pre>
</blockquote>
<p>If you&#8217;ve ever used Python&#8217;s command line prompt, you&#8217;ve used a REPL. A bash prompt is a REPL. So is Lisp.</p>
<h3>Prefix</h3>
<p>Most expressions in C-style languages are prefix, but exceptions are made for arithmetic, such as 1/(x+3). All expressions in Lisp are prefix expressions: the first argument of an S-expression (Sexp) is a function name, and the rest of the atoms of the S-expression are the arguments for the function.  The following are examples of Lisp function calls:</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: #66cc66;">&#40;</span>+ x <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>         <span style="color: #808080; font-style: italic;">; 1/(x+3)</span>
<span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;~A~%&quot;</span> x<span style="color: #66cc66;">&#41;</span>    <span style="color: #808080; font-style: italic;">; prints the value of 'x' and a newline.</span></pre></div></div>

<h3>All Expressions Return Values</h3>
<p>All expressions in Lisp return values. For instance, the &#8216;<code>if</code>&#8216; statement in Lisp not only chooses which execution branch to follow, but returns the result of the executed expression. &#8216;<code>nil</code>&#8216; is the default return value of an expression, which also evaluates to false (&#8216;<code>t</code>&#8216; is true).</p>
<p>In C-style languages, expressions don&#8217;t necessarily return values. The expression &#8216;<code>1+3</code>&#8216; will return &#8216;<code>4</code>&#8216;, but <del datetime="2008-12-29T13:36:49+00:00">&#8216;<code>cout&lt;&lt;1+3</code>&#8216; doesn&#8217;t return anything</del><em> (They return a reference to an <code>ostream</code>, so this is a mistake</em>) void functions don&#8217;t return anything; they can&#8217;t be used as a sub-expression for a larger statement of code.</p>
<p>Functions in Common Lisp can return multiple values. The &#8216;<code>multiple-value-bind</code>&#8216; macro is used to bind multiple return values to multiple variables.<br />
<a name="high-level"></a></p>
<h2>High Level Overview: What You Need To Get Started</h2>
<p>1) <strong>An implementation</strong>. All you need is an implementation that conforms to the standards. All of them have an edge case or two, but odds are it won&#8217;t affect you. If you absolutely need to switch later (for CLisp&#8217;s fast arbitrary precision arithmetic, for instance), nothing is stopping you; most libraries support all popular implementations.</p>
<p>2) <strong>An editor</strong>. I personally recommend Emacs with the SLIME extension. You can also use <a href="http://www.vim.org/">Vim</a> and <a href="http://vim.sourceforge.net/scripts/script.php?script_id=2219">Limp</a> if you either prefer Vim or you can&#8217;t stand Emacs. I left Vim before I started using Emacs, so I&#8217;ve never used Limp. <em>Caveat emptor!</em></p>
<p>You don&#8217;t absolutely need to use an editor that interacts with Lisp. It&#8217;s POSSIBLE to run a Lisp program like a script, but it usually makes as much sense as opening a bag of chips with a hammer. You will want to use the REPL for most of your Lisp tasks, and this will only be bearable from within a good editor.</p>
<p>3) <strong>A Lisp tutorial or reference</strong>. I highly recommend &#8220;<span style="text-decoration: underline;"><a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.com%2FANSI-Common-Prentice-Artificial-Intelligence%2Fdp%2F0133708756%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1215999519%26sr%3D8-1&amp;tag=jakvoyshom-20&amp;linkCode=ur2&amp;camp=1789&amp;creative=9325">ANSI Common Lisp</a></span>&#8221; by Paul Graham [<a href="http://www.jakevoytko.com/blog/2008/07/14/review-of-ansi-common-lisp/">see my review</a>].</p>
<p>4) <strong>An installation system</strong>, like <a href="http://www.cliki.net/ASDF-Install">asdf-install</a>. Installation systems automatically load Lisp projects into your current image, and are essential for working with a large project. asdf-install allows you to download libraries from cliki.net and load them into your Lisp image from within the interactive Lisp command prompt!<br />
<a name="what-you-need"></a></p>
<h2>Details: What You Need To Get Started</h2>
<p><a name="sbcl"></a></p>
<h3>SBCL</h3>
<p><strong>About</strong></p>
<p>Steel Bank Common Lisp (SBCL) was forked from Carnagie-Mellon University Common Lisp (CMUCL) in 1999. It is named after Carnagie and Mellon&#8217;s respective industries, steel and banking.</p>
<p>Most Lisps can be interpreted or compiled, depending on the context. However, SBCL compiles all Lisp code it receives. Having used SBCL for 6 months or so, there&#8217;s not much of a pause even for large Lisp files. On the other hand, the compiler is very verbose, but this can be tweaked.</p>
<p><strong>Why SBCL: Popular</strong></p>
<p>Naturally, I can&#8217;t find good numbers on the popularity of SBCL. However, it is noteworthy that <a href="http://www.cliki.net/ASDF-Install">asdf-install</a> and LIMP were initially designed to work with <a href="http://www.vim.org/scripts/script.php?script_id=2219">SBCL</a>. This seems to be par for the course for all of the libraries I&#8217;ve used.</p>
<p><strong>Why SBCL: Active Development</strong></p>
<p>Development of SBCL is still roaring along, with 1,228 commits to their <a href="http://sourceforge.net/projects/sbcl/">Sourceforge</a> page at the time of writing, including several in the past few weeks. New releases come at the beginning of every month, and this month was no exception: version 1.0.23 was produced December 1, 2008.</p>
<h3>Installing SBCL</h3>
<p><strong>Installing SBCL: Linux</strong></p>
<p>SBCL can be installed like any other package:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">aptitude</span> <span style="color: #c20cb9; font-weight: bold;">install</span> sbcl
<span style="color: #666666; font-style: italic;"># Or your local variant.</span></pre></div></div>

<p>Once that&#8217;s finished, it&#8217;s easy to test the installation:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">jake<span style="color: #000000; font-weight: bold;">@</span>justalaptop:~<span style="color: #000000; font-weight: bold;">/</span>code<span style="color: #000000; font-weight: bold;">/</span>genesis$ sbcl
<span style="color: #7a0874; font-weight: bold;">&#40;</span>+ <span style="color: #000000;">2</span> <span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">*</span>
<span style="color: #000000;">4</span></pre></div></div>

<p><strong>Installing SBCL: Windows</strong></p>
<p>There is an &#8220;experimental&#8221; Windows installation binary: [<a href="http://prdownloads.sourceforge.net/sbcl/sbcl-1.0.22-x86-windows-binary.msi">link</a>].</p>
<p>I&#8217;ve ran it for brief periods and it seems to work fine, so I&#8217;m not sure what&#8217;s so experimental about it. Again, <em>Caveat emptor</em>!</p>
<p>After installation, run SBCL. You will get a command-line prompt, and type the following:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (+ 2 2)</span>
*
<span style="color: #cc66cc;">4</span></pre></div></div>

<p>It works!<br />
<a name="emacs"></a></p>
<h3>Emacs</h3>
<p>You can use whatever editor that you want to write Lisp. Emacs has good advantages:</p>
<p><strong>ELisp:</strong> Emacs is scripted in its own subset of Common Lisp. Called &#8216;elisp&#8217;, anybody who has been working with Lisp for a few months and can type &#8220;<a href="http://www.google.com/search?q=elisp+manual&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=com.ubuntu:en-US:unofficial&amp;client=firefox-a">ELisp manual</a>&#8221; into Google can customize Emacs to their taste.</p>
<blockquote><p><strong>Irony alert:</strong> I typed &#8220;elisp manula&#8221; into Google on my first try.</p></blockquote>
<p><strong>Automatic indentation:</strong> Yeah, I know, every editor in the history of the world does automatic indentation. However, Emacs&#8217; authors primarily write Lisp code, so they paid close attention to how Emacs interacts with Lisp. I rarely see Emacs do the Wrong Thing. When it does, I&#8217;m usually writing awful code.</p>
<p><strong>SLIME:</strong> The <strong>S</strong>uperior <strong>L</strong>isp <strong>I</strong>nteraction <strong>M</strong>ode for <strong>E</strong>macs. In order to effectively code in Lisp, you need an editor mode that will interact with a Lisp implementation. Typing the code directly into a command line is nice for small experiments, but is painful for anything more than 30 lines of code (for me at least.. your pain tolerance may vary).<br />
<a name="slime"></a></p>
<h3>SLIME</h3>
<p>SLIME combines the editing power of Emacs with the interactive nature of Lisp.</p>
<p>If you&#8217;ve ever used Python&#8217;s command-line prompt, you&#8217;re aware that it&#8217;s painful to use for any amount of time. The editing capability is limited to that of your console, and when you exit Python, all of your code disappears!</p>
<p>SBCL (and all other Lisp implementations) is no different. If this were the only option for working with Lisp, nobody would. You must work with the REPL from within a real editor. You COULD write code in an external file and load it into SBCL from the command line, but that removes the interactive nature of Lisp.</p>
<p>Enter SLIME (Superir Lisp Interactive Mode for Emacs). SLIME spawns a new Lisp process and acts as the liaison between you and the process.</p>
<p>To start the whole process, just type &#8220;<code>M-x slime</code>&#8220;, and a new REPL buffer opens. Any Lisp command can be entered into this buffer, and you get all of the editing commands from Emacs. <code>slime-mode</code> begins in all of your open Lisp buffers, which gives you code completion and function argument hints for all compiled functions.</p>
<p>If <code>slime-mode</code> is enabled in a Lisp buffer, you can compile the file with &#8220;<code>C-c C-k</code>&#8220;, or compile a single function with &#8220;<code>C-c C-c</code>&#8221; (it looks to me like dependencies are propagated correctly). SLIME knows what capability your Lisp has, so it can take advantage of implementation-specific capabilities like debugging.</p>
<p><strong>Installing SLIME</strong></p>
<p>SLIME is installed the same way as any other Emacs plugin:</p>
<ul>
<li>Download and extract.</li>
<li>Add the load path to your .emacs file</li>
<li>Add any other code snippets required.</li>
</ul>
<p>In this case, you&#8217;re going to be adding the following code to your .emacs file (open using &#8220;<code>M-x M-f ~/.emacs</code>&#8220;):</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>add-to-<span style="color: #b1b100;">list</span> 'load-path <span style="color: #ff0000;">&quot;&lt;em&gt;/the/path/to/slime&lt;/em&gt;&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>require 'slime<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>add-hook 'lisp-mode-hook <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>slime-mode t<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>add-hook 'inferior-lisp-mode-hook <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>inferior-slime-mode t<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;">setq</span> inferior-lisp-program <span style="color: #ff0000;">&quot;sbcl&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>I haven&#8217;t tested it on Windows, so I&#8217;m not sure if there are any extra gotchas. If you find that you&#8217;re running into undefined function errors, one possible workaround is to install Cygwin and add C:\cygwin\bin and c:\cygwin\usr\bin to your <code>%PATH%</code> environment variable. Some people run away in horror at the idea, so if you don&#8217;t like it, you&#8217;re on your own.<br />
<a name="asdf-install"></a></p>
<h3>asdf-install</h3>
<p>One thing that you should research is <a href="http://www.cliki.net/ASDF-Install">asdf-install</a>, a package manager for Lisp. If you find that you need a package that is found on cliki.net, you can download it with one step:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (require 'asdf-install) ; Not the one step.</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ASDF-INSTALL&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (asdf-install:install 'postmodern) ; The one step.</span>
<span style="color: #b1b100;">NIL</span></pre></div></div>

<p>The above installs <a href="http://common-lisp.net/project/postmodern/">Postmodern</a>, a simple <a href="http://www.postgresql.org/">Postgres</a> interface for Common Lisp. Once you&#8217;ve done this, you only need to load the package into your Lisp image. This is also accomplished using <code>asdf-install</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (asdf:oos 'asdf:load-op :postmodern)</span>
<span style="color: #b1b100;">NIL</span></pre></div></div>

<p>Using those 3 lines, you can now start to define database connections and operations on the database, without ever leaving Emacs!<br />
<a name="starting"></a></p>
<h2>Starting to Code</h2>
<p>I highly recommend that you find a real reference or a real tutorial and start using that to write code. However, if you&#8217;re looking for things to enter into the REPL, I&#8217;ll give you some overviews of the basics of Lisp.</p>
<h3>Arithmetic</h3>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (+ 2 2)</span>
<span style="color: #cc66cc;">4</span></pre></div></div>

<p>This adds 2 and 2. Notice that Lisp arithmetic (and all Lisp functions) have prefix notation: the function name always comes first. You can nest values like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (+ (+ 1 1) 1)</span>
<span style="color: #cc66cc;">3</span></pre></div></div>

<h3>Printing</h3>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (format t &quot;Hello, ~A~%&quot; &quot;Jacob&quot;)</span>
<span style="color: #ff0000;">&quot;Hello, Jacob&quot;</span>
<span style="color: #b1b100;">nil</span></pre></div></div>

<p>&#8216;<code>format</code>&#8216; is the function name. This is the Lisp-version of &#8216;<code>printf</code>&#8216;,</p>
<p>The second argument is the destination stream. You can give this &#8216;<code>t</code>&#8216; or &#8216;<code>nil</code>&#8216; for true or false, or the name of a stream.</p>
<p>When &#8216;<code>t</code>&#8216; is given, the value is printed to <code>Standard Output</code>. When &#8216;<code>nil</code>&#8216; is given, the value is returned as a string.</p>
<p>The third argument is the formatting string. &#8220;<code>~</code>&#8221; is the escape character. &#8220;<code>~A</code>&#8221; means that it takes an argument that follows (like <code>printf</code>), and <code>~%</code> is a newline.</p>
<p>There are other functions that you can use for printing, like &#8216;<code>princ</code>&#8216;. I usually stick with &#8216;<code>format</code>&#8216;, but there are different print semantics for different functions.</p>
<h3>Defining functions</h3>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (defun adding-function (arg1 arg2)</span>
    <span style="color: #ff0000;">&quot;Adds arg1 and arg2.&quot;</span>
    <span style="color: #66cc66;">&#40;</span>+ arg1 arg2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
ADDING-<span style="color: #b1b100;">FUNCTION</span>
&nbsp;
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (adding-function 1 2)</span>
<span style="color: #cc66cc;">3</span>
&nbsp;
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (+ (adding-function 1 2) 1)</span>
<span style="color: #cc66cc;">4</span></pre></div></div>

<p>Here we see a function definition and two function calls. Let&#8217;s examine the function definition:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (defun adding-function (arg1 arg2)</span></pre></div></div>

<p>This can be considered the prototype line. &#8216;adding-function&#8217; is the name of the function. It takes two mandatory parameters, &#8216;arg1&#8242; and &#8216;arg2&#8242;, both with no default values.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #ff0000;">&quot;Adds arg1 and arg2.&quot;</span></pre></div></div>

<p>This is the &#8220;documentation string.&#8221; A string may optionally be the first atom of a function, and it can be accessed from within Lisp using Lisp&#8217;s documentation reading abilities. Note that unlike wimpy strings in most languages, Lisp strings can contain newlines with no extra syntactical work.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>+ arg1 arg2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This is the executed statement within the function body. A Lisp function returns the return value of the last statement. Since this is the only statement, it returns the addition of <code>arg1</code> and <code>arg2</code>.</p>
<h3>Defining variables</h3>
<p>Defining a variable for the first time in SBCL:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defvar my-var<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; my-var is 'nil'.</span>
<span style="color: #66cc66;">&#40;</span>defvar my-var 'a-<span style="color: #b1b100;">value</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; my-var is 'a-value'.</span></pre></div></div>

<p>Notice the quote in <code>'a-value</code>. The single quotation mark indicates that the following expression is <strong>data, not code</strong>. Otherwise, it would try to look up the value of the variable <code>a-value</code>, which we may or may not have defined. Either way, it&#8217;s not what we want.</p>
<p>If a variable is already defined, you can use &#8216;<code>setf</code>&#8216;:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (setf my-var 3)</span>
<span style="color: #cc66cc;">3</span></pre></div></div>

<h3>Conditionals</h3>
<p>For statements that only execute when something is true, use &#8216;<code>when</code>&#8216;. For statements that only execute when something is false, use &#8216;<code>unless</code>&#8216;.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (when (= 1 1)</span>
    t<span style="color: #66cc66;">&#41;</span>
t
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (unless (= 1 1)</span>
    t<span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">nil</span></pre></div></div>

<p>For statements of the form &#8220;<code>if (true), do (x), otherwise do (y)</code>&#8220;, use &#8216;<code>if</code>&#8216;.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (if expression</span>
    x
    y<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>If &#8216;<code>expression</code>&#8216; doesn&#8217;t evaluate to &#8216;<code>nil</code>&#8216;, then &#8216;<code>x</code>&#8216; is executed and its return value is returned. Otherwise, &#8216;<code>y</code>&#8216; is executed and its return value is returned.</p>
<p>For statements involving a lot of if, else-if clauses in other languages, use cond:</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;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>test1<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>expression1<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>test2<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>expression2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #808080; font-style: italic;">; ...</span>
      <span style="color: #66cc66;">&#40;</span>t <span style="color: #66cc66;">&#40;</span>default-expression<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h3>Iteration</h3>
<p>The two easiest ways to iterate are the macros &#8216;<code>dotimes</code>&#8216; and &#8216;<code>dolist</code>&#8216;.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (dotimes (i 3)</span>
    <span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;~A&quot;</span> i<span style="color: #66cc66;">&#41;</span>
012
<span style="color: #b1b100;">nil</span></pre></div></div>

<p>&#8216;<code>dotimes</code>&#8216; is exactly as it sounds: it executes a statement a number of times. You assign a variable (in our case, &#8216;<code>i</code>&#8216;) and tell it the number of times it shall execute, and the variable takes on the values of all of the integers in the range <code>[0, n)</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (dolist (i '(1 2 3))</span>
    <span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;~A&quot;</span> i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #cc66cc;">123</span>
<span style="color: #b1b100;">nil</span></pre></div></div>

<p>&#8216;<code>dolist</code>&#8216; iterates through a list much in the same way &#8216;<code>dotimes</code>&#8216; does.</p>
<h3>Reduce</h3>
<p>The idea of &#8216;<code>reduce</code>&#8216; is to iterate through a set, applying a function to each value that acts as an accumulator. For instance, we can do this with addition:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (reduce #'+ '(1 2 3))</span>
<span style="color: #cc66cc;">6</span></pre></div></div>

<h3>Lambdas</h3>
<p>If you want to define your own functions for &#8216;<code>reduce</code>&#8216; without writing formal functions, you can do them as a &#8216;<code>lambda</code>&#8216;. That&#8217;s just a fancy word for &#8216;function with no name&#8217;. It consists of the name, the argument list, and a function body to execute:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (lambda (x) (+ x 2))</span></pre></div></div>

<p>This defines an anonymous function that adds 2 to the input given. The only difference from &#8216;<code>reduce</code>&#8216; is that we need to provide two parameters: the accumulated value and the next value in the list.</p>
<p>You&#8217;re going to use this. A lot. Formally defining every function is not worth it. A lot of Lisp code ends up much cleaner with functional approaches.</p>
<p>To define our own addition function for &#8216;<code>reduce</code>&#8216;:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (reduce (lambda (x y) (+ x y)) '(1 2 3))</span>
<span style="color: #cc66cc;">6</span></pre></div></div>

<p><a name="other-features"></a></p>
<h2>Other features of Common Lisp</h2>
<ul>
<li>Macros: Creating code on-the-fly</li>
<li>CLOS: Common Lisp Object System. Lisp&#8217;s answer to Object Oriented programming.</li>
<li>Packages: This is how Lisp does namespacing.</li>
<li>More iteration constructs than you know what to do with.</li>
<li>&#8216;<code>mapcar</code>&#8216;: Apply a function to each element of a list</li>
<li>Hash tables</li>
<li>Arbitrary precision integers and numbers</li>
<li><code>cons</code>: &#8220;Construct&#8221; function. This is the function that builds lists.<code>car</code>: Returns the first element of a list.</li>
<li><code>cdr</code>: Returns the tail of a list (everything but the car).</li>
<li>Arbitrary binary manipulation</li>
<li>&#8216;<code>eval</code>&#8216;: Generate code from data at runtime.</li>
<li>File streams, input streams, output streams, stream redirections, oh my!</li>
<li>FFI: Foreign function interface. This lets you interface Lisp with C. You&#8217;ll probably need to do this if you want to add functionality to Common Lisp. It&#8217;s actually surprisingly easy to use.</li>
</ul>
<p><a name="resources"></a></p>
<h2>Resources</h2>
<p><strong>Books</strong></p>
<p><span style="text-decoration: underline;"><a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.com%2FANSI-Common-Prentice-Artificial-Intelligence%2Fdp%2F0133708756%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1215999519%26sr%3D8-1&amp;tag=jakvoyshom-20&amp;linkCode=ur2&amp;camp=1789&amp;creative=9325">ANSI Common Lisp</a></span> by Paul Graham. A great first Lisp book. It can be used as a textbook.</p>
<p><span style="text-decoration: underline;"><a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.com%2FParadigms-Artificial-Intelligence-Programming-Studies%2Fdp%2F1558601910%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1216001266%26sr%3D8-1&amp;tag=jakvoyshom-20&amp;linkCode=ur2&amp;camp=1789&amp;creative=9325">Paradigms of Artificial Intelligence Programming</a></span> by Peter Norvig. The best programming book I have ever read. It does have quite a bit on AI, but it also has extensive sections on advanced Lisp programming.</p>
<p><strong>Websites</strong></p>
<p><a href="http://www.sbcl.org/">SBCL:</a> The Lisp implementation I currently use.</p>
<p><a href="http://common-lisp.net/project/slime/">SLIME</a>: A Lisp interaction plugin for Emacs.</p>
<p><a href="http://vim.sourceforge.net/scripts/script.php?script_id=2219">Limp</a>: A SBCL plugin for Vim.</p>
<p><a href="http://cliki.net">cliki.net</a>: A general Lisp resource. The home of all projects remotely installable using asdf-install.</p>
<p><a href="http://cl-cookbook.sourceforge.net/">Common Lisp Cookbook</a>: An incomplete, yet still helpful, recipe book for Lisp code.</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=343&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2008/12/29/everything-you-need-to-get-started-with-common-lisp/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>
