<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Lisp on Manuel Herrmann</title>
    <link>https://blog.0x17.de/tags/lisp/</link>
    <description>Recent content in Lisp on Manuel Herrmann</description>
    <image>
      <title>Manuel Herrmann</title>
      <url>https://blog.0x17.de/images/mh.jpg</url>
      <link>https://blog.0x17.de/images/mh.jpg</link>
    </image>
    <generator>Hugo -- 0.131.0</generator>
    <language>en</language>
    <lastBuildDate>Sat, 29 Mar 2025 00:00:00 +0100</lastBuildDate>
    <atom:link href="https://blog.0x17.de/tags/lisp/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Getting Started with Emacs Lisp: Core Concepts Explained</title>
      <link>https://blog.0x17.de/post/emacs-list-introduction/</link>
      <pubDate>Sat, 29 Mar 2025 00:00:00 +0100</pubDate>
      <guid>https://blog.0x17.de/post/emacs-list-introduction/</guid>
      <description>Emacs Lisp (often abbreviated as Elisp) is the programming language that powers the extensibility of the Emacs text editor. It&amp;rsquo;s a dialect of Lisp that has been adapted specifically for text editing tasks. While it might look unusual at first glance, understanding a few key concepts opens up a world of possibilities for customizing your Emacs experience.
In this article, we&amp;rsquo;ll explore some of the fundamental concepts in Emacs Lisp that often confuse newcomers.</description>
      <content:encoded><![CDATA[<p>Emacs Lisp (often abbreviated as Elisp) is the programming language that powers the extensibility of the Emacs text editor. It&rsquo;s a dialect of Lisp that has been adapted specifically for text editing tasks. While it might look unusual at first glance, understanding a few key concepts opens up a world of possibilities for customizing your Emacs experience.</p>
<p><img alt="elisp" loading="lazy" src="/post/emacs-list-introduction/elisp.png"></p>
<p>In this article, we&rsquo;ll explore some of the fundamental concepts in Emacs Lisp that often confuse newcomers.</p>
<h2 id="variables-global-vs-local-scope">Variables: Global vs Local Scope</h2>
<p>Elisp provides several ways to define variables, each with different scoping behaviors.</p>
<h3 id="setq-global-variables"><code>setq</code>: Global Variables</h3>
<p>The <code>setq</code> function is used to define and set global variables:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elisp" data-lang="elisp"><span style="display:flex;"><span>(setq my-variable <span style="color:#e6db74">&#34;Hello, world!&#34;</span>)
</span></span></code></pre></div><p>These variables are accessible from anywhere in your Emacs session once defined. They&rsquo;re ideal for configuration settings or values that need to persist.</p>
<h3 id="let-and-let-local-variables"><code>let</code> and <code>let*</code>: Local Variables</h3>
<p>For variables with limited scope, Elisp provides <code>let</code> and <code>let*</code>:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elisp" data-lang="elisp"><span style="display:flex;"><span><span style="color:#75715e">;; Using let for local binding</span>
</span></span><span style="display:flex;"><span>(let ((x <span style="color:#ae81ff">5</span>)
</span></span><span style="display:flex;"><span>      (y <span style="color:#ae81ff">10</span>))
</span></span><span style="display:flex;"><span>  (<span style="color:#a6e22e">message</span> <span style="color:#e6db74">&#34;Sum: %d&#34;</span> (<span style="color:#a6e22e">+</span> x y)))
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">;; x and y are not accessible here</span>
</span></span></code></pre></div><p>The difference between <code>let</code> and <code>let*</code> is significant:</p>
<ul>
<li><code>let</code> evaluates all variable expressions before binding</li>
<li><code>let*</code> evaluates in sequence, allowing later variables to reference earlier ones</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elisp" data-lang="elisp"><span style="display:flex;"><span><span style="color:#75715e">;; This won&#39;t work with let</span>
</span></span><span style="display:flex;"><span>(let ((x <span style="color:#ae81ff">5</span>)
</span></span><span style="display:flex;"><span>      (y (<span style="color:#a6e22e">+</span> x <span style="color:#ae81ff">2</span>)))  <span style="color:#75715e">; Error: x is not yet bound when this is evaluated</span>
</span></span><span style="display:flex;"><span>  (<span style="color:#a6e22e">message</span> <span style="color:#e6db74">&#34;y: %d&#34;</span> y))
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">;; This works with let*</span>
</span></span><span style="display:flex;"><span>(let* ((x <span style="color:#ae81ff">5</span>)
</span></span><span style="display:flex;"><span>       (y (<span style="color:#a6e22e">+</span> x <span style="color:#ae81ff">2</span>)))  <span style="color:#75715e">; Works because x is already bound</span>
</span></span><span style="display:flex;"><span>  (<span style="color:#a6e22e">message</span> <span style="color:#e6db74">&#34;y: %d&#34;</span> y))  <span style="color:#75715e">; Output: &#34;y: 7&#34;</span>
</span></span></code></pre></div><h2 id="understanding-quote-">Understanding Quote (<code>'</code>)</h2>
<p>One of the most distinctive features of Lisp is the quote character (<code>'</code>). It prevents evaluation of an expression, instead treating it as data.</p>
<h3 id="quoting-variables">Quoting Variables</h3>
<p>When you place a quote in front of a symbol:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elisp" data-lang="elisp"><span style="display:flex;"><span><span style="color:#e6db74">&#39;my-symbol</span>
</span></span></code></pre></div><p>You&rsquo;re telling Elisp, &ldquo;Don&rsquo;t evaluate this symbol, just give me the symbol itself.&rdquo; This is similar to references in other programming languages.</p>
<p>Without the quote, Elisp would try to look up the value of the variable <code>my-symbol</code>.</p>
<h3 id="list-syntax--vs-list--and-cons-">List Syntax: <code>'(...)</code> vs <code>(list ...)</code> and <code>(cons ...)</code></h3>
<p>Lists are fundamental data structures in Lisp. There are several ways to create them:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elisp" data-lang="elisp"><span style="display:flex;"><span><span style="color:#75715e">;; These are equivalent:</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&#39;</span>(a b c)
</span></span><span style="display:flex;"><span>(<span style="color:#a6e22e">list</span> <span style="color:#e6db74">&#39;a</span> <span style="color:#e6db74">&#39;b</span> <span style="color:#e6db74">&#39;c</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">;; These are also equivalent:</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&#39;</span>(a <span style="color:#f92672">.</span> b)
</span></span><span style="display:flex;"><span>(<span style="color:#a6e22e">cons</span> <span style="color:#e6db74">&#39;a</span> <span style="color:#e6db74">&#39;b</span>)
</span></span></code></pre></div><p>The quoted form <code>'(...)</code> is concise but has a limitation: everything inside is automatically quoted. This means you can&rsquo;t mix evaluated and non-evaluated expressions.</p>
<p>When to use each:</p>
<ul>
<li>Use <code>'(...)</code> for literal lists that don&rsquo;t need evaluation</li>
<li>Use <code>(list ...)</code> when you need to mix literals and evaluated expressions:</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elisp" data-lang="elisp"><span style="display:flex;"><span>(setq name <span style="color:#e6db74">&#34;John&#34;</span>)
</span></span><span style="display:flex;"><span>(<span style="color:#a6e22e">list</span> <span style="color:#e6db74">&#39;person</span> name <span style="color:#e6db74">&#39;age</span> <span style="color:#ae81ff">30</span>)  <span style="color:#75715e">; =&gt; (person &#34;John&#34; age 30)</span>
</span></span></code></pre></div><h2 id="understanding-cons-cells-car-and-cdr">Understanding Cons Cells: CAR and CDR</h2>
<p>At the heart of Lisp&rsquo;s data structures is the &ldquo;cons cell&rdquo; - a simple pair of values. Each cons cell has two parts, traditionally accessed with the functions <code>car</code> and <code>cdr</code> (pronounced &ldquo;could-er&rdquo;):</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elisp" data-lang="elisp"><span style="display:flex;"><span>(setq my-pair (<span style="color:#a6e22e">cons</span> <span style="color:#e6db74">&#39;a</span> <span style="color:#e6db74">&#39;b</span>))
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>(<span style="color:#a6e22e">car</span> my-pair)  <span style="color:#75715e">; =&gt; a</span>
</span></span><span style="display:flex;"><span>(<span style="color:#a6e22e">cdr</span> my-pair)  <span style="color:#75715e">; =&gt; b</span>
</span></span></code></pre></div><p>These peculiarly named functions are historical artifacts from the original Lisp implementation on IBM machines:</p>
<ul>
<li><code>car</code>: Contents of the Address part of Register</li>
<li><code>cdr</code>: Contents of the Decrement part of Register</li>
</ul>
<p>In modern terms, think of them as:</p>
<ul>
<li><code>car</code>: head (first element)</li>
<li><code>cdr</code>: tail (rest of the list)</li>
</ul>
<p>Lists in Lisp are actually chains of cons cells that always terminate with <code>nil</code>:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elisp" data-lang="elisp"><span style="display:flex;"><span><span style="color:#75715e">;; This list:</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&#39;</span>(<span style="color:#ae81ff">1</span> <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">3</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">;; Is actually structured as:</span>
</span></span><span style="display:flex;"><span>(<span style="color:#a6e22e">cons</span> <span style="color:#ae81ff">1</span> (<span style="color:#a6e22e">cons</span> <span style="color:#ae81ff">2</span> (<span style="color:#a6e22e">cons</span> <span style="color:#ae81ff">3</span> <span style="color:#66d9ef">nil</span>)))
</span></span></code></pre></div><p>Visualized as pairs:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span>(1 . (2 . (3 . nil)))
</span></span></code></pre></div><p>This implicit <code>nil</code> at the end is crucial - it&rsquo;s what defines a &ldquo;proper list&rdquo; in Lisp. Without this nil termination, you would have a different data structure. The presence of this nil terminator is what allows functions like <code>length</code> to work properly and what lets Lisp know where a list ends.</p>
<p>You can extract elements from lists using these functions:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elisp" data-lang="elisp"><span style="display:flex;"><span>(setq numbers <span style="color:#f92672">&#39;</span>(<span style="color:#ae81ff">1</span> <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">3</span> <span style="color:#ae81ff">4</span>))
</span></span><span style="display:flex;"><span>(<span style="color:#a6e22e">car</span> numbers)       <span style="color:#75715e">; =&gt; 1</span>
</span></span><span style="display:flex;"><span>(<span style="color:#a6e22e">cdr</span> numbers)       <span style="color:#75715e">; =&gt; (2 3 4)</span>
</span></span><span style="display:flex;"><span>(<span style="color:#a6e22e">car</span> (<span style="color:#a6e22e">cdr</span> numbers)) <span style="color:#75715e">; =&gt; 2</span>
</span></span></code></pre></div><p>Elisp provides convenient shorthand functions like <code>cadr</code> (car of cdr) for common combinations:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elisp" data-lang="elisp"><span style="display:flex;"><span>(cadr numbers)      <span style="color:#75715e">; =&gt; 2 (same as (car (cdr numbers)))</span>
</span></span><span style="display:flex;"><span>(caddr numbers)     <span style="color:#75715e">; =&gt; 3 (car of cdr of cdr)</span>
</span></span></code></pre></div><h2 id="the-dot-syntax-in-pairs">The Dot Syntax in Pairs</h2>
<p>In Emacs Lisp, you might encounter constructs with dot notation like:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elisp" data-lang="elisp"><span style="display:flex;"><span>(add-to-list <span style="color:#e6db74">&#39;auto-mode-alist</span> <span style="color:#f92672">&#39;</span>(<span style="color:#e6db74">&#34;\\.md\\&#39;&#34;</span> <span style="color:#f92672">.</span> markdown-mode))
</span></span></code></pre></div><p>This dot notation represents a &ldquo;cons cell&rdquo; or pair. In this example, we&rsquo;re creating a pair where the car is <code>&quot;\\.md\\'&quot;</code> and the cdr is <code>markdown-mode</code>.</p>
<p>The following expressions are equivalent:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elisp" data-lang="elisp"><span style="display:flex;"><span><span style="color:#f92672">&#39;</span>(<span style="color:#e6db74">&#34;\\.md\\&#39;&#34;</span> <span style="color:#f92672">.</span> markdown-mode)
</span></span><span style="display:flex;"><span>(<span style="color:#a6e22e">cons</span> <span style="color:#e6db74">&#34;\\.md\\&#39;&#34;</span> <span style="color:#e6db74">&#39;markdown-mode</span>)
</span></span></code></pre></div><p>The dot creates a direct pair between two values. This syntax is commonly used in alists (like <code>auto-mode-alist</code>), mode hooks, and other paired data structures in Emacs.</p>
<p>When you see a dot in the middle of a list, it means &ldquo;the rest of this list is the cdr of this cons cell&rdquo; rather than continuing the list structure with an implicit nil at the end.</p>
<p>To understand the difference clearly:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elisp" data-lang="elisp"><span style="display:flex;"><span><span style="color:#75715e">;; A list with two elements (has an implicit nil at the end)</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&#39;</span>(<span style="color:#ae81ff">1</span> <span style="color:#ae81ff">2</span>)  <span style="color:#75715e">; =&gt; equivalent to (cons 1 (cons 2 nil))</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">;; A single cons cell (a pair) - NOT a proper list</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&#39;</span>(<span style="color:#ae81ff">1</span> <span style="color:#f92672">.</span> <span style="color:#ae81ff">2</span>)  <span style="color:#75715e">; =&gt; equivalent to (cons 1 2)</span>
</span></span></code></pre></div><p>The difference is significant:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elisp" data-lang="elisp"><span style="display:flex;"><span>(<span style="color:#a6e22e">length</span> <span style="color:#f92672">&#39;</span>(<span style="color:#ae81ff">1</span> <span style="color:#ae81ff">2</span>))     <span style="color:#75715e">; =&gt; 2</span>
</span></span><span style="display:flex;"><span>(<span style="color:#a6e22e">length</span> <span style="color:#f92672">&#39;</span>(<span style="color:#ae81ff">1</span> <span style="color:#f92672">.</span> <span style="color:#ae81ff">2</span>))   <span style="color:#75715e">; =&gt; Error: Wrong type argument: listp, (1 . 2)</span>
</span></span></code></pre></div><p>This distinction is fundamental to understanding how Lisp data structures work.</p>
<h2 id="function-quoting--vs-">Function Quoting: <code>#'</code> vs <code>'</code></h2>
<p>In Elisp, you&rsquo;ll see both <code>'function-name</code> and <code>#'function-name</code> when referring to functions. The difference is important:</p>
<ul>
<li><code>'function-name</code> simply quotes the symbol</li>
<li><code>#'function-name</code> is shorthand for <code>(function function-name)</code>, which specifically tells Elisp that the symbol represents a function</li>
</ul>
<p>When should you use <code>#'</code>? Best practice is to use it whenever you&rsquo;re referring to a function as a function, especially with higher-order functions:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elisp" data-lang="elisp"><span style="display:flex;"><span><span style="color:#75715e">;; Good practice</span>
</span></span><span style="display:flex;"><span>(<span style="color:#a6e22e">mapcar</span> <span style="color:#a6e22e">#&#39;1+</span> <span style="color:#f92672">&#39;</span>(<span style="color:#ae81ff">1</span> <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">3</span>))  <span style="color:#75715e">; =&gt; (2 3 4)</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">;; Works but less explicit</span>
</span></span><span style="display:flex;"><span>(<span style="color:#a6e22e">mapcar</span> <span style="color:#e6db74">&#39;1+</span> <span style="color:#f92672">&#39;</span>(<span style="color:#ae81ff">1</span> <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">3</span>))
</span></span></code></pre></div><p>Using <code>#'</code> helps the byte-compiler optimize your code and makes your intent clearer to other programmers.</p>
<h2 id="association-lists-alists">Association Lists (alists)</h2>
<p>Association lists (alists) are lists of key-value pairs used for simple lookups:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elisp" data-lang="elisp"><span style="display:flex;"><span>(setq my-alist <span style="color:#f92672">&#39;</span>((name <span style="color:#f92672">.</span> <span style="color:#e6db74">&#34;John&#34;</span>)
</span></span><span style="display:flex;"><span>                 (age <span style="color:#f92672">.</span> <span style="color:#ae81ff">30</span>)
</span></span><span style="display:flex;"><span>                 (city <span style="color:#f92672">.</span> <span style="color:#e6db74">&#34;Boston&#34;</span>)))
</span></span></code></pre></div><p>You can access values using functions like <code>assoc</code>:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elisp" data-lang="elisp"><span style="display:flex;"><span>(<span style="color:#a6e22e">cdr</span> (<span style="color:#a6e22e">assoc</span> <span style="color:#e6db74">&#39;age</span> my-alist))  <span style="color:#75715e">; =&gt; 30</span>
</span></span></code></pre></div><p>Alists are frequently used in Emacs for configuration options, especially when the list is relatively small and frequently modified. They&rsquo;re simple but not as efficient for large datasets.</p>
<h2 id="property-lists-plists">Property Lists (plists)</h2>
<p>Property lists are another key-value structure in Elisp, but with a different syntax:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elisp" data-lang="elisp"><span style="display:flex;"><span>(setq my-plist <span style="color:#f92672">&#39;</span>(:name <span style="color:#e6db74">&#34;John&#34;</span> :age <span style="color:#ae81ff">30</span> :city <span style="color:#e6db74">&#34;Boston&#34;</span>))
</span></span></code></pre></div><p>Notice how the keys are prefixed with colons. These are called keywords, similar to Ruby&rsquo;s symbols. You can access values using <code>plist-get</code>:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-elisp" data-lang="elisp"><span style="display:flex;"><span>(<span style="color:#a6e22e">plist-get</span> my-plist :age)  <span style="color:#75715e">; =&gt; 30</span>
</span></span></code></pre></div><p>Other languages with similar concepts include:</p>
<ul>
<li>Ruby with its symbols (<code>:symbol</code>)</li>
<li>Clojure with keywords (<code>:keyword</code>)</li>
<li>Elixir with atoms (<code>:atom</code>)</li>
</ul>
<p>Plists are often used for function arguments that have default values or are optional.</p>
<h2 id="summary">Summary</h2>
<p>Understanding these fundamental Emacs Lisp concepts will help you:</p>
<ol>
<li>Write more effective Elisp code</li>
<li>Understand existing Emacs configurations</li>
<li>Create your own Emacs extensions</li>
</ol>
<p><img alt="summary" loading="lazy" src="/post/emacs-list-introduction/summary.png"></p>
<p>Emacs Lisp&rsquo;s power comes from its simplicity and consistency. Once you grasp these basic concepts, you&rsquo;ll find that the language follows logical patterns that make learning advanced features much easier.</p>
<p>Happy hacking!</p>
]]></content:encoded>
    </item>
  </channel>
</rss>
