Skip to content
Alessandro Solbiati

ANSI Common Lisp with Clojure

programming, Clojure, Lisp2 min read

I have always been fascinated by obscure technologies, after reading Paul Graham essays about ViaWeb I decided to spend the weekend to learn lisp.

A first look with Practical Common Lisp

Being a strong vim user I have mixed feeling about using emacs for writing Lisp. Turns out you need to use emacs, since is actually built in Lisp. I am following Practical Common Lisp and the Common Lisp Cookbook to get started (since getting a hold on Paul Graham "Ansi Common Lisp" book is surprisingly hard. Together with the book I am also followinig 6.001 Structure And Interpretation of Computer Program, a course from MIT from 1986 about Lisp.

My firsts attempt is building a toy database query language (e.g. SQL) in Lisp. I am trying to do it along side with python, and I am pushing the limits of its functional programming style: In Lisp we are writing closures for WHERE clauses, and we construct SELECT statements that passes around those closures. With Python I am applying filters passing around lambda functions and so far it holds. But if we try to write UPDATE statements, keeping Python functional starts to make the code totally unreadable. On the other hand, Lisp is code still looks pretty clean: we use funcall to apply the WHERE closure.

Better setup with Clojure

As Paul Graham mentioned on the 18th March 2020, the go-to option to learn Lisp is the Lisp-dialect Clojure. One interesting features is that Clojure compiles natively to the JVM and also to Javascript with ClojureScript.

Clojure.

— Paul Graham (@paulg) March 18, 2020

I set up my VIM using vim-fireplace, that provides Clojure REPL support. As explained at clojure-doc.org, we can make vim speak with a lein repl, having a bunch of features like :cpr to reload the code in the REPL, :gf goes to function definition, :cpp to evaluate the form under the cursor in the REPL and :cqp to open a quasi-REPL in vim. Here is a cheatsheet for all the commands.

2. Learning the syntax (Chapters 1 to 5)

We get introduced right away to the concept that Lisp programs are expressed as lists: Lisp programs can generate Lisp code. A interesting point of different is between Lisp list and Clojure sequences, you use first instead of car and rest instead of cdr. Even with this basic concept the syntax between the two dialects varies moderately, here is the Clojure code from chapter two. I went on up to chapter 5 translating all the Lisp example in Clojure. Nothing remarkable, beside learning that in Clojure they don't actually have the Lisp cons operator, but they have equivalents (cons, conj, concat).

3. Deep Dive in the Functional (Chapter 6+)

From Chapter6 we have a fun exercise of implementing the language Dylan, a Lisp with some elaborate functional operators like compose, conjoin, and disjoin.