Getting started in Common Lisp on Ubuntu.

Written by:GrayMagiker
Published by:Nightscript
Published on:2008-12-14 20:56:13
Topic:Common Lisp

This article will explain the necessary steps to install a common
lisp on your Ubuntu machine. By the end of this article you should be able to write some simple Common Lisp programs. It uses
[url=http://clisp.cons.org/]GNU CLISP[/url] and is mainly geared
toward Ubuntu, though should work on Debian.

Install Ubuntu
The first step is to install Ubuntu (or other Debian based Linux).
For this example I am using Ubuntu 8.04LTS. The steps are fairly
standard, but I have not tested them on any other system. For help on
installing Ubuntu please check out the

Documentation


Install CLISP from apt
Next, you need to install a lisp. There are various versions of
common lisp, and each has it's strengths. I have chosen to use
GNU CLISP, however
SBCL is also available in apt.

Get CLISP in the normal way from apt with the command:
sudo apt-get install clisp

Enter your password, and confirm the install.

Test that clisp is working
Now you should have clisp installed. Test it by trying to get to the
clisp prompt.

Enter the following command at the terminal:
clisp


You should see something like this

test@ubuntu:~$ clisp
i i i i i i i ooooo o ooooooo ooooo ooooo
I I I I I I I 8 8 8 8 8 o 8 8
I \ `+' / I 8 8 8 8 8 8
\ `-+-' / 8 8 8 ooooo 8oooo
`-__|__-' 8 8 8 8 8
| 8 o 8 8 o 8 8
------+------ ooooo 8oooooo ooo8ooo ooooo 8

Welcome to GNU CLISP 2.42 (2007-10-16) <http://clisp.cons.org/>

Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
Copyright (c) Bruno Haible, Sam Steingold 1999-2000
Copyright (c) Sam Steingold, Bruno Haible 2001-2007

Type :h and hit Enter for context help.

[1]>


You are now at the lisp REPL (Read Eval Print Loop).

To return to the command line, type
(quit)


Congratulations, you have just installed a common lisp on your Ubuntu
machine. While you can now follow along with some of the articles on
OSIX like this one http://osix.net/modules/article/?id=673
you don't have the advantage of syntax highlighting, or anything fancy
like that. But don't worry, keep reading and I'll show you how to get
syntax highlighting, and a real interactive environment.

Install Emacs and SLIME

Now we need to install Emacs. Emacs is written in it's own version of
lisp (called elisp), and has a lot of built in support for editing
lisp code in files. Even some die hard vi fans edit lisp code in
Emacs. We are also going to install an optional component for Emacs,
called slime, which provides even more support for interacting with
the lisp process.

Both are available via apt, so...
sudo apt-get install emacs slime


Configure Emacs
Now before you fire up Emacs, we have to configure some settings so
that Emacs knows where to find our lisp install. Actually even if you
did start Emacs, you could change these things while it was running
but that is a bit more complicated then we want to get right at the
moment.

First we need to make a directory for slime to use, and for us to put
any extensions that we install for it latter.

mkdir ~/.slime


Next we need to tell Emacs where that directory is, and how to start
clisp. Put the following code into a file named `.emacs' located in
your home directory:

;;; Lisp (SLIME) interaction
(setq inferior-lisp-program "clisp")
(add-to-list 'load-path "~/.slime")
(require 'slime)
(slime-setup)


The first line of this code is a comment, explaining what the other
lines do. (good practice).

The second line of the above code tells Emacs the command to use to
start a lisp session. This isn't necessary to run e-macs, but it is
if we want to get a interactive session going. You could change
"clisp" to "sbcl" if you wanted to use SBCL. You could even switch
back and forth to test your code in each one.

The third line tells Emacs where to find the slime directory that we
created for it. I don't know what this directory is for, but it is
recommended by the slime manual, so I'd put it in.

The next two lines tell Emacs to make slime ready to run every time it
starts up. Slime will not start every time you start Emacs, but it
will be ready at a moment's notice to start.

Add some simple customizations to Emacs
Adding the following lines to your `.emacs' file is not necessary,
but I find them to be useful.

The first one tells Emacs to enable syntax highlighting whenever it
can.

The second one tells Emacs to highlight the opening and closing
parenthesis when your cursor is over the other one. This is useful
for lisp as parenthesis are very common.

The third and forth make it so every time you hit return while editing lisp code
the next line is automatically indented correctly.
(global-font-lock-mode t)
(show-paren-mode 1)
(add-hook 'lisp-mode-hook '(lambda ()
      (local-set-key (kbd "RET") 'newline-and-indent)))


You can add any of these lines, or none of them, or only some of
them. The only exception is lines 3 and 4 must go together.



REPL, v2.0

Now all that remains is to start up Emacs, and get to writing lisp
code. For some reason Emacs does not place an entry in the
application menu when you install it. You can start it by typing
`emacs &' at the terminal. the `&' after emacs makes it run in the
background so you can enter other commands at the terminal. You can
make an launcher for Emacs on the your Gnome-panel in the standard
way. See
https://help.ubuntu.com/7.04/user-guide/C/launchers.html
for details.

Once you start Emacs you will see the welcome screen. From here it
may not be obvious what to do to start writing some lisp code, so I
will walk you through how I like to use Emacs to write lisp code.

First, some conventions: In Emacs, a lot of things are done via "key
chords" which is a fancy way of saying shortcut keys. If a line
connects two keys, they are to be pressed at the same time. Otherwise
they are to be pressed one after the other. `C-' means the control
key, and `M-' means the 'meta' key, which is usually the alt key.

eg "C-x C-f" means to press the control key and the 'x' key at the
same time. Then release them. Then press the control key and the 'f'
key at the same time. For commands like this, you can leave the
control key pressed if you'd like.

To start slime, and get to a REPL like the one we saw at he beginning
of this article, press M-x and then type the word 'slime' without
quotes. Notice that this appears at the bottom of the screen. Now
press enter.

At this point, a lot of stuff will happen and text will fly across the
screen. Don't worry, this is just Emacs setting up clisp to talk to
it.

After it is all done, you should see something like
This


This REPL is exactly like the one we had at the beginning of this
article, and you can do all the same things with it. You might ask
yourself why, then, did we go through all that trouble? Well, this
REPL is more helpful than the other one. For one, it automatically
indents your code when you press the enter key. For another, when you
are typing a function, down at the bottom of the screen it will show
you what arguments that function takes. That is kind of useful, but
there is another advantage to using slime as well.

Saving code in files

Up until now, I have not told you how to edit source code files with
lisp code in them. This is because lisp is very much about
interactive programing, and the REPL is the main way that this is
accomplished. However, interactive programing does not mean you can't
write your code in a separate file.

In Emacs, to open a file press C-x C-f. Then type the name of the
file. Alternatively you can use the open or new buttons on the tool
bar or from the file menu.

If you want Emacs to automatically do lisp highlighting, then name the
file with a .lisp extension. Or name it anything you want, and do the
following M-x and type `slime-mode' M-x and type `lisp-mode'.

If you start slime before you open the file, slime is still running.
Click on the buffers menu at the top of the Emacs window and go to
'*slime-repl clisp*' to get to it. Now to split the window to see
both at the same time, press C-x 2 to split the window horizontally or
C-x 3 to split the window vertically.

You can send code from the file you are editing to the REPL with lisp
shortcut keys. This is the true advantage of using an editing
environment like Emacs. The following short cut keys will get you
started.


C-c C-k slime-compile-and-load-file
Compile and load the current buffer’s source file.
C-c M-k slime-compile-file
Compile (but don’t load) the current buffer’s source file.
C-c C-c slime-compile-defun
Compile the top-level form at point.



The inevitable error
When you are running a program, you will almost certainly have an
error. The way in which the REPL loop deals with errors is different
from most compilers and interpreters. It allows you to inspect the
running program at the point of an error, basically it has a built in
debugger.

How to effectively use the debugger is a topic for another article.
However, if you run something at the REPL and get thrown to the
debugger read the first message, and it will give some clue as to what
went wrong. Then, to return to the REPL type `q' and the debugger
will exit.

Hack the good hack
So now you have a full lisp editing environment set up, and are ready
to start hacking lisp code.

So where to from here? There are lots of resources on the web, bellow
are a few starting with other articles right here on OSIX



This is an article from http://www.osix.net - view the original at: http://www.osix.net/modules/article/?id=912