quasiTech

experiments

Starting With Common Lisp

Learning a new programming language requires you to interact with its runtime. But the bare bones runtime is usually just that – bare. It generally is an unpleasant task to interact with directly. This is nowhere more true then when learning to program in Lisp. But the good news is that the tools of the Lisp environment are evolved and provide a lot of support to make interaction less unpleasent. It is therefore imperative that we use these tools and setup an ideal environment before we start with the lessons.

Today we will setup, from scratch, a Common Lisp development environment. This will include an editor, an interactive development environment, a structured editing mode and an implementation of Common Lisp.

Note : please change the paths in the examples to reflect your system

Common Lisp

We will need an implemntation of Common Lisp. There are several free and commercial ones available. We will use the excellent public domain Steel Bank Common Lisp (SBCL) for this tutorial.

Steel Bank Common Lisp (SBCL) is a high performance Common Lisp compiler. It is open source / free software, with a permissive license. In addition to the compiler and runtime system for ANSI Common Lisp, it provides an interactive environment including a debugger, a statistical profiler, a code coverage tool, and many other extensions.

Let us select the Darwin (Mac OS X) AMD64 port from the downloads section and download (< 10 Mb).

1
wget http://prdownloads.sourceforge.net/sbcl/sbcl-1.1.8-x86-64-darwin-binary.tar.bz2

Extract the archive

1
tar xjvf sbcl-1.1.8-x86-64-darwin-binary.tar.bz2

Change to the extracted folder

1
cd sbcl-1.1.8-x86-64-darwin

The INSTALL file details the installation instructions. I prefer to install in my home folder. Create a folder to contain all the SBCL files. Run the install script specifying the installation folder of our choice.

1
2
3
mkdir /Users/me/sbcl

INSTALL_ROOT=/Users/me/sbcl sh install.sh

This will install all the files into /Users/me/sbcl. SBCL contains an executable and a core file. The executable – usually named sbcl – looks for an environment variable SBCL_HOME to locate the core file. We also need to set the locale. I have a shell script in my local bin which does all this.

Create a file ~/bin/sbcl and add following to it :

1
2
3
4
5
6
#!/bin/sh
LC_CTYPE=en_US.UTF-8
export LC_CTYPE
export SBCL_HOME=/Users/me/sbcl/lib/sbcl/

/Users/me/sbcl/bin/sbcl $*

Make this file executable

1
chmod +x ~/bin/sbcl

now typing sbcl into your shell prompt should start up SBCL and show something like below

1
2
3
4
5
6
7
8
This is SBCL 1.1.10, 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.
*

You can type s-expressions at the * prompt and the lisp will evaluate and return you an answer. For example.

1
2
3
4
* (+ 2 3)

5
*

Congrats !! We have an installed and running Common Lisp ! Now let’s move forward.

Quicklisp !

http://www.quicklisp.org/beta/ by Zach Beane

Quicklisp is a library manager for Common Lisp. It works with your existing Common Lisp implementation to download, install, and load any of over 900 libraries with a few simple commands.

Read through the installation instructions from that site. In short we will have to :

1
$ curl -O http://beta.quicklisp.org/quicklisp.lisp

Start SBCL with this file :

1
sbcl --load quicklisp.lisp

At the prompt follow the instructions – type following :

1
* (quicklisp-quickstart:install)

This will download and install the latest quicklisp software. To add the quicklisp to the SBCL startup file :

1
(ql:add-to-init-file)

Now everytime you start SBCL it will load quicklisp automatically. yey!

To install a library you simply have to :

1
* (ql:quickload "libraryname")

Now the we need to install a very important package called SLIME. This is the software which gives us our interactive development environment (like no other) !

1
(ql:quickload "quicklisp-slime-helper")

Note the instructions which follow this – you will need to use them for your editor configuration. Next topic.

Editor

GNU Emacs is an extensible, customizable text editor—and more. At its core is an interpreter for Emacs Lisp, a dialect of the Lisp programming language with extensions to support text editing.

Emacs is the editor of choice for Lisp programmers. It provides fantastic support for programming in various lisps. There are a lot of flavours available for a lot of different systems. On GNU/Linux systems, the latest emacs can be installed using the system package manager. I use Aquamacs on Mac OS X. Please install your Emacs before moving to the next step.

Configuring Emacs

Create a .emacs file in your home. This file is the Emacs startup file and can contain any valid elisp. All options can be configured here. By convention a folder ~/.emacs.d is usually used for any misc elisp files, packages etc. which we may need to install manually.

Emacs comes with a package manager. This can automagically install packages and their dependencies. I like all the installed packages to reside inside a folder .emacs.d/elpa. Create this folder. To set it up, add the following to your .emacs. (note : ; start a comment in lisp source files)

1
2
3
4
5
(require 'package)
(add-to-list 'package-archives
             '("melpa" . "http://melpa.milkbox.net/packages/") t)
(setq package-user-dir "/Users/me/.emacs.d/elpa/") ;; change the path.
(package-initialize)

Now M-x package-install <package-name> will download and install the package.

SLIME

SLIME is a Emacs mode for Common Lisp development.

Configuring SLIME : Add the following to your .emacs

1
2
3
4
5
6
7
8
(setq inferior-lisp-program "~/bin/sbcl")
(load (expand-file-name "~/quicklisp/slime-helper.el"))

(setq slime-lisp-implementations
           '((sbcl ("~/bin/sbcl") :coding-system utf-8-unix)))

(set-language-environment "UTF-8")
(setq slime-net-coding-system 'utf-8-unix)

This basically tells Emacs where to find the lisp (called inferior-lisp) program. Then we load the slime-helper which quicklisp provides.

M-x eval-buffer will read your new changes. M-x slime should start SLIME and present you with a nice REPL. At the REPL ,q will quit SLIME.

PAREDIT

ParEdit (paredit.el) is a minor mode for performing structured editing of S-expression data.

M-x package-install paredit should download and install it.

To setup paredit and to get it to work with SLIME, add following to .emacs.

1
2
3
4
5
6
7
;; Paredit customizations
(autoload 'paredit-mode "paredit"
  "Minor mode for pseudo-structurally editing Lisp code."
  t)
(add-hook 'slime-mode-hook (lambda () (paredit-mode +1)))
(add-hook 'slime-repl-mode-hook (lambda () (paredit-mode +1)))
(require 'paredit)

Almost there

At this point of time we have an installed and running Common Lisp implementation, an advanced editor, an interactive development environment & a structured editing extention for the editor. This is the place to spend some time to see how all of these work.

  • Inside Emacs C-h t starts the inbuilt interactive tutorial
  • Emacs introductory videos http://emacsrocks.com/
  • SLIME video – This is an excellent and instructional watch. Marco Baringer shows how it is done. Consider this also as informal introduction to SLIME
  • SLIME manual
  • Paredit Author’s page
  • Paredit Emacs Wiki page

Epilogue : Learning Common Lisp

Now that we have got all the boring setup stuff out of the way, let us start with Common Lisp !! Practical Common Lisp is an excellent modern introduction and you can start here.

I am assuming that as you have come this far, you are interested in learning. I would suggest that you do exactly so – learn. Give it some time and thought before you come to any conclusions. This is very important for people who are coming from a non-lisp programming background. The lisp way is different. It may take time for you to get it. Concentrate on the simpler language aspects and try to solve challenging programming problems. Stay away from the temptation of exploring libraries and doing some ‘real’ work at this point. You will only get distracted with unnecessary detail.

Best Luck on your journey – You will not regret it.

May the force be with you.

Comments