Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up a working Common Lisp environment for the aspiring Lisp newbie

Tags:

emacs

lisp

I've been a UNIX sysadmin for a long time, and aside from automating tasks with shell scripting, some light PHP work, and a few simple C programs, I've never done much in the way of programming. I recently decided to stretch my mind a bit and learn Common Lisp.

I'm half-way through Touretzky's "Gentle Intro" and, having just reached the chapter on I/O, I'm wanting to get beyond typing stuff into the REPL as I walk through the book and the exercises. The problem is that I can't find a decent howto/tutorial on getting a decent, working environment.

I've installed emacs (which in itself is a tough change, as I've used vi for almost 20 years), SLIME, and several CL implementations. However, the documentation for any one of these pieces is enormous, nevermind trying to wade through all three. I have skimmed several free online CL books and all of them are agnostic to the environment, assuming that you'll be typing stuff into the REPL or loading source files.

It would really make this experience much more enjoyable if I could find a decent intro to setting up an Emacs environment for Common Lisp that allows me to edit in Lisp mode in one window (function and variable auto-completion would be really nice here), see the results of evaluating single expressions (both output and return value) in another, as well as be able to tell the whole buffer to be evaluated and "run" while I see the results somewhere.

Any and all pointers would be greatly appreciated.

EDIT: My platform is FreeBSD/amd64, and I prefer open source solutions.

like image 476
Geoff Fritz Avatar asked Jul 09 '09 02:07

Geoff Fritz


People also ask

How do you set up a Lisp?

Steps for installation on Windows:Step 1: Download LISP. Step 2: After downloading the compressed zip file on your windows machine now extract it. Step 3: Now open the extracted folder. Step 4: Open clisp.exe to run clisp commands.

What is a Lisp environment?

An environment in Common Lisp is a Lisp object that contains, in some fashion, a set of bindings and also information about operators, variables, symbols, and so on. This information can be useful in various ways, particularly during evaluation, compilation and macro expansion.

What IDE do you use for a Lisp?

SLIME is the most widely-used Common Lisp IDE. Portacle is a portable and multiplatform development environment. It includes Emacs with Slime, SBCL, Quicklisp and Git.

How do you say hello world in Lisp?

Unfortunately, Lisp has many flavors which means the following implementation of Hello World will likely only be applicable to handful of those flavors: (format t "Hello, World!") (format t "Hello, World!")


1 Answers

Let's assume you have emacs running and have checked out SLIME from CVS and installed it. This should be easy with any Linux distro; apt-get install emacs slime does it for me. (You should also install SBCL, the best Common Lisp implementation.)

SLIME out of the box doesn't do much anymore, so it needs configuration. Configuring emacs is done in your ~/.emacs file. Visit that file (C-x C-f ~/.emacs). I have something like this, which loads a variety of goodies:

(eval-after-load "slime"
  '(progn
     (setq slime-lisp-implementations
           '((sbcl ("/usr/bin/sbcl"))
             (ecl ("/usr/bin/ecl"))
             (clisp ("/usr/bin/clisp"))))
     (slime-setup '(
                    slime-asdf
                    slime-autodoc
                    slime-editing-commands
                    slime-fancy-inspector
                    slime-fontifying-fu
                    slime-fuzzy
                    slime-indentation
                    slime-mdot-fu
                    slime-package-fu
                    slime-references
                    slime-repl
                    slime-sbcl-exts
                    slime-scratch
                    slime-xref-browser
                    ))
     (slime-autodoc-mode)
     (setq slime-complete-symbol*-fancy t)
     (setq slime-complete-symbol-function
  'slime-fuzzy-complete-symbol)))

(require 'slime)

If you don't have SLIME installed in your "site directory", you'll also want to add this before those lines:

(add-to-list 'load-path "~/elisp/slime/")
(add-to-list 'load-path "~/elisp/slime/contrib")

(Change the these paths to the correct location, of course.)

The key part is the slime-setup, this loads a variety of optional modules, including the REPL. (I also consider the fancy indentation, and autodoc essential.)

Anyway, you can load this into your running emacs with M-x eval-buffer, or by putting the point inside each sexp and pressing C-M-x. (You can also point to the end of the sexp and press C-x C-e. There are a variety of ways to evaluate Emacs Lisp in Emacs, but that is not really relevant to learning CL.)

At this point, you should be able to type M-x slime and be taken to your REPL. Before that happens, your lisp will be loaded, SLIME will tell it to compile Swank (the CL side of SLIME), and you will see that happen. This only happens once, though, next time SLIME will start up faster.

At the REPL, you can type Common Lisp commands and see the result of their evaluation. Pressing TAB to complete something will bring up a window of possible completions; this is smart completion, so typing something like "d-b" will bring up a list containing "destructuring-bind". Very helpful. M-/ will also complete symbols that you have already typed somewhere inside Emacs; another great time saver. (This works everywhere in Emacs, BTW, and is called "dynamic abbreviation expansion".)

Earlier you said the REPL was boring, so let's open a real file. Press C-x C-f test.lisp. Not surprisingly, this will open up a buffer editing test.lisp, which will be created when you first save the file (C-x C-s). This Lisp file is aware of SLIME, so anything you type can easily be validated by the running Lisp. Tab completion, class inspection, interactive testing, etc. are all available.

As an example, type something like (+ 1 2) and press C-x C-e. You will see the result, as evaluated by the running Lisp, at the bottom of the window. This lets you test individual expressions as you type them into the file.

The next step is to write a full function, perhaps:

(defun my-1+ (x)
    (+ x 1))

You can load this function into the running Lisp in a variety of ways; I usually use C-M-x. You can now change back to the REPL buffer (C-x b *slime-repl sbcl* or similar) and type (my-1+ 42) and see the result 43 printed.

Another way to bring the functions into the running Lisp is to compile and load the whole file with C-c C-k.

Anyway, that's the basics of using SLIME and Emacs. There are obviously many more features available, but this should give you a taste of Lisp programming with SLIME. One last thing I'll mention, Emacs is largely self-documenting, so if you want to learn more, Emacs can help you. While in your Lisp buffer, you can type C-h m to get a full list of commands you can use while editing Lisp. You can click the links to get more information about each function.

Have fun!

like image 152
jrockway Avatar answered Sep 21 '22 14:09

jrockway