Clojure is a great language and has a great community surrounding it. It is opinionated, and if you want to get down to business and write code that solves problems, it mostly just gets out of your way.
It also lets me write lisp at work instead of Java!
Clojure has also allowed me to become less intimidated with lisp in general, and when I started working on a personal hobby project I decided to pick up another lisp language. There are however quite a few lisp languages out there. I seriously considered guile scheme, chicken scheme and common lisp and decided to go with common lisp at the end.
This blog post and some more subsequent posts will deal with learning common lisp when you are already comfortable with clojure (or any other lisp actually).
I will try to refrain from making arguments that one language is better than another. Both the languages are tools, so use the tool that you prefer and which allows you to solve the problem best.
What is Common Lisp?
Common lisp refers to languages that conforms to the ANSI specification in document ANSI INCITS 226-1994 (R2004) (formerly X3.226-1994 (R1999). There are several implementation of the common lisp specification like Steel Bank Common Lisp (sbcl
), Clozure CL (ccl
), etc. There are also paid implementations like Allegro Common Lisp, LispWorks, etc. This is different from Clojure, which is both a specification and the language.
This page provides a list & comparison of implementations currently still relevant.
This means the first decision you will have to make when you decide to use Common Lisp, is which implementation should you use? Each implementation has it’s own set of features. Features like if they support threads, compile to native binary, etc.
General advice with lispers is for beginners to pick SBCL. It is still being actively developed/maintained, provides threads, comes with a native compiler, can generate executable. This checks all the boxes for me.
I assume all examples from here on are run in SBCL
Getting Started
First step is to install SBCL.
The Getting Started page on SBCL home page should help you with this. On Arch linux, Fedora or Ubuntu you could install the sbcl
package. On OS X, sbcl
is also available on brew
or macports. For Windows there is LispStick. edit: There is also portacle which runs on Windows and seems to be better maintained
Second step would be to setup an IDE environment.
I use Emacs for clojure development, and I can’t think of any reason to switch away from Emacs for lisp development. Packages like smartparens
, rainbow-delimiters
, etc makes lisp development a pleasure in emacs.
Along with Emacs, there is also SLIME, which stands for “Superior Lisp Interaction Mode for Emacs”, from what I’ve read about it so far is that it is what CIDER for Clojure strives to be!
We will also be installing QuickLisp, which is a package manager for Common Lisp, this will allow you to pull other CL (Common Lisp) libraries from the internet and use it in your application. Quicklisp builds on another tool called asdf
, which stands for Another System Definition Facility. asdf
with quicklisp
can provide features similar to lein
or boot
.
This page will guide you through setting up SLIME and QuickLisp. (asdf
comes packaged with sbcl
)
Once you have sbcl
and QuickLisp setup you should be ready to start writing some code!
REPL
To start the REPL, you can run sbcl
, you should see something like this:
$ sbcl
This is SBCL 1.5.3, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.
SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses. See the CREDITS and COPYING files in the
distribution for more information.
*
The *
is the REPL prompt!
You may want to use rlwrap to make navigating between commands, and search for previous repl evaluations easier. To do this, start sbcl like this:
rlwrap sbcl
Go ahead and try some simple commands:
;; add some numbers
(+ 1 4)
;; print some stuff
(print "abcd")
;; concatenate some strings
(concatenate 'string "string" " together " "some" " strings.")
;; declare some global variable
(defparameter *x* 10) ;; clojure def
(defvar *y* 2) ;; clojure defonce
;; format a string
(format t "x is ~d and y is ~d" *x* *y*)
First Common Lisp file
In clojure, you are required to have your source files in a specific directory structure (depending on the namespace). However in Common Lisp, there are no such restrictions, you can type in some lisp code in a file and load it into your repl, and access its functions, variables, etc.
So let us create a file called first.lisp
as follows:
;; first.lisp
(defun first-fn (x)
x)
(defun is-factor-p (m n)
(zerop (rem m n)))
now open up your repl and try out the functions from your file as follows (make sure you navigate to where you have first.lisp
saved first):
* (load "first")
T
* (first-fn 1)
1
* (first-fn "abc")
"abc"
* (is-factor-p 4 2)
T
* (is-factor-p 4 3)
NIL
Getting Started with SLIME
You can open first.lisp
in emacs and then M-x slime
. This will start up the SLIME REPL.
In your first.lisp
buffer, you can C-c C-k
to compile the entire buffer and load it into the SLIME REPL.
You can also compile specific forms by, moving you cursor to the form you want to compile and C-c C-c
.
This is just the basics, I will cover more SLIME specific features in a future post.
Conclusion
This post is intended to bring you up to speed with a Common Lisp development environment and allow you to write some CL code!
In the next post I will be covering how to set up a Common Lisp project, defining a directory structure, compiling and other basics in setting up a CL Project.