tags:

views:

1239

answers:

7

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.

+2  A: 

Did you try Lispbox?

John the Statistician
I tried to get started with Practical Common Lisp, using Lispbox, but they didn't have a FreeBSD setup, and I had difficulty trying to get it running on my platform. I hope that when I get more experienced, I could attempt again after finishing "Gentle Intro".
Geoff Fritz
A: 

Well, a good place to start would be the executing lisp section of the info pages.

In general, when you work with an interpreted language in Emacs, you edit the source files, and interact with an inferior process (a new process spawned by Emacs, in this case a lisp process). You can evaluate an sexp (balanced expression), region, or the entire buffer with simple keystrokes. Those commands send the code to the lisp process and evaluate it.

This usage model works for shells, Tcl, lisp, anything with an interpreter...

The wiki has a lot of good information.

Trey Jackson
SLIME is much more complete than other "inferior" interpreters spawned by Emacs. Most everything is integrated into your editing window, so you won't need to refer to the actual interpreter (`*inferior lisp*`) at all.
jrockway
Sweet, -2 for no particular reason. Can I get a -3?
Trey Jackson
+1 Thanks for the helpful references.
Geoff Fritz
Yes, I downmodded this. Just like you can edit your disk with a (very small) magnetized needle, you *could* do things your way. But why?(Also, "interpreted language" is a misnomer. The Common Lisp standard does not specify whether implementations should compile or interpret the input. SBCL compiles your functions to native machine code as you type them in, just like the C compiler does.)
jrockway
the 'interpreter' thing already kills it. Several implementations of Lisp don't have one or don't use it by default - still you can use them interactively, since they compile every bit of code - even small snippets.
Rainer Joswig
sure you can use Emacs and an inferior Lisp, but that hardly make it an 'Common Lisp environment' (see the question) - from a CL environment I would expect a bit more services.
Rainer Joswig
@jrockway Just seems contrary to SO etiquette to downvote other answers simply because they aren't match yours. As seems to be your pattern.
Trey Jackson
I have strong opinions on what is and isn't the right answer. Downvoting allows me to express my opinions with one click. Not sure why this upsets you, do you get money or something if you get above a certain reputation level?
jrockway
You're totally welcome to vote your mind. Just hadn't come across many who downvoted things that were correct at the same time as answering the question themselves. e.g. this one: your answer is obviously the better one. Similarly, on a different question, you downvoted an answer and posted your answer (whose content was essentially in the downvoted answer). Just a different (and new to me) style. I wasn't upset, just curious. Especially since the MO for downvotes is to comment as to why (and at the time you hadn't).
Trey Jackson
A: 

I can say that Emacs is a good environment for writing Lisp, since I used it in college to that end. The *scratch* buffer provides a good place for test code. The compiler docs should say how to call them, so you can write out your files to disk and then run them in a shell window, or use one of Emacs' modes to integrate the edit-compile-and-run. (Sorry I'm not more specific, it's been 15 years.)

Jason Catena
The `*scratch*` buffer is for Emacs Lisp, not Common Lisp. There is a SLIME contrib extension to give you a CL scratch buffer, however.
jrockway
Also, nobody develops Lisp with Emacs and the command-line. Everything can be done inside SLIME.
jrockway
I would not say that 'nobody' develops using a text editor and the command line - I know hackers who are comfortable with it. It just makes no 'Common Lisp environment'.
Rainer Joswig
+13  A: 

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!

jrockway
+1 Very helpful. BTW, I didn't say the REPL was "boring" -- just that I didn't think it was appropriate for editing large amounts of stuff in. Maybe I'm just totally ignorant on how experienced Lisp coders work on real projects in this environment. BTW, after typing in functions and assigning variables into the REPL, is there a way to see what you've got in there? An equivalent to "list" in BASIC. (I know, I know... eeeeew!)
Geoff Fritz
@Geoff Fritz: This is probably getting ahead of your exploration, but if you create your own package (namespace) to work in, and use that in the REPL, then you can programmatically list all the symbols that are defined in that package, see which of them correspond to functions, and do other introspective things like that. (It helps if it's your own package, because the default CL-USER package will have a lot more than your work in it.) I don't tend to do a lot of coding in the REPL directly, I edit in a separate buffer and evaluate (like the SLIME suggestion here, except with Franz's ELI).
khedron
While SBCL is very fine, I wouldn't say that it is "the best".
Svante
What makes the other implementations better? Running slower and implementing less of CLOS?
jrockway
+2  A: 

Lispbox & SLIME are fine answers, but if you want something even simpler, just to play with, you might consider the free version of LispWorks Personal or Allegro CL Free Edition. I have not used either on linux myself, but they both appear to come with GUIs. There will be heap size or other limitations (since the full versions are not free), but they may be easier to dive into.

khedron
Thanks for the suggestion, buy my platform is FreeBSD.
Geoff Fritz
@Geoff Fritz: Both LispWorks Personal and Allegro CL Free say they're available for FreeBSD.http://www.lispworks.com/support/system-requirements.htmlhttp://franz.com/downloads/
khedron
I have watched Pascal Costanza use one of these, and they both basically work like Emacs + SLIME. Except, of course, the editor isn't Emacs. They are good for a quick start, but I don't see any reason to pay for less functionality than the free implementations. (If you are on Windows, then the equation changes, but you said you're on FreeBSD, so...)
jrockway
+2  A: 

There are different ways of setting up a Lisp environment, from manual approaches to out-of-the-box installers.

The following answer is for clbuild, which always downloads brand-new versions of all packages for you:

Assuming you have Emacs and SBCL already installed, the following gets you up and running with SLIME. (You also need bash, darcs, CVS, etc, but FreeBSD ports include all of these, as far as I know.)

$ darcs get --set-scripts-executable http://common-lisp.net/project/clbuild/clbuild
$ ./clbuild/clbuild install slime
$ ./clbuild/clbuild slime

You don't have to (indeed, should not) modify your .emacs for this. If you have played with a manual installation of SLIME before, clean out the old changes from .emacs. clbuild does the slime setup for you.

If you like, clbuild can also fetch and build a recent SBCL for you:

$ ./clbuild/clbuild install sbcl
$ ./clbuild/clbuild compile-implementation sbcl
# afterwards you can uninstall the FreeBSD-provided Lisp used for bootstrapping

As for your usage questions:

  • C-x 5 2 gives you the second window to play with
  • C-c C-k compiles & loads the current buffer
  • There is tab completion built-in, but also other completion features. Personally I love the C-c M-i fuzzy completion.
  • jrockway's answer has additional good tips on emacs commands which I won't repeat here
David Lichteblau