
\documentclass[12pt]{article}
\usepackage[T2A,OT1]{fontenc}
\usepackage[default]{cantarell}
\usepackage[a4paper, top=20mm, bottom=20mm, left=20mm, right=20mm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[russian, english]{babel}
\usepackage{tabu}
\usepackage{hyperref}
\usepackage{parskip}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage[normalem]{ulem}
\usepackage{float}
\floatstyle{boxed}
\restylefloat{figure}
\usepackage{setspace}
\onehalfspacing
\author{Artyom Bologov \href{mailto:cl-is-lots@aartaka.me}{(email)}}
\date{\today}
\title{Common Lisp Is Not a Single Language, It Is Lots}
\makeatletter
\def\endenv{\expandafter\end\expandafter{\@currenvir}}
\makeatother
\begin{document}
\maketitle

\begin{figure}[h!]\begin{quote}
There’s no such thing as C++. C++ is a heap of sloppily stiched languages. Everyone uses a different “C++”, picking features from either of these.
\end{quote}\caption{Vasily Gerasimov, conversation, rephrased}\end{figure}

Being a part of the Lisp community, I often see a talk of “Lisp” the language.
\href{https://reddit.com/r/lisp/comments/1c4pls3/what_do_they_mean_by_lisp}{Like here, for example}.
What is meant by “Lisp” is often uncertain.
But Common Lisp seems to be the most worthy contender for the title.
It’s “Common”, after all.
One true Lisp to rule them all.

But Lisp is a family of languages.
“Lisp” might mean anything else—Scheme(s), Clojure, original LISP, and any of its descendants.
So there’s no “The Lisp”, and implying it is misleading.

But there is “The Common Lisp”, right?
It’s a standard well-defined language with a name to it.
It’s one coherent thing, one can say.

It’s not.
In this post, I’ll try to show how many languages there are in CL.
Hopefully helping one to understand why saying “Common Lisp is bad”
or “Common Lisp is beautiful” is always playing with the (mis)conception of CL others have.

I’ll list the example code in each of these.
The example snippet will be a simplistic implementation of
\href{https://geminiprotocol.net/docs/gemtext.gmi}{Gemtext} parser.
Don’t expect super correct and beautiful code.
I’m doing it for the sake of example, after all.

\section*{The Language?} \label{the-language}

Probably the biggest thing that “proves” Common Lisp integrity is the book.
“Common Lisp The Language”.
Funnily enough, Wikipedia says

\begin{figure}[h!]\begin{quote}
Common Lisp the Language is a reference book by Guy L. Steele about a set of technical standards and programming languages (sic) named Common Lisp.
\end{quote}\caption{Wikipedia saying that Common Lisp is a set of languages}\end{figure}

But let’s see whether there is The Language in the book.
The book is 1000 pages long.
And I’m not able to focus on anything longer than 30 pages.
So I’ll take a different direction: that of a programmer exploring the API.
The API of \verb|trivial-cltl2|, the CLtL portability library.
Here’s a listing of symbols that
\verb|trivial-cltl2| exports in addition to standard ones:

\begin{figure}[h!]\begin{verbatim}
(:export #:compiler-let
         #:variable-information
         #:function-information
         #:declaration-information
         #:augment-environment
         #:define-declaration
         #:parse-macro
         #:enclose)
\end{verbatim}\caption{Trivial CLtL2 symbols}\end{figure}

And that’s it.
Not much.
CLtL2 is the same as ANSI CL.

But notice what the list is lacking: the notion of objects.
So CLtL2 (as both the book and the language), although including CLOS and other niceties discussed below...
Is not object-oriented at its initial core.
And all the mentions of CLOS etc. are only happening near the end of the book.
An epilogue.
Appendix.
Erratum.

So we have our first language (out of many): CLtL.
A language built out of variables, functions, and maybe structures.
Like Scheme, but batteries included.
Small and neat nonetheless.
Here’s how our parser could look in The Language:

\begin{figure}[h!]\begin{verbatim}
;; We don’t necessarily need structs if we use lists.
(defstruct element
  (type :paragraph :type keyword)
  (content "" :type string))

(defun parse-elements (stream)
  (if (not (peek-char nil stream nil nil))
      '()
      (cons
       (case (peek-char nil stream)
         (#\= (let ((line (read-line stream)))
                ;; Yes, I know links have text, but bear with me.
                (if (uiop:string-prefix-p "=> " line)
                    (make-element :type :link :content (subseq line 3))
                    (make-element :content line))))
         (&num\> (make-element :type :quote :content (subseq (read-line stream) 2)))
         ;; Yes, headings have level. But oh well.
         (#\# (make-element :type :heading :content (subseq line 2)))
         (t (make-element :content (read-line stream))))
       (parse-elements stream))))
\end{verbatim}\caption{Example Gemtext parser in core Common Lisp}\end{figure}

No fancy DSLs, no Turing-complete APIs, no OOP.
Speaking of which, another Common Lisp language:

\section*{Common Lisp Object System} \label{clos}

OOP is but a one approach to building Lisp software.
There are CL codebases built entirely out of plain functions.
Even the ones built entirely in a Scheme-y functional style (I worked with some.)
There are others built out of objects and methods on them.
There are ones that are built around
\href{run:lisp-design-patterns}{generics and protocols (section protocols)}.
And it’s quite hard to work with either if one’s typical style is not the same as that of the codebase.

So here goes our first non-CLtL language: CLOS.
Defining classes and methods on them:

\begin{figure}[h!]\begin{verbatim}
(defclass base-element ()
  ((content :type string
            :initarg :content)))

(defclass paragraph-element (base-element) ())
(defclass quote-element (base-element) ())
(defclass link-element (base-element) ())
(defclass heading-element (base-element)
  ((level :type integer
          :initarg :level)))

(defmethod parse-elements-clos ((stream stream))
  (if (not (peek-char nil stream nil nil))
      '()
      (cons
       (case (peek-char nil stream)
         (#\= (let ((line (read-line stream)))
                (if (uiop:string-prefix-p "=> " line)
                    (make-instance 'link-element :content (subseq line 3))
                    (make-instance 'paragraph-element :content line))))
         (#\> (make-instance 'quote-element :content (subseq (read-line stream) 2)))
         (#\# (make-instance 'heading-element :level 1 :content (subseq (read-line stream) 2)))
         (t (make-instance 'paragraph-element :content (read-line stream))))
       (parse-elements-clos stream))))

(defmethod parse-elements-clos ((string string))
  (with-input-from-string (s string)
    (parse-elements-clos s)))
\end{verbatim}\caption{CLOS-heavy Gemtext parser}\end{figure}

The code is more verbose than that in The Language.
Yet it’s also more reliable and introspectable.
All these classes add type checking and convenient slot access with e.g. \verb|with-slots|.
The behavior is more overridable with \verb|:around| methods.
And the code can be extended with more classes.

I must admit that I cheated by reusing the code from above.
So the parser stays kind of the same procedural piece of code.
It’s enhanced with more types and MOP introspection.
But it still is procedural, because OOP
(not the CLOS, but the general idea)
is merely a facade over procedural programming,
encapsulating the procedural behavior into classes.
One can implement it with methods and character dispatch.
But that’s no longer a CLOS-y code, it’s...

\section*{Generics And Protocols} \label{generics-and-protocols}

Generics are part of CLOS, right?
Why make them a separate “language” then?
My reasoning is: one can program anything in them without touching classes.
And if you can, then it’s a separate language alright.

Generics shine the most when there’s a library intended for extension.
Like lots of libraries by Shinmera or Robert Strandh, where there’s

\begin{itemize}\item A general protocol.
\item And implementation-specific extensions to it with the actual behavior.
\end{itemize}

Here’s a weird version of the Gemtext parser with generics:

\begin{figure}[h!]\begin{verbatim}
(defgeneric parse-elements-generic (stream state char)
  (:method ((stream stream) (state t) (char null))
    state)
  (:method ((stream stream) (state null) (char null))
    (nreverse
     (parse-elements-generic stream nil (peek-char nil stream nil nil)))))

(defmethod parse-elements-generic ((stream stream) (state list) (char (eql #\=)))
  (let ((line (read-line stream)))
    (parse-elements-generic
     stream
     (cons (if (uiop:string-prefix-p "=> " line)
               (list :link (subseq line 3))
               (list :paragraph line))
           state)
     (peek-char nil stream nil nil))))

(defmethod parse-elements-generic ((stream stream) (state list) (char (eql #\#)))
  (parse-elements-generic
   stream
   (cons (list :heading (subseq (read-line stream) 2))
         state)
   (peek-char nil stream nil nil)))

(defmethod parse-elements-generic ((stream stream) (state list) (char (eql #\>)))
  (parse-elements-generic
   stream
   (cons (list :quote (subseq (read-line stream) 2))
         state)
   (peek-char nil stream nil nil)))

(defmethod parse-elements-generic ((stream stream) (state list) (char character))
  (parse-elements-generic
   stream
   (cons (list :paragraph (read-line stream))
         state)
   (peek-char nil stream nil nil)))
\end{verbatim}\caption{Generic-heavy Gemtext parser}\end{figure}

That’s where the example stops being useful.
Generic style is nice, but this piece of code is by no means representative.
I must’ve used classes for elements.
I must’ve made a simpler protocol.
I must’ven’t used lists for state.
\href{https://yogthos.net/posts/2022-12-18-StructuringClojureApplications.html}{I might have used state machine to drive the parsing}.

But even this ugly code has the benefits of generics:

\begin{itemize}\item It is easy to extend: just dispatch over the new character.
\item It provides a strict protocol with type checking—if something goes wrong, a \verb|no-applicable-method| signals and shows the problem.
\item It makes the behaviors compose nicely and puts recursion termination into the method of its own.
\end{itemize}

Try generics for your new projects.
They are nicer than I represented them here.
Speaking of “nicer”, how about some syntactic sugar?

\section*{Loop Macro} \label{loop}

Using \verb|loop| is always a risk: some Lispers love it, and some hate it.
But one thing is undeniable: it’s powerful and reads like English.
Well, given that it’s used right.

And no, it’s not just “another iteration construct”.
\verb|loop| has:

\begin{itemize}\item Symbol binding.
\item Conditionals.
\item Iteration over (almost) all the standard data types.
\item Data aggregation.
\end{itemize}

So \verb|loop| is quite a free-standing entity.
I’ve proof:
\href{https://github.com/aartaka/advent-of-code-in-cl-loop}{I completed two weeks of Advent of Code in it}.

So let’s try implementing this simple Gemtext parser as one huge loop:

\begin{figure}[h!]\begin{verbatim}
(defun parse-elements-loop (stream)
  (loop for char = (peek-char t stream nil nil)
        while char
        when (eql #\= char)
          collect (list :link (subseq (read-line stream) 3))
        else when (eql #\> char)
               collect (list :quote (subseq (read-line stream) 2))
        else when (eql #\# char)
               collect (list :heading (subseq (read-line stream) 2))
        else collect (list :paragraph (read-line stream))))
\end{verbatim}\caption{Gemtext parser as a single \verb|loop|}\end{figure}

Beautiful, isn’t it?
Ugly, isn’t it?
Love it or hate it, it’s a nice readable syntax.
And you can advertise this small language to your C-family friends.
“This is The Lisp, try it!”
I did this once, and it kinda worked...

\section*{Clojure?!} \label{clojure}

The legend goes: Rich Hickey made Clojure after being irritated by ABCL.
So Clojure feels (after programming some sufficiently big projects in it)...
Lispy (sorry for the linguistic abuse here).
Like an opinionated standard lib over CL.
Some reader macros here and there.
Square and curly braces.
Some nicer functional programming constructs.
More consistency and less burden of the past.
A fancy OOP system based on interfaces.

We could’ve had that in CL.
And there
\href{https://cl21.org}{are projects}
\href{https://github.com/ciel-lang/CIEL}{enabling a “nicer CL”},
\href{https://github.com/ruricolist/cloture}{or even outright implementing Clojure in CL}!
Many CL programmers squint at them.
These extensions are going “too far” making a different language out of CL.
Yes, yes, yet another Common Lisp language.

\section*{Notable Mention: Format} \label{format}

\verb|format| function is an enormous text formatting DSL with

\begin{itemize}\item Conditionals.
\item Iteration.
\item Pretty-printing.
\item Recursive function calls.
\item And something I surely forgot to mention.
\end{itemize}

Here is how one of my \verb|format| strings looks like:

\begin{figure}[h!]\begin{verbatim}
"~s (~a bit~:p):
#b~b, #o~o, #x~x~
~{~\&Universal time: ~2,'0d:~2,'0d:~2,'0d ~
~[~;Jan~;Feb~;Mar~;Apr~;May~;Jun~;Jul~;Aug~;Sep~;Oct~;Nov~;Dec~] ~
~a~[th~;st~;nd~;rd~:;th~], year ~a.~} ~
~{~\&Approximate UNIX time: ~2,'0d:~2,'0d:~2,'0d ~
~[~;Jan~;Feb~;Mar~;Apr~;May~;Jun~;Jul~;Aug~;Sep~;Oct~;Nov~;Dec~] ~
~a~[th~;st~;nd~;rd~:;th~], year ~a.~}"
\end{verbatim}\caption{A regular format string, right?}\end{figure}

Horrible, but it gets the job done.
And it’s much more effective than writing 100+ lines of printing code.

So while I’m hesitant about calling
\verb|format| another Common Lisp language...
It is alien enough to be one.

See also:
\href{https://cs-www.cs.yale.edu/homes/dvm/format-stinks.html}{a case against format}!

\section*{Conclusion} \label{conclusion}

There’s no Common Lisp The Language.
There’s a set of languages sloppily stitched together.
Well, less sloppy than C++ anyway.
Agree or disagree, these are quite orthogonal to each other:

\begin{itemize}\item Core Scheme-y Common Lisp.
\item CLOS.
\item Generics.
\item \verb|loop| macro
\item Clojure and Scheme?!
\item \verb|format| strings.
\end{itemize}

A lot of sub-languages for one (sorry) “language”, right?

(Drafts of this post also included sections on namespaces and OS interfaces.
I decided to drop them to save space and because they weren’t adding much.
Lots of languages!)


\par\noindent\rule{\textwidth}{0.4pt}
\href{https://creativecommons.org/licenses/by/4.0}{CC-BY 4.0} 2022-2026 by Artyom Bologov (aartaka,)
\href{https://codeberg.org/aartaka/pages/commit/a91befa}{with one commit remixing Claude-generated code}.
Any and all opinions listed here are my own and not representative of my employers; future, past and present.
\end{document}
