
\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:lisp-indent@aartaka.me}{(email)}}
\date{\today}
\title{All Lisp Indentation Schemes Are Ugly}
\makeatletter
\def\endenv{\expandafter\end\expandafter{\@currenvir}}
\makeatother
\begin{document}
\maketitle

\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{./assets/lisp-indent.png}

Once you get used to Lisp, you stop noticing the parentheses and rely on indentation instead.
That's partially why there are several alternative syntaxes based solely on indentation.
\href{run:wisp}{Like Wisp},
\href{https://srfi.schemers.org/srfi-110}{or sweet expressions}.
But then, the question stands: how to indent the code, actually?
Especially so—in Lispy syntax.

\section*{No Indent: One Can Only Go So Far} \label{no-indent}

A solution that will likely satisfy a proponent of any indentation style:
"Just put it all on one line lol."
Of course, lines are not infinitely readable and there's a column cap,
(whether natural or enforced.)
So this line (adapted from
\href{https://github.com/aartaka/cl-blc}{cl-blc},)
while devoid of indentation problems, is unreadable:

\begin{figure}[h!]\begin{verbatim}
(list (tree-transform-if predicate transformer (first tree) depth) (tree-transform-if predicate transformer (second tree) depth))
\end{verbatim}\caption{Absurdly long line of a Lisp code}\end{figure}

That's the problem statement: some forms need multiple lines and indentation.
But what kind of indentation?

\section*{(Dis)Functional Aligned Indent} \label{function-indent}

There's an established style of indentation: align the function arguments on the same column:

\begin{figure}[h!]\begin{verbatim}
(list (tree-transform-if predicate transformer (first tree) depth)
      (tree-transform-if predicate transformer (second tree) depth))
(inc! d (/ (* (mtx:get x i k)
              (mtx:get x j k))
           (1+ (* (vec:get dl l)
                  (vec:get eval k)))))
(let ((s (if (< j i) j i))
      (l (if (< j i) i j)))
  (+ (* s 1/2 (- (* 2 d-size) (1+ s)))
     l
     (- s)))
\end{verbatim}\caption{Examples of function-like indentation}\end{figure}

This style if useful in reflecting the code structure: just look at what's indented and what's outdented.
It works especially well for short function/macro/form names, like \verb|+| or \verb|vec:get|.
Not so well for long ones:

\begin{figure}[h!]\begin{verbatim}
(tree-transform-if predicate
                   transformer
                   (second tree)
                   depth)
\end{verbatim}\caption{A problematic function indentation}\end{figure}

Nineteen!
Nineteen spaces of indentation!
It's getting unruly.
Such an indent, when used in deeply nested code, makes it too wide and unreadable.
If you add the strict one-per-line alignment of arguments, it's also painfully long line-wise.
Let's handle the verticality first:

\section*{Space-filling Indent} \label{filling-indent}

No sane Lisper would write a \verb|loop| with every keyword on its own line:

\begin{figure}[h!]\begin{verbatim}
(loop for
      i
      below
      10
      collect
      i)
\end{verbatim}\caption{Ugly loop}\end{figure}

(Some pretty-printers do that too (I'm looking at you, ECL!), but that's a topic for another day.)

We don't have to put every argument on its own line.
That's the intuition behind the space-filling indent:

\begin{figure}[h!]\begin{verbatim}
(loop for i below 10
      collect i)
\end{verbatim}\caption{Less ugly loop}\end{figure}

One can go as far as splitting the argument list in arbitrary places.
Regardless of semantics.
(Looking at you, SBCL!)
Putting some keyword arguments on the first line, and then some on the second/third/etc.
This utilizes the space efficiently enough to be used.
But what if one's stuck really deep in nesting levels?

\section*{Sick Macro Indent} \label{macro-indent}

Now what I'm about to suggest is likely not to your taste:

\begin{figure}[h!]\begin{verbatim}
(tree-transform-if
 predicate transformer (second tree) depth)
\end{verbatim}\caption{My indentation style suited for deeply nested code}\end{figure}

This style of indentation
(putting function name on one line, and arguments on the other)
was frowned upon more than once in my practice:

\begin{itemize}\item It messes up with nesting identification: one space is not enough.
\item It makes the code too vertical.
\item And it certainly isn't idiomatic.
\end{itemize}

But what this style achieves is perfect indentation control.
You only get one space of indentation per form.
Complex algorithms are easier to read when written in this style.

And!
This style also plays well with most indentation tools, even the simplest ones.
I had this situation more than once:

\begin{itemize}\item Writing a \verb|with-*| macro in Scheme;
\item Using it;
\item And realizing that Emacs/Geiser indentation functions think that this macro is a procedure.
\end{itemize}

Indenting all the arguments of the \verb|progn|/\verb|begin|-like macro in this sick style helps:

\begin{figure}[h!]\begin{verbatim}
;; From
(mtx:with-column (uab-col uab index-ab)
                 (mtx:set!
                  ppab 0 index-ab
                  (blas:dot hi-hi-eval uab-col)))
;; To
(mtx:with-column
 (uab-col uab index-ab)
 (mtx:set!
  ppab 0 index-ab
  (blas:dot hi-hi-eval uab-col)))
\end{verbatim}\caption{Sick indent helps you to manage macros}\end{figure}

\section*{Another Solution: Threading Macros} \label{threading}

I worked on a deeply nested corporate codebase functions.
In Clojure.
And I realized why threading macros exist.
To make the logic more sequential and readable, true.
But also to tame excessive nesting!

Unfortunately, threading/arrow macros don't always work.
Common Lisp in particular is extremely unfriendly to threading macros.
Arrows imply a consistent thread-first or thread-last functions.
But CL's standard lib is too inconsistent for that to work.
So we're left with picking an indentation style we don't necessarily like.

\section*{What Is Your Favorite Style?} \label{favorite}

I often prefer the macro-like indentation, because I sometimes write deeply nested code.
But I see the value in all the other indentation schemes!


\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}
