Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Nikodemus' Common Lisp FAQ

31 views
Skip to first unread message

niko...@random-state.net

unread,
Dec 5, 2006, 5:26:24 AM12/5/06
to
I finally got fed up with the state of all the FAQ efforts, so here's
mine. Feel free not to like it -- though I am open to actual
constructive critisism. I'll be posting this here monthly from now,
unless a better FAQ appears.

Cheers,

-- Nikodemus Siivola

* Nikodemus' Common Lisp FAQ

2006-12-05

This is a highly uncanonical Common Lisp FAQ with a strong editorial
bias, by Nikodemus Siivola. Partially based on various comp.lang.lisp
and general Lisp FAQs.

If you think I've stolen your words I'm all to happy to 'fess up, so
feel free to drop a line to <niko...@random-state.net>.

Current version of this FAQ is available from:

http://random-state.net/files/nikodemus-cl-faq.txt

This FAQ is posted to comp.lang.lisp once a month.

** Just starting out

*** How can I learn Common Lisp?

(1) Read a good book on Common Lisp.

(2) Start using it.

Many people seem to manage the first stage over and over again, but
fail to act on the second one. Seriously: you cannot learn any
programming language without using it, nor can you really understand
one without working on a moderately large program in it.

*** Which book should I read first?

Read Peter Seibel's `Practical Common Lisp', a.k.a. PCL. It is a good
starting place if you already know how to program in some other
language. It's available both online and in print:

http://www.gigamonkeys.com/book/

Another alternative is David Touretzky's `Common Lisp: A Gentle
Introduction to Symbolic Computation', which is a good choice if you
are new to programming or find PCL too hard. Even if you start with
this, read PCL afterwards. If you've already read PCL and understood
it, you can safely skip this. Available both online and in print:

http://www.cs.cmu.edu/~dst/LispBook/index.html

There are plenty of other books too, but these two are the best bets
for starters. However, you should familiarize yourself with the
`Hyperspec', aka CLHS -- the online version of the language standard,
which is an invaluable reference:

http://www.lispworks.com/documentation/HyperSpec/index.html

Don't try to read it right now. Just remember it is there, and contains
the authoritative answer to all possible questions about CL: it is the
perfect place to check if you find yourself in doubt about what a
given operator does.

Also, do not neglect the documentation that comes with your
implementation.

*** Which implementation should I pick?

Depends on your needs, but as long as you are just starting out
it really doesn't matter. However, if you are expecting a particular
group of people to help you, do pick an implementation they are
familiar with.

Once you have learned more you can make an informed decision for
yourself. Moving between implementations is not that big an issue,
really, so there is no point in agonizing over initial choice.

That said, I am is highly partial to SBCL:

http://www.sbcl.org/

SBCL is open source, runs on a number of platforms (including
experimental Windows support), has a pretty good native compiler,
takes ANSI compliance pretty seriously, and generally bring happiness
and joy to the world. ...but then I would say that, since I am one of
its developers and my company Steel Bank Studio Ltd provides
commercial support for it.

*** Which IDE should I use?

If you are using SBCL, then you want to use Emacs and Slime:

http://www.common-lisp.net/project/slime/

Even if you haven't used Emacs before, do yourself a favor and use
Slime -- the learning curve isn't all that long, and you can get at
the commands from menus too.

If you are using another implementation, use the IDE the implementors
recommend for it (though Slime works with practically every
implementation).

If you really really hate Emacs, then there is an Eclipse plugin
called `Cusp', but I won't vouch for it:

http://www.paragent.com/lisp/cusp/cusp.htm

If you really really love Vi, then you are out of luck. This picture
explains why:

http://www.xach.com/img/lisp-and-vim.png

That said, some people do seem to be happy using Lisp just fine from
Vi, but how they do that is quite beyond me. Look elsewhere for help
on that.

For a reasonable Lisp experience your editing environment should at
least: autoindent lisp correctly; match parens; provide commands like
"edit definition", "compile defun", "eval expression", "describe
symbol", "disassemble function", "trace function", "inspect
expression", etc. for interacting with you Lisp; provide integration
with the Lisp debugger. Slime does all this and more.

*** What online forums are there?

Comp.lang.lisp on Usenet / Google Groups is probably the largest, but
is also inhabited by several trolls. While many of the frequent
writers are quite knowledgeable, uninformed speculation remains
common. It remains, however, at the time of the writing the only
non-interactive general purpose forum -- but reading it can be a
stress-inducing experience, and is definitely not necessary in order
to use Lisp.

http://groups.google.com/group/comp.lang.lisp

There are several special-purpose mailing lists which have vastly
better signal to noise ratio: all implementations tend to have their
own user- or help-lists, and most libraries have their own mailing
lists.

One list worth a special mention is `lispweb', a mailing list focusing
on web development in lisp:

http://www.red-bean.com/lispweb/
http://news.gmane.org/gmane.lisp.web

The IRC channel #lisp on freenode.org is quite popular also,
especially among the open source users and developers.

*** Is there a CPAN equivalent?

Yes and no. `Cliki' and `common-lisp.net' are what we have.

http://www.cliki.net/
http://www.common-lisp.net/

Most libraries on CLiki are install-able using ASDF-INSTALL, which also
takes care of dependencies.

*** Common Lisp? CL? Lisp?

`Common Lisp' is the name of the ANSI standardized language this FAQ
is about.

`CL' is the preferred abbreviation of that name. `Clisp' is not an
acceptable abbreviation, as it is the name of a specific Common Lisp
implementation.

`Lisp' is a category of languages to which CL belongs. Is hasn't been
written as `LISP' for the last 20 years or so. Common Lisp often
referred to as just Lisp when the context is clear.

** Language features

*** How do I compile a file?

Short answer: start your Lisp, and type:

(compile-file "/path/to/myfile.lisp")

Then you probably want to LOAD the compiled file.

Long answer: most compiled languages are non-interactive -- you
compile a file from the command line or IDE, and then run the compiled
file. Not so with Lisp.

While you can generally speaking turn your project into an executable
file, the typical Common Lisp development session does not resemble
the edit, compile, run cycle you've come to expect.

Normally you interact with a long-running Lisp process that holds your
development session, and add code to it incrementally. For example:

1. Open Emacs, start Slime and Lisp with M-x slime.

2. Load existing code to Lisp using eg. ASDF.

3. Open the file you're interested in, edit a function, and then
hit C-c C-c to recompile that single function.

4. Go to the Slime REPL and test your changes.

5. Go to step 3.

The ASDF alluded above is "Another System Definition Facility", which
can be used to describe the way several files form a single system,
and allows the files to be eg. compiled and loaded with a single
command. Sort of like Make, but not at all like it really.

*** How do I make an executable?

The answer depends on the implementation you are using. Refer to its
documentation. That said, if you are using SBCL:

;; First load you application into SBCL, then do this. MY-FUNCTION
;; is the entry point to your application.
(save-lisp-and-die "my.exe" :executable t :toplevel 'my-function)

*** FUNCALL and APPLY -- what's the difference, which one should I use?

Short answer: Use FUNCALL if you can, APPLY otherwise.

Long answer: With FUNCALL the number of arguments must be known at the
call site. With APPLY (and MULTIPLE-VALUE-CALL) the number of
arguments need not be known.

(defun map-list-with-1 (function list arg)
(mapcar (lambda (elt)
(funcall function elt arg))
list))

(defun map-list-with-n (function list &rest args)
(mapcar (lambda (elt)
(apply function elt args))
list))

There is no reason to write MAP-LIST-WITH-1 using APPLY, and FUNCALL
is almost certain to be more efficient here.

In contrast, MAP-LIST-WITH-N cannot be written using FUNCALL, as the
number of arguments is not known at the call site -- APPLY has to be
used.

*** SET, SETQ and SETF -- what's the difference, which one should I
use?

Short answer: Always use SETF.

Long answer: Originally, in the days of yore, long before there was
Common Lisp, there were no lexical variables -- only dynamic ones. And
there was no SETQ or SETF, just the SET function.

What is now written as:

(setf (symbol-value '*foo*) 42)

was written as:

(set (quote *foo*) 42)

which was eventually abbreviavated to SETQ (SET Quoted):

(setq *foo* 42)

Then lexical variables happened, and SETQ came to be used for
assignment to them too -- so it was no longer a simple wrapper around
SET.

Later, someone invented SETF (SET Field) as a generic way of assigning
values to data structures, to mirror the l-values of other languages:

x.car := 42;

would be written as

(setf (car x) 42)

For symmetry and generality, SETF also provided the functionality of
SETQ. At this point it would have been correct to say that SETQ was a
low-level primitive, and SETF a high-level operation.

Then symbol macros happened. So that symbol macros could work
transparently, it was realized that SETQ would have to act like SETF
if the "variable" being assigned to was really a symbol macro:

(defvar *hidden* (cons 42 42))
(define-symbol-macro foo (car *hidden*))

foo => 42

(setq foo 13)

foo => 13

*hidden* => (13 . 42)

So we arrive in the present day: SET and SETQ are athropied remains of
older dialects, and will probably be booted from eventual successors
of Common Lisp.

Just Use SETF.

EOF

Sathyaish

unread,
Dec 5, 2006, 6:44:41 AM12/5/06
to
That was very helpful FAQ. Thanks, Nikodemus.

Pedro Kröger

unread,
Dec 5, 2006, 7:17:07 AM12/5/06
to
niko...@random-state.net writes:

> *** Common Lisp? CL? Lisp?

I'd also put Clisp here, because people seem to call "clisp" meaning
"common lisp", and, of course, that's a source for confusion.

Pedro Kroger

Ken Tilton

unread,
Dec 5, 2006, 9:06:46 AM12/5/06
to

niko...@random-state.net wrote:
> I finally got fed up with the state of all the FAQ efforts, so here's
> mine. Feel free not to like it -- though I am open to actual
> constructive critisism. I'll be posting this here monthly from now,
> unless a better FAQ appears.
>
> Cheers,
>
> -- Nikodemus Siivola
>
> * Nikodemus' Common Lisp FAQ
>
> 2006-12-05
>
> This is a highly uncanonical Common Lisp FAQ with a strong editorial
> bias, by Nikodemus Siivola. Partially based on various comp.lang.lisp
> and general Lisp FAQs.
>
> If you think I've stolen your words I'm all to happy to 'fess up, so
> feel free to drop a line to <niko...@random-state.net>.
>
> Current version of this FAQ is available from:
>
> http://random-state.net/files/nikodemus-cl-faq.txt
>
> This FAQ is posted to comp.lang.lisp once a month.
>
> ** Just starting out
>
> *** How can I learn Common Lisp?
>
> (1) Read a good book on Common Lisp.
>
> (2) Start using it.

I think you accidentally transposed those two.

> its developers...

uh-oh. :)

>... and my company Steel Bank Studio Ltd provides


> commercial support for it.
>
> *** Which IDE should I use?
>
> If you are using SBCL, then you want to use Emacs and Slime:
>
> http://www.common-lisp.net/project/slime/
>
> Even if you haven't used Emacs before, do yourself a favor and use
> Slime -- the learning curve isn't all that long, and you can get at
> the commands from menus too.
>
> If you are using another implementation, use the IDE the implementors
> recommend for it (though Slime works with practically every
> implementation).
>
> If you really really hate Emacs, then there is an Eclipse plugin
> called `Cusp', but I won't vouch for it:
>
> http://www.paragent.com/lisp/cusp/cusp.htm
>
> If you really really love Vi, then you are out of luck. This picture
> explains why:
>
> http://www.xach.com/img/lisp-and-vim.png
>
> That said, some people do seem to be happy using Lisp just fine from
> Vi, but how they do that is quite beyond me.

You need to search the c.l.l archives for my fortune cookie on that: "It
is easier for programmers to learn a new language than to learn a new
editor." or something along those lines.

Otherwise, nice effort. Don't you want to mention not modifying '(a b c)?

kt

--
Algebra: http://www.tilton-technology.com/LispNycAlgebra1.htm

"Well, I've wrestled with reality for thirty-five
years, Doctor, and I'm happy to state I finally
won out over it." -- Elwood P. Dowd

"I'll say I'm losing my grip, and it feels terrific."
-- Smiling husband to scowling wife, New Yorker cartoon

dus...@gmail.com

unread,
Dec 5, 2006, 9:50:35 AM12/5/06
to

I feel it would be a good idea to add LispWorks as a viable
implementation for Windows users that may not be as comfortable diving
right into a *nix system and a new language at the same time. LW seems
like a reasonably friendly setup for beginnings, and even has an ide
built-in.

0 new messages