<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Parenthetical Curios</title>
    <link>https://read.cicadas.surf/parenthetical-curios/</link>
    <description>Oddities Explored in Common Lisp</description>
    <pubDate>Thu, 16 Apr 2026 06:05:46 -0500</pubDate>
    <item>
      <title>Swank Deploys</title>
      <link>https://read.cicadas.surf/parenthetical-curios/commonlisp-emacs</link>
      <description>&lt;![CDATA[Swank Deploys&#xA;&#xA;SLIME and SWANK are incredible. Start a SWANK server in your application and Whammo! You&#39;ve can connect to it from the comfort of Emacs. From there you can tinker with its insides to your heart&#39;s content. This is a beta-testing dream!&#xA;&#xA;Except when it isn&#39;t. Recently I deployed a Common Lisp web application where I ran into some trouble.&#xA;&#xA;Trouble The First: Connection Error&#xA;&#xA;So say that my service is running on server.com and SWANK is listening on port 4040. Then to connect to it I have been using ssh port forwarding:&#xA;&#xA;    ssh -L 4040:localhost:4040 me@server.com &#xA;&#xA;So that I can then do slime-connect with localhost and 4040 as its arguments.&#xA;&#xA;You might be surprised, however if you were to have used the following ssh command, which lists the local host ip of 127.0.0.1, instead of localhost:&#xA;&#xA;    ssh -L 4040:127.0.0.1:4040 me@server.com &#xA;&#xA;For whatever reason, this fails: ssh reports a connection error. So, I had to use localhost explicitly.&#xA;&#xA;Trouble The Second: SWANK-REQUIRE&#xA;&#xA;The second, and more irksome issue comes up when I actually succeed at connecting via slime-connect: I immediately drop into the slime debugger. The issue seems to be that SWANK is trying to load some code upon connection. Obviously, it is not going to be able to load code unless that code is present and available on the server.&#xA;&#xA;In order to get the required code into the deployed binary, I do the following prior to executing save-lisp-and-die:&#xA;&#xA;(swank:swank-require &#xA; &#39;(SWANK-IO-PACKAGE::SWANK-INDENTATION&#xA;   SWANK-IO-PACKAGE::SWANK-TRACE-DIALOG&#xA;   SWANK-IO-PACKAGE::SWANK-PACKAGE-FU&#xA;   SWANK-IO-PACKAGE::SWANK-PRESENTATIONS&#xA;   SWANK-IO-PACKAGE::SWANK-MACROSTEP&#xA;   SWANK-IO-PACKAGE::SWANK-FUZZY&#xA;   SWANK-IO-PACKAGE::SWANK-FANCY-INSPECTOR&#xA;   SWANK-IO-PACKAGE::SWANK-C-P-C&#xA;   SWANK-IO-PACKAGE::SWANK-ARGLISTS&#xA;   SWANK-IO-PACKAGE::SWANK-REPL))&#xA;&#xA;I got the list of required modules directly from the SLIME debugger. I recompiled, redeployed, and ta-dah! It works!&#xA;&#xA;\#commonlisp \#emacs]]&gt;</description>
      <content:encoded><![CDATA[<h1 id="swank-deploys">Swank Deploys</h1>

<p><a href="https://slime.common-lisp.dev/" rel="nofollow">SLIME and SWANK</a> are incredible. Start a SWANK server in your application and Whammo! You&#39;ve can connect to it from the comfort of Emacs. From there you can tinker with its insides to your heart&#39;s content. This is a beta-testing dream!</p>

<p>Except when it isn&#39;t. Recently I deployed a Common Lisp web application where I ran into some trouble.</p>

<h2 id="trouble-the-first-connection-error">Trouble The First: Connection Error</h2>

<p>So say that my service is running on <code>server.com</code> and SWANK is listening on port <code>4040</code>. Then to connect to it I have been using ssh port forwarding:</p>

<p>    ssh -L 4040:localhost:4040 me@server.com</p>

<p>So that I can then do <code>slime-connect</code> with <code>localhost</code> and <code>4040</code> as its arguments.</p>

<p>You might be surprised, however if you were to have used the following ssh command, which lists the local host ip of <code>127.0.0.1</code>, instead of <code>localhost</code>:</p>

<p>    ssh -L 4040:127.0.0.1:4040 me@server.com</p>

<p>For whatever reason, this fails: ssh reports a connection error. So, I had to use <code>localhost</code> explicitly.</p>

<h2 id="trouble-the-second-swank-require">Trouble The Second: SWANK-REQUIRE</h2>

<p>The second, and more irksome issue comes up when I actually succeed at connecting via <code>slime-connect</code>: I immediately drop into the slime debugger. The issue seems to be that <code>SWANK</code> is trying to load some code upon connection. Obviously, it is not going to be able to load code unless that code is present and available on the server.</p>

<p>In order to get the required code into the deployed binary, I do the following prior to executing <code>save-lisp-and-die</code>:</p>

<pre><code class="language-lisp">
(swank:swank-require 
 &#39;(SWANK-IO-PACKAGE::SWANK-INDENTATION
   SWANK-IO-PACKAGE::SWANK-TRACE-DIALOG
   SWANK-IO-PACKAGE::SWANK-PACKAGE-FU
   SWANK-IO-PACKAGE::SWANK-PRESENTATIONS
   SWANK-IO-PACKAGE::SWANK-MACROSTEP
   SWANK-IO-PACKAGE::SWANK-FUZZY
   SWANK-IO-PACKAGE::SWANK-FANCY-INSPECTOR
   SWANK-IO-PACKAGE::SWANK-C-P-C
   SWANK-IO-PACKAGE::SWANK-ARGLISTS
   SWANK-IO-PACKAGE::SWANK-REPL))

</code></pre>

<p>I got the list of required modules directly from the <code>SLIME</code> debugger. I recompiled, redeployed, and ta-dah! It works!</p>

<p>#commonlisp #emacs</p>
]]></content:encoded>
      <guid>https://read.cicadas.surf/parenthetical-curios/commonlisp-emacs</guid>
      <pubDate>Mon, 08 Aug 2022 12:33:07 +0000</pubDate>
    </item>
    <item>
      <title>WITH-PLIST</title>
      <link>https://read.cicadas.surf/parenthetical-curios/with-plist</link>
      <description>&lt;![CDATA[WITH-PLIST&#xA;&#xA;\#commonlisp&#xA;&#xA;When I&#39;m deserializing JSON, I usually end up with a property list, also called a plist (I pronounce it &#34;Pea List&#34;). But extracting values from plists using getf over and over again gets tedious.&#xA;&#xA;Common Lisp already has with-slots and with-accessors for convenient access to CLOS objects, so why not a with-plist for convenient access to plist values?&#xA;&#xA;What should with-plist do exactly? Let&#39;s look at with-slots for guidance.&#xA;&#xA;WITH-SLOTS as a Guide&#xA;&#xA;Observe the following:&#xA;&#xA;;; define a class to test with-slots with &#xA;(defclass abc ()&#xA;  ((a :initform 0)&#xA;   (b :initform 0)&#xA;   (c :initform 0)))&#xA;&#xA;(let ((ob (make-instance &#39;abc))) &#xA;  ;; bind slots by name or give a local-name to a slot binding&#xA;  (with-slots (a (my-b b) (my-c c)) ob &#xA;    (setf a 10     &#xA;          my-b 20 &#xA;          my-c 30) &#xA;    (format t &#34;a=~a, my-b=~a, my-c=~a~%&#34; &#xA;            a my-b my-c)) &#xA;  (format t &#34;ob&#39;s b = ~a~%&#34; (slot-value ob &#39;b)))&#xA;&#xA;;; the above prints:&#xA;;; a=10, my-b=20, my-c=30&#xA;;; ob&#39;s b = 20&#xA;&#xA;So here you see that with-slots allows the programmer to associate accessors with an object&#39;s slots, optionally giving those accessors a name that differs from correpsonding slot&#39;s name. Furthermore, when those variables are mutated with setf, the object itself is also mutated. How does with-slots accomplish this?&#xA;&#xA;To find out, lets crack it open with macroexpand and see what&#39;s inside:&#xA;&#xA;(macroexpand &#xA; &#39;(with-slots (a (my-b b) (my-c c)) ob &#xA;   (setf a 10 &#xA;    my-b 20 &#xA;    my-c 30) &#xA;   (format t &#34;the OB has a=~a, b=~a, c=~a~%&#34; &#xA;    a my-b my-c)))&#xA;&#xA;;; returns&#xA;&#xA;(LET ((#:G252 OB))&#xA;  (DECLARE (IGNORABLE #:G252))&#xA;  (DECLARE (SB-PCL::%VARIABLE-REBINDING #:G252 OB))&#xA;  (SYMBOL-MACROLET ((A (SLOT-VALUE #:G252 &#39;A))&#xA;                    (MY-B (SLOT-VALUE #:G252 &#39;B))&#xA;                    (MY-C (SLOT-VALUE #:G252 &#39;C)))&#xA;    (SETF A 10&#xA;          MY-B 20&#xA;          MY-C 30)&#xA;    (FORMAT T &#34;the OB has a=~a, b=~a, c=~a~%&#34; A MY-B MY-C)))&#xA;&#xA;Ah-hah! It&#39;s symbol-macrolet!&#xA;&#xA;The symbol macrolet replaces instances of the symbols a, my-b and my-c with slot access forms within the body of with-slots. We may profit from the same technique in the construction of with-plist.&#xA;&#xA;But first, an hypothetical example of with-plist in use:&#xA;&#xA;Using WITH-PLIST&#xA;&#xA;Consider the following example:&#xA;&#xA;(let ((pl&#xA;        (list &#39;name &#34;Buckaroo Banzai&#34;&#xA;              :age 29&#xA;              :|currentJob| &#34;Astro-Spy Rocker&#34;)))&#xA;  (with-plist (name (age :age) (job :|currentJob|)) pl &#xA;    (incf age) &#xA;    (format t &#34;~a the ~a had a birthday and is now ~a years old~%&#34; &#xA;            name job age) &#xA;    pl))&#xA;;; prints out&#xA;;; Buckaroo Banzai the Astro-Spy Rocker had a birthday and is now 30 years old&#xA;&#xA;;; and returns&#xA;;; (NAME &#34;Buckaroo Banzai&#34; :AGE 30 :|currentJob| &#34;Astro-Spy Rocker&#34;)&#xA;&#xA;Here you can see that with-plists should be able to access keys of differing types, associate names for those accessors, and update the plist by referencing those names.&#xA;&#xA;If the key is an ordinary symbol (e.g name above), then you can use that symbol itself to name the accessor. Otherwise, if the key is a keyword symbol (e.g. :age and :|currentJob| above), then some kind of local name should be provided.&#xA;&#xA;Otherwise it works just like with-slots.&#xA;&#xA;A Draft of the Macro&#xA;&#xA;(defmacro with-plist (keys plist &amp;body body)&#xA;  (let* ((plist-var&#xA;           (gensym))&#xA;         (macrolet-bindings&#xA;           (loop for term in keys&#xA;                 when (consp term)  &#xA;                   collect (destructuring-bind (var key) term&#xA;                             `(,var (getf ,plist-var &#39;,key)))&#xA;                 else&#xA;                   collect `(,term (getf ,plist-var &#39;,term)))))&#xA;    `(let ((,plist-var ,plist))&#xA;       (symbol-macrolet ,macrolet-bindings ,@body))))&#xA;&#xA;The macro first determines the names of the symbol-macrolet bindings before binding each one to a getf form that accesses the plist. Thats it!]]&gt;</description>
      <content:encoded><![CDATA[<h1 id="with-plist">WITH-PLIST</h1>

<p>#commonlisp</p>

<p>When I&#39;m deserializing JSON, I usually end up with a <a href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_p.htm#property_list" rel="nofollow">property list</a>, also called a <em>plist</em> (I pronounce it “Pea List”). But extracting values from plists using <code>getf</code> over and over again gets tedious.</p>

<p>Common Lisp already has <code>with-slots</code> and <code>with-accessors</code> for convenient access to CLOS objects, so why not a <code>with-plist</code> for convenient access to plist values?</p>

<p>What should <code>with-plist</code> do exactly? Let&#39;s look at <code>with-slots</code> for guidance.</p>

<h2 id="with-slots-as-a-guide">WITH-SLOTS as a Guide</h2>

<p>Observe the following:</p>

<pre><code class="language-lisp">
;; define a class to test with-slots with 
(defclass abc ()
  ((a :initform 0)
   (b :initform 0)
   (c :initform 0)))

(let ((ob (make-instance &#39;abc))) 
  ;; bind slots by name or give a local-name to a slot binding
  (with-slots (a (my-b b) (my-c c)) ob 
    (setf a 10     
          my-b 20 
          my-c 30) 
    (format t &#34;a=~a, my-b=~a, my-c=~a~%&#34; 
            a my-b my-c)) 
  (format t &#34;ob&#39;s b = ~a~%&#34; (slot-value ob &#39;b)))

;; the above prints:
;; a=10, my-b=20, my-c=30
;; ob&#39;s b = 20
</code></pre>

<p>So here you see that <code>with-slots</code> allows the programmer to associate accessors with an object&#39;s slots, optionally giving those accessors a name that differs from correpsonding slot&#39;s name. Furthermore, when those variables are mutated with <code>setf</code>, the object itself is also mutated. How does <code>with-slots</code> accomplish this?</p>

<p>To find out, lets crack it open with <code>macroexpand</code> and see what&#39;s inside:</p>

<pre><code class="language-lisp">
(macroexpand 
 &#39;(with-slots (a (my-b b) (my-c c)) ob 
   (setf a 10 
    my-b 20 
    my-c 30) 
   (format t &#34;the OB has a=~a, b=~a, c=~a~%&#34; 
    a my-b my-c)))

;; returns

(LET ((#:G252 OB))
  (DECLARE (IGNORABLE #:G252))
  (DECLARE (SB-PCL::%VARIABLE-REBINDING #:G252 OB))
  (SYMBOL-MACROLET ((A (SLOT-VALUE #:G252 &#39;A))
                    (MY-B (SLOT-VALUE #:G252 &#39;B))
                    (MY-C (SLOT-VALUE #:G252 &#39;C)))
    (SETF A 10
          MY-B 20
          MY-C 30)
    (FORMAT T &#34;the OB has a=~a, b=~a, c=~a~%&#34; A MY-B MY-C)))

</code></pre>

<p>Ah-hah! It&#39;s <code>symbol-macrolet</code>!</p>

<p>The symbol macrolet replaces instances of the symbols <code>a</code>, <code>my-b</code> and <code>my-c</code> with slot access forms within the body of <code>with-slots</code>. We may profit from the same technique in the construction of <code>with-plist</code>.</p>

<p>But first, an hypothetical example of <code>with-plist</code> in use:</p>

<h2 id="using-with-plist">Using WITH-PLIST</h2>

<p>Consider the following example:</p>

<pre><code class="language-lisp">
(let ((pl
        (list &#39;name &#34;Buckaroo Banzai&#34;
              :age 29
              :|currentJob| &#34;Astro-Spy Rocker&#34;)))
  (with-plist (name (age :age) (job :|currentJob|)) pl 
    (incf age) 
    (format t &#34;~a the ~a had a birthday and is now ~a years old~%&#34; 
            name job age) 
    pl))
;; prints out
;; Buckaroo Banzai the Astro-Spy Rocker had a birthday and is now 30 years old

;; and returns
;; (NAME &#34;Buckaroo Banzai&#34; :AGE 30 :|currentJob| &#34;Astro-Spy Rocker&#34;)

</code></pre>

<p>Here you can see that <code>with-plists</code> should be able to access keys of differing types, associate names for those accessors, and update the plist by referencing those names.</p>

<p>If the key is an ordinary symbol (e.g <code>name</code> above), then you can use that symbol itself to name the accessor. Otherwise, if the key is a keyword symbol (e.g. <code>:age</code> and <code>:|currentJob|</code> above), then some kind of local name should be provided.</p>

<p>Otherwise it works just like <code>with-slots</code>.</p>

<h2 id="a-draft-of-the-macro">A Draft of the Macro</h2>

<pre><code class="language-lisp">
(defmacro with-plist (keys plist &amp;body body)
  (let* ((plist-var
           (gensym))
         (macrolet-bindings
           (loop for term in keys
                 when (consp term)  
                   collect (destructuring-bind (var key) term
                             `(,var (getf ,plist-var &#39;,key)))
                 else
                   collect `(,term (getf ,plist-var &#39;,term)))))
    `(let ((,plist-var ,plist))
       (symbol-macrolet ,macrolet-bindings ,@body))))

</code></pre>

<p>The macro first determines the names of the <code>symbol-macrolet</code> bindings before binding each one to a <code>getf</code> form that accesses the plist. Thats it!</p>
]]></content:encoded>
      <guid>https://read.cicadas.surf/parenthetical-curios/with-plist</guid>
      <pubDate>Sun, 31 Jul 2022 12:08:19 +0000</pubDate>
    </item>
    <item>
      <title>Where&#39;s COND-LET?</title>
      <link>https://read.cicadas.surf/parenthetical-curios/imperative-cond</link>
      <description>&lt;![CDATA[Where&#39;s COND-LET?&#xA;&#xA;I use alexandria&#39;s when-let macro more than any other common utility. After that, the next most commonly used export is if-let. It seems to me that there is a natural extension of these two macros to a cond-let. Before getting into it, however, here is an example of if-let in action:&#xA;&#xA;(if-let (x (car some-list))&#xA;  (print x)&#xA;  (print &#34;the list was empty&#34;))&#xA;&#xA;In the above if-let behaves just like if in the case that (car some-list) is NIL. But in the case that some-list has a car, that value is bound to the variable X, which can then be used.&#xA;&#xA;when-let is just like when, but again, it lets you bind a variable as the result of the tested condition in case you want to use it later.&#xA;&#xA;But where oh where is cond-let? Shouldn&#39;t there be one? I have found myself writing a version of cond-let a number of times in the past, usually stashed in my project&#39;s utility module. Today we&#39;ll explore an implementation that relies on the imperative macro.&#xA;&#xA;But first &amp;#x2026;&#xA;&#xA;Why COND-LET ?&#xA;&#xA;Because sometimes you want to do something like this:&#xA;&#xA;(cond&#xA;  ((cadr xs)&#xA;   (let ((x (cadr xs)))&#xA;     (do-stuff-with x)))&#xA;  ((car xs)&#xA;   (let ((x (car xs)))&#xA;     (do-stuff-with x)))&#xA;  (t&#xA;   (do-stuff-with NIL)))&#xA;&#xA;The example, however contrived, illustrates an extension of when-let and if-let to the case of a cond pattern. The concept is that you often want to use the result a predicate&#39;s evaluation (here, more like a semipredicate) in the execution of code that is run on condition that predicate returned non-nil.&#xA;&#xA;Seeing COND-LET&#xA;&#xA;Here is a version of cond-let macro that I&#39;m calling imperative-cond-let.&#xA;&#xA;(defun do-list-stuff (xs) &#xA;  (imperative-cond-let &#xA;   ((:= x (cadr xs)) &#xA;    (list :cadr x))&#xA;   ((:= x (car xs)) &#xA;    (list :car x)) &#xA;   (t &#xA;    &#34;it&#39;s NIL!&#34;)))&#xA;&#xA;;; calling do-list-stuff&#xA;&#xA;  (do-list-stuff (list 1 2))&#xA;(:CADR 2)&#xA;&#xA;  (do-list-stuff (list 1))&#xA;(:CAR 1)&#xA;&#xA;  (do-list-stuff nil)&#xA;&#34;it&#39;s NIL!&#34;&#xA;&#xA;If you include several variables in those binding forms then the clause will execute in the case that each variable has a non-nil value:&#xA;&#xA;(defun add-first-two (xs) &#xA;  (imperative-cond-let &#xA;    ((:= x (cadr xs) &#xA;         y (car xs))&#xA;     (+ x y))&#xA;    ((:= x (car xs)) x)&#xA;    (t &#xA;     0)))&#xA;&#xA;;; calling add-first-two&#xA;  (add-first-two (list 1 2))&#xA;3&#xA;  (add-first-two (list 1))&#xA;1&#xA;  (add-first-two (list ))&#xA;0&#xA;&#xA;Defining IMPERATIVE-COND-LET&#xA;&#xA;(defmacro imperative-cond-let (&amp;body clauses)&#xA;  (let ((imperative-body &#xA;          (loop&#xA;            for (bindings . body) in clauses&#xA;            for vars = (unless (eq t bindings)&#xA;                         (loop for var in (rest bindings) by #&#39;cddr&#xA;                               collect var))&#xA;            collect bindings&#xA;            collect `(when (and ,@vars) &#xA;                       (return (progn ,@body))))))&#xA;    `(imperative ,@imperative-body)))&#xA;&#xA;I like this implementation because of its parsimony. For each clause, the variables bound using the convensions of the imperative macro are extracted. They are then check to all be be non-nil via (and ,@vars) and, when they are, the clause body is run and returned. In the case of a clause beginning with t, no variables are collected so that (and) is called, which returns t.&#xA;&#xA;Try it out yourself! Use macroexpand to see what it expands into.&#xA;&#xA;\#commonlisp]]&gt;</description>
      <content:encoded><![CDATA[<h1 id="where-s-cond-let">Where&#39;s COND-LET?</h1>

<p>I use <a href="https://gitlab.common-lisp.net/alexandria/alexandria" rel="nofollow">alexandria&#39;s</a> <code>when-let</code> macro more than any other common utility. After that, the next most commonly used export is <code>if-let</code>. It seems to me that there is a natural extension of these two macros to a <code>cond-let</code>. Before getting into it, however, here is an example of <code>if-let</code> in action:</p>

<pre><code class="language-lisp">(if-let (x (car some-list))
  (print x)
  (print &#34;the list was empty&#34;))
</code></pre>

<p>In the above <code>if-let</code> behaves just like <code>if</code> in the case that <code>(car some-list)</code> is <code>NIL</code>. But in the case that <code>some-list</code> has a <code>car</code>, that value is bound to the variable <code>X</code>, which can then be used.</p>

<p><code>when-let</code> is just like <code>when</code>, but again, it lets you bind a variable as the result of the tested condition in case you want to use it later.</p>

<p>But where oh where is <code>cond-let</code>? Shouldn&#39;t there be one? I have found myself writing a version of <code>cond-let</code> a number of times in the past, usually stashed in my project&#39;s utility module. Today we&#39;ll explore an implementation that relies on the <a href="https://read.cicadas.surf/parenthetical-curios/getting-imperative-orgacb6a8b" rel="nofollow">imperative macro</a>.</p>

<p>But first …</p>

<h2 id="why-cond-let">Why COND-LET ?</h2>

<p>Because sometimes you want to do something like this:</p>

<pre><code class="language-lisp">
(cond
  ((cadr xs)
   (let ((x (cadr xs)))
     (do-stuff-with x)))
  ((car xs)
   (let ((x (car xs)))
     (do-stuff-with x)))
  (t
   (do-stuff-with NIL)))

</code></pre>

<p>The example, however contrived, illustrates an extension of <code>when-let</code> and <code>if-let</code> to the case of a <code>cond</code> pattern. The concept is that you often want to use the result a predicate&#39;s evaluation (here, more like a <a href="https://en.wikipedia.org/wiki/Semipredicate_problem" rel="nofollow">semipredicate</a>) in the execution of code that is run on condition that predicate returned non-nil.</p>

<h2 id="seeing-cond-let">Seeing COND-LET</h2>

<p>Here is a version of <code>cond-let</code> macro that I&#39;m calling <code>imperative-cond-let</code>.</p>

<pre><code class="language-lisp">
(defun do-list-stuff (xs) 
  (imperative-cond-let 
   ((:= x (cadr xs)) 
    (list :cadr x))
   ((:= x (car xs)) 
    (list :car x)) 
   (t 
    &#34;it&#39;s NIL!&#34;)))

;; calling do-list-stuff

&gt; (do-list-stuff (list 1 2))
(:CADR 2)

&gt; (do-list-stuff (list 1))
(:CAR 1)

&gt; (do-list-stuff nil)
&#34;it&#39;s NIL!&#34;

</code></pre>

<p>If you include several variables in those binding forms then the clause will execute in the case that each variable has a non-nil value:</p>

<pre><code class="language-lisp">(defun add-first-two (xs) 
  (imperative-cond-let 
    ((:= x (cadr xs) 
         y (car xs))
     (+ x y))
    ((:= x (car xs)) x)
    (t 
     0)))

;; calling add-first-two
&gt; (add-first-two (list 1 2))
3
&gt; (add-first-two (list 1))
1
&gt; (add-first-two (list ))
0
</code></pre>

<h2 id="defining-imperative-cond-let">Defining IMPERATIVE-COND-LET</h2>

<pre><code class="language-lisp">
(defmacro imperative-cond-let (&amp;body clauses)
  (let ((imperative-body 
          (loop
            for (bindings . body) in clauses
            for vars = (unless (eq t bindings)
                         (loop for var in (rest bindings) by #&#39;cddr
                               collect var))
            collect bindings
            collect `(when (and ,@vars) 
                       (return (progn ,@body))))))
    `(imperative ,@imperative-body)))

</code></pre>

<p>I like this implementation because of its parsimony. For each clause, the variables bound using the convensions of the <code>imperative</code> macro are extracted. They are then check to all be be non-nil via <code>(and ,@vars)</code> and, when they are, the clause body is run and returned. In the case of a clause beginning with <code>t</code>, no variables are collected so that <code>(and)</code> is called, which returns <code>t</code>.</p>

<p>Try it out yourself! Use <code>macroexpand</code> to see what it expands into.</p>

<p>#commonlisp</p>
]]></content:encoded>
      <guid>https://read.cicadas.surf/parenthetical-curios/imperative-cond</guid>
      <pubDate>Sat, 30 Jul 2022 11:54:32 +0000</pubDate>
    </item>
    <item>
      <title>Getting Imperative</title>
      <link>https://read.cicadas.surf/parenthetical-curios/getting-imperative-orgacb6a8b</link>
      <description>&lt;![CDATA[Getting Imperative&#xA;&#xA;A few weeks ago I was messing around with CLOG when I noticed a peculiar pattern. In several the excellent examples that ship with CLOG, the let form is used to emulate an imperative style of programming. A good example of this can be found here. The faint putrescence of a code smell wafted up from that example, and, I thought, &#34;Darn it, it should be better. It can be better!&#34;&#xA;&#xA;Isn&#39;t Common Lisp a multi-paradigm language? Why shouldn&#39;t this imperative pattern be expressed more naturally?&#xA;&#xA;The result of my curiosity is a rough draft of a macro, called imperative.&#xA;&#xA;The IMPERATIVE Macro&#xA;&#xA;The imperative macro allows for the interleaving of binding forms with arbitrary non-binding forms without deep nesting. Here is how it might be used:&#xA;&#xA;(imperative &#xA;  (:= x 10 &#xA;      y (+ x 20) &#xA;      z (+ x 100)) &#xA;  (print (list x y z))&#xA;  (setf x 100) &#xA;  (print (list :x x))&#xA;  (:= x 101) &#xA;  (print (list :x x)) &#xA;  (list x y z))&#xA;&#xA;;; which would print&#xA;;; (10 30 110) &#xA;;; (:X 100) &#xA;;; (:X 101) &#xA;&#xA;;; then return &#xA;;; (101 30 110)&#xA;&#xA;Under the hood, the macro just nests LET forms. For example, a macroexpand-1 of the above yeilds:&#xA;&#xA;(BLOCK NIL&#xA;  (LET ((X 10) (Y (+ X 20)) (Z (+ X 100)))&#xA;    (PRINT (LIST X Y Z))&#xA;    (SETF X 100)&#xA;    (PRINT (LIST :X X))&#xA;    (LET ((X 101))&#xA;      (PRINT (LIST :X X))&#xA;      (LIST X Y Z))))&#xA;&#xA;The whole thing is inside of a BLOCK so that you can return early if you wish by calling (return &amp;optional value) anywhere inside.&#xA;&#xA;The macro&#xA;&#xA;It&#39;s not perfect, but here&#39;s the macro in its current form:&#xA;&#xA;(defmacro imperative (&amp;body body)&#xA;   &#34;Evaluate expressins in BODY in sequence. Expressions that look&#xA; like (:= VAR1 EXPR1 ...) will expand into a LET form whose bindings&#xA; are valid for the rest of the BODY&#xA;&#xA; E.g.&#xA;&#xA; (imperative &#xA;   (format t \&#34;Welcome to IMPERATIVE\&#34;) &#xA;   (terpri)&#xA;   (:= x 10 z (+ x 20))&#xA;   (format t \&#34;X = ~a, Z = ~a~%\&#34; x z)&#xA;   (:= y (+ z 20))&#xA;   (format t \&#34;Y = ~a~%\&#34; y)&#xA;   (list x y z))&#xA;&#xA; would evaluate to:&#xA;&#xA; Welcome to IMPERATIVE    ;; &lt;-- printed to stdout&#xA; X = 10, Z = 30&#xA; Y = 50&#xA;&#xA;(10 50 30)     ;; &lt;-- return value&#xA;&#xA;IMPERATIVE introduces an implicit, anonymous BLOCK, and hence can be&#xA;returned from.&#xA; &#34;&#xA;   (labels ((binding-form-p (form)&#xA;              (and (consp form)&#xA;                   (keywordp (first form))&#xA;                   (eql := (first form))))&#xA;            (collect-bindings (bindings)&#xA;              (loop for (var expr . more) on bindings by #&#39;cddr&#xA;                    collect (list var expr)))&#xA;            (expander (body)&#xA;              (cond&#xA;                ((null body) body)&#xA;                ((binding-form-p (first body))&#xA;                 (list (list &#39;let* (collect-bindings (rest (first body)))&#xA;                              (expander (rest body)))))&#xA;                (t&#xA;                 (cons (first body)&#xA;                       (expander (rest body)))))))&#xA;     `(block () ,@(expander body))))&#xA;&#xA;Good enough as a first draft.&#xA;&#xA;Deodorizing?&#xA;&#xA;A partial re-write of the smelly example is here:&#xA;&#xA;(imperative&#xA;  (:= last-tab nil&#xA;      t1  (create-button body :content &#34;Tab1&#34;)&#xA;      t2  (create-button body :content &#34;Tab2&#34;)&#xA;      t3  (create-button body :content &#34;Tab3&#34;))&#xA;  (create-br body)&#xA;&#xA;  (:=  p1  (create-div body)&#xA;       p2  (create-div body)&#xA;       p3  (create-div body :content &#34;Panel3 - Type here&#34;)&#xA;       f1  (create-form p1)&#xA;       fe1 (create-form-element&#xA;            f1 :text&#xA;            :label (create-label f1 :content &#34;Fill in blank:&#34;)))&#xA;  (create-br f1)&#xA;&#xA;  (:= fe2 (create-form-element&#xA;           f1 :color&#xA;           :value &#34;#ffffff&#34; &#xA;           :label (create-label f1 :content &#34;Pick a color:&#34;)))&#xA;  (create-br f1)&#xA;  (create-form-element f1 :submit :value &#34;OK&#34;)&#xA;  (create-form-element f1 :reset :value &#34;Start Again&#34;)&#xA;  ;; .. this is a long one, but you get the idea ...&#xA;  )&#xA;&#xA;I can&#39;t tell whether or not the deodorizor smells any better.]]&gt;</description>
      <content:encoded><![CDATA[<h1 id="getting-imperative">Getting Imperative</h1>

<p>A few weeks ago I was messing around with <a href="https://rabbibotton.github.io/clog/clog-manual.html" rel="nofollow">CLOG</a> when I noticed a peculiar pattern. In several the excellent examples that ship with CLOG, the <code>let*</code> form is used to emulate an imperative style of programming. A good example of this can be found <a href="https://github.com/rabbibotton/clog/blob/b24c728ee9163ac392c6f30c2687f97ac831f9c4/tutorial/09-tutorial.lisp" rel="nofollow">here</a>. The faint putrescence of a <a href="https://en.wikipedia.org/wiki/Code_smell" rel="nofollow">code smell</a> wafted up from that example, and, I thought, “Darn it, it should be better. It can be better!”</p>

<p>Isn&#39;t Common Lisp a multi-paradigm language? Why shouldn&#39;t this imperative pattern be expressed more naturally?</p>

<p>The result of my curiosity is a rough draft of a macro, called <code>imperative</code>.</p>

<h2 id="the-imperative-macro">The <code>IMPERATIVE</code> Macro</h2>

<p>The <code>imperative</code> macro allows for the interleaving of binding forms with arbitrary non-binding forms without deep nesting. Here is how it might be used:</p>

<pre><code class="language-lisp">(imperative 
  (:= x 10 
      y (+ x 20) 
      z (+ x 100)) 
  (print (list x y z))
  (setf x 100) 
  (print (list :x x))
  (:= x 101) 
  (print (list :x x)) 
  (list x y z))

;; which would print
;; (10 30 110) 
;; (:X 100) 
;; (:X 101) 

;; then return 
;; (101 30 110)
</code></pre>

<p>Under the hood, the macro just nests <code>LET*</code> forms. For example, a <code>macroexpand-1</code> of the above yeilds:</p>

<pre><code class="language-lisp">(BLOCK NIL
  (LET* ((X 10) (Y (+ X 20)) (Z (+ X 100)))
    (PRINT (LIST X Y Z))
    (SETF X 100)
    (PRINT (LIST :X X))
    (LET* ((X 101))
      (PRINT (LIST :X X))
      (LIST X Y Z))))
</code></pre>

<p>The whole thing is inside of a <code>BLOCK</code> so that you can return early if you wish by calling <code>(return &amp;optional value)</code> anywhere inside.</p>

<h2 id="the-macro">The macro</h2>

<p>It&#39;s not perfect, but here&#39;s the macro in its current form:</p>

<pre><code class="language-lisp">
(defmacro imperative (&amp;body body)
   &#34;Evaluate expressins in BODY in sequence. Expressions that look
 like (:= VAR1 EXPR1 ...) will expand into a LET* form whose bindings
 are valid for the rest of the BODY

 E.g.

 (imperative 
   (format t \&#34;Welcome to IMPERATIVE\&#34;) 
   (terpri)
   (:= x 10 z (+ x 20))
   (format t \&#34;X = ~a, Z = ~a~%\&#34; x z)
   (:= y (+ z 20))
   (format t \&#34;Y = ~a~%\&#34; y)
   (list x y z))

 would evaluate to:

 Welcome to IMPERATIVE    ;; &lt;-- printed to stdout
 X = 10, Z = 30
 Y = 50

(10 50 30)     ;; &lt;-- return value

IMPERATIVE introduces an implicit, anonymous BLOCK, and hence can be
returned from.
 &#34;
   (labels ((binding-form-p (form)
              (and (consp form)
                   (keywordp (first form))
                   (eql := (first form))))
            (collect-bindings (bindings)
              (loop for (var expr . more) on bindings by #&#39;cddr
                    collect (list var expr)))
            (expander (body)
              (cond
                ((null body) body)
                ((binding-form-p (first body))
                 (list (list* &#39;let* (collect-bindings (rest (first body)))
                              (expander (rest body)))))
                (t
                 (cons (first body)
                       (expander (rest body)))))))
     `(block () ,@(expander body))))
</code></pre>

<p>Good enough as a first draft.</p>

<h2 id="deodorizing">Deodorizing?</h2>

<p>A partial re-write of the <a href="https://github.com/rabbibotton/clog/blob/b24c728ee9163ac392c6f30c2687f97ac831f9c4/tutorial/09-tutorial.lisp" rel="nofollow">smelly example</a> is here:</p>

<pre><code class="language-lisp">
(imperative
  (:= last-tab nil
      t1  (create-button body :content &#34;Tab1&#34;)
      t2  (create-button body :content &#34;Tab2&#34;)
      t3  (create-button body :content &#34;Tab3&#34;))
  (create-br body)

  (:=  p1  (create-div body)
       p2  (create-div body)
       p3  (create-div body :content &#34;Panel3 - Type here&#34;)
       f1  (create-form p1)
       fe1 (create-form-element
            f1 :text
            :label (create-label f1 :content &#34;Fill in blank:&#34;)))
  (create-br f1)

  (:= fe2 (create-form-element
           f1 :color
           :value &#34;#ffffff&#34; 
           :label (create-label f1 :content &#34;Pick a color:&#34;)))
  (create-br f1)
  (create-form-element f1 :submit :value &#34;OK&#34;)
  (create-form-element f1 :reset :value &#34;Start Again&#34;)
  ;; .. this is a long one, but you get the idea ...
  )

</code></pre>

<p>I can&#39;t tell whether or not the deodorizor smells any better.</p>
]]></content:encoded>
      <guid>https://read.cicadas.surf/parenthetical-curios/getting-imperative-orgacb6a8b</guid>
      <pubDate>Fri, 29 Jul 2022 14:47:40 +0000</pubDate>
    </item>
  </channel>
</rss>