Frequently Asked Questions for comp.lang.lisp

This document aims to provide answers to questions frequently asked on the comp.lang.lisp newsgroup. It is hoped that this will promote a more harmonious atmosphere on the newsgroup, while also enabling newcomers better to judge the appropriateness or otherwise of their putative posts.

This document's source is available from cCLan's CVS repository, in a module named `faq'; it was derived in part from the old comp.lang.lisp FAQ maintained by Mark Kantrowitz and Barry Margolin, to whom the current maintainers extend their thanks. The canonical location for the HTML version of this document is currently http://www-jcsu.jesus.cam.ac.uk/~csr21/lispfaq.html.

Corrections, suggestions, and comments to, for and regarding this document are welcome, either on cCLan mailing lists or as a followup to the regular posting on comp.lang.lisp; while this FAQ is under development, you may also contact the `primary' maintainer directly at csr21@cam.ac.uk.

Last modified: Wed 06 Aug 2003 11:24 BST


This document aims to provide answers to questions frequently asked on the comp.lang.lisp newsgroup. It is hoped that this will promote a more harmonious atmosphere on the newsgroup, while also enabling newcomers better to judge the appropriateness or otherwise of their putative posts.

This document's source is available from cCLan's CVS repository, in a module named `faq'; it was derived in part from the old comp.lang.lisp FAQ maintained by Mark Kantrowitz and Barry Margolin, to whom the current maintainers extend their thanks. The canonical location for the HTML version of this document is currently http://www-jcsu.jesus.cam.ac.uk/~csr21/lispfaq.html.

Corrections, suggestions, and comments to, for and regarding this document are welcome, either on cCLan mailing lists or as a followup to the regular posting on comp.lang.lisp; while this FAQ is under development, you may also contact the `primary' maintainer directly at csr21@cam.ac.uk.

Last modified: Wed 06 Aug 2003 11:24 BST

1. This newsgroup
1.1. What is the purpose of comp.lang.lisp?
1.2. What is on-topic on comp.lang.lisp?
1.3. What is off-topic on comp.lang.lisp?
1.4. Is Scheme a lisp?
1.5. Can you help me with my homework?
2. Resources
2.1. Where can I find free lisp compilers or interpreters?
2.2. Where can I buy a professional lisp system?
2.3. What books should I read to learn more about lisp?
2.4. What online resources are there for lisp users?
3. Terminology
3.1. What do all those abbreviations mean?
3.2. I've heard the terms "Lisp-1" and "Lisp-2" – what do they mean?
4. Programming questions
4.1. How do I write a "Hello, World!" programme in Lisp?"
4.2. How do I make an executable from my programme?
4.3. How do I run my programme as a script, then?
4.4. What is the Lisp equivalent of the __FILE__ and __LINE__ ANSI C preprocessor macros? How do I find out where an error occurred?
4.5. How do I split a string?
4.6. Why doesn't Common Lisp have continuations?
4.7. How does (defun car (x) (car x)) work?
4.8. Does anyone actually use flet?
5. Common programming pitfalls
5.1. The read-eval-print loop is just sitting there after I've typed in my form. What's happening?
5.2. I want an array of foos, but (make-array 10 :initial-element (make-foo)) causes strange bugs. Why?
5.3. Why does (read-from-string "foobar" :start 3) return FOOBAR instead of BAR?
5.4. Why can't I apply #'AND or #'OR?
5.5. Why are my structure contents wrong?
5.6. Why isn't there a DEEP-COPY function in the language?
6. Debunking myths
6.1. When did LISP die?
6.2. Why is LISP slow?
6.3. How do I manipulate symbols with IMPLODE/EXPLODE?
6.4. Why doesn't lisp have structures or arrays?

1. This newsgroup

1.1. What is the purpose of comp.lang.lisp?

The charter at ftp://ftp.uu.net/usenet/control/comp/comp.lang.lisp states that the purpose of comp.lang.lisp is "Discussion about LISP". It is somewhat in the nature of things that newsgroups' purposes evolve, as do names. Firstly, the newsgroup has evolved such that the main topic of discussion is ANSI Common Lisp, though discussion about other lisp variants is welcome within reasonable bounds. Secondly, the spelling "LISP" is passť; while it does reflect the initial abbreviation, it no longer reflects the actual language, so "Lisp" is the preferred capitalization.

1.2. What is on-topic on comp.lang.lisp?

Disscussion of the language defined by the ANSI Common Lisp standard is definitely on-topic on comp.lang.lisp. Unlike comp.lang.c, we do not restrict our discussions to the standard, but also actively discuss the differences between implementations and implementation-specific extensions.

Since the Lisp community is remarkably long-lived, discussion of the history and evolution of Lisp tends also to be welcomed, or at least tolerated; discussion of non-standard lisps (though generally not Scheme or Emacs Lisp) is also accepted. Though CLOS (the Common Lisp Object System) has its own newsgroup it is also part of ANSI CL, and so is a valid topic for discussion.

1.3. What is off-topic on comp.lang.lisp?

Questions about Scheme, Emacs Lisp and AutoLisp tend not to be terribly welcome, as they have their own fora in the comp.lang.scheme, comp.emacs and comp.cad.autocad newsgroups.

1.4. Is Scheme a lisp?

Scheme is a member of the greater family of Lisp languages, assuming that is considered to include others like Dylan and Emacs Lisp. The design of Scheme predates the ANSI Common Lisp standard, and some CL features such as lexical scoping may be considered to have been derived from Scheme.

More detailed comparative discussions don't generally prove very productive; those that are interested in discussing Scheme should first consider discussing it in comp.lang.scheme, where discussion would be much more welcome and appropriate.

1.5. Can you help me with my homework?

But of course!

However, we will need the e-mail address of your lecturer or teaching instructor to best aid you; for it is only fair that if we do the homework we should get the course credits. Alternatively, should you wish to hire a consultant from the group, competitive rates can be arranged.

Post the assignment, and your attempt so far; state explicitly that it is homework. You may find a kind soul on the group to explain some point that you are missing; this is more likely the more visible your own work is. Posts of the form "my assignment is due tomorrow please hlep!" are unlikely to engender much sympathy.

2. Resources

2.1. Where can I find free lisp compilers or interpreters?

There are a number of free (in both the `gratis' and the `libre' senses) lisp environments:

CLISP

A lightweight common lisp interpreter and compiler, which compiles to bytecode, and runs on Windows, AmigaOS, Acorns, OS/2 and most flavours of Unix. CLISP's implementation of CLOS is currently not quite complete. CLISP is licensed under the GNU GPL.

CMUCL

An interpreter and optimizing compiler to native code running on a few flavours of Unix (including x86/FreeBSD, x86/Linux and sparc/Solaris). CMUCL can be difficult to compile; it requires itself to build itself, and bootstrapping is an issue. CMUCL is mostly public domain software, though portions are under an X-like or BSD-like licence.

Corman Lisp

Corman Lisp is a compiler for Windows 95/98/2000/ME/NT, with integration with the Windows API. Corman Lisp is free for personal use.

ECLS

Standing for `Embeddable Common Lisp', ECL is an implementation of Common Lisp running on at least x86, Sparc and PPC architectures, under Linux, FreeBSD, Solaris, MacOSX and Windows (Using the CYGWIN environment). As well as a bytecode compiler, ECL can compile to C. ECL is licensed under the GNU LGPL.

GCL

Gnu Common Lisp has languished in the past as a non-ANSI compliant Common Lisp; recent times have let it see more developments towards supporting the ANSI Common Lisp standard. It cannot (yet) be recommended as a Common Lisp, but it is still useful for running software written for it (primarily Maxima). GCL is licensed under the GNU LGPL.

OpenMCL

OpenMCL is an `opensourced' version of a port of a subset of MCL 4.2, and runs on powerpc/Linux. Not quite all functionality is yet present, but it is improving rapidly. OpenMCL is licensed under the GNU LGPL

Poplog

Poplog is licensed under an XFree86-style license.

SBCL

SBCL is a fork from CMUCL aimed at improving the maintainability by removing the bootstrapping issues involved in recompiling. Currently it runs on x86/Linux and alpha/Linux. SBCL is licensed in a similar fashion to CMUCL.

2.2. Where can I buy a professional lisp system?

There are a fair few commercial vendors of Lisp systems; the following are the most active:

Allegro Common Lisp

Franz Inc's Allegro Common Lisp is a fine lisp development environment. See their website for more details

LCL

LCL (formerly Liquid Common Lisp) is an offering from Xanalys

LispWorks

Another offering from Xanalys, LispWorks has a different set of extensions above the ANSI specification from LCL

MCL

A commercial implementation of Common Lisp for the Macintosh.

Scieneer Pty Ltd

A commercial natively multithreaded implementation of Common Lisp for various Unixes.

Symbolics Common Lisp

2.3. What books should I read to learn more about lisp?

2.4. What online resources are there for lisp users?

The Common Lisp Hyperspec

A non-normative transferral of the official ANSI standard for Common Lisp to the hypertext medium, by Kent Pitman.

Association of Lisp Users

The website of the association of lisp users. Infrequently updated.

CLiki

An on-line Wiki-equivalent with emphasis on Free software written in Common Lisp.

3. Terminology

3.1. What do all those abbreviations mean?

ANSI

American National Standards Institute

CL

Common Lisp

GC

Garbage Collection

You may also wish to visit the Jargon File

3.2. I've heard the terms "Lisp-1" and "Lisp-2" – what do they mean?

4. Programming questions

4.1. How do I write a "Hello, World!" programme in Lisp?"

This is actually a subtle question; not in the respect of computing "Hello, World!", obviously, but because of what being a "Hello, World!" programme actually means.

At its simplest, you can simply type "Hello, World!" at the REPL, to find that the `P' part of that will print "Hello, World!" back to you. However, this won't do what you want if this is in a file, as return values aren't printed unless they are at the REPL.

Something which is closer to the canonical "Hello, World!" attempt is (format t "~&Hello, World!~%").

4.2. How do I make an executable from my programme?

4.3. How do I run my programme as a script, then?

4.4. What is the Lisp equivalent of the __FILE__ and __LINE__ ANSI C preprocessor macros? How do I find out where an error occurred?

There is no direct equivalent of __FILE__ and __LINE__ in ANSI Common Lisp; this is perhaps most simply explained by the fact that CL is not particularly a file-oriented and definitely not a line-oriented language. That said, your particular implementation may carry around some information about where functions were compiled, and COMPILE-FILE binds the special variables *COMPILE-FILE-TRUENAME* and *COMPILE-FILE-PATHNAME*.

(defun foo () (break "Stopped inside ~S" (the-function-i-am-in)))

(setf (symbol-function 'bar) (symbol-function 'foo))

(bar)

Are you in a breakpoint in FOO or in BAR?

4.5. How do I split a string?

There is no `right' answer to this question; many lisp programmers have rolled their own solution in the past, and others are of the view that it should never be necessary, as long as all sequence functions are used with consistent :start and :end arguments.

However, a community-based `standard' was developed in June/July 2001 on comp.lang.lisp; known as SPLIT-SEQUENCE (formerly PARTITION), it works as follows in its simplest form:

(split-sequence #\Space "A stitch in time saves nine.") gives ("A" "stitch" "in" "time" "saves" "nine."), 28

For more details, consult the specification, available from its CLiki page.

4.6. Why doesn't Common Lisp have continuations?

Continuations are a great theoretical tool; if a language has first-class, multiply invocable continuations then one can build threads, exceptions, coroutines, and the kitchen sink on top.

However, there is an implementation burden with continuations; supporting first-class, multiply invocable continuations complicates things tremendously for the Lisp implementor. The ANSI standardizing committee J13, mindful of this, took the view that it would be better to specify the user-level control structure (CATCH, UNWIND-PROTECT, and so on) and let implementors choose whether to build those on top of continuations or not.

If you need to play with continuations, you should use a Scheme implementation.

4.7. How does (defun car (x) (car x)) work?

This code is probably part of the source to a lisp compiler, which knows how to open-code calls to CAR. However, the interpreter also needs to know how to call CAR, which is what the above defun is doing. This is not recommended in user code...

4.8. Does anyone actually use flet?

This question is usually motivated by the existence of labels, which is a similar form for binding functions, but also allows mutual recursion between the functions being bound. Given this, it is perhaps natural to question the utility of flet.

However, there are two reasons for using flet: one idiomatic and one programmatic. The idiomatic reason is that flet can be useful to signal to the reader of the code that it is not expected that the functions will be mutually recursive; in other words, it is part of the documentation of the system, so that a subsequent programmer can see by inspection the purpose of binding the functions.

More usefully, though, flet can be useful to locally modify the behaviour of functions, for instance (a contrived example due to Kent Pitman):

(defun square (x) (* x x))

(flet ((square (x) (make-instance 'square :area (square x)))) ...)

5. Common programming pitfalls

5.1. The read-eval-print loop is just sitting there after I've typed in my form. What's happening?

One possible explanation for this behaviour is that you have typed in a program that causes an infinite loop; for instance (loop).

However, the fact that you are surprised by this behaviour suggests that this isn't the case; in which case a far more likely explanation is that your form is not quite complete. You may have typed a doublequote, vertical bar, "#|" comment beginning, or left parenthesis that you never matched with another doublequote, vertical bar, "|#", or right parenthesis, respectively. Try typing a few right parentheses followed by Return.

5.2. I want an array of foos, but (make-array 10 :initial-element (make-foo)) causes strange bugs. Why?

Well, the array created above contains 10 pointers to the same foo, which will indeed cause strange bugs. The correct way to initialize your array is probably (map-into (make-array 10) #'make-foo)

5.3. Why does (read-from-string "foobar" :start 3) return FOOBAR instead of BAR?

READ-FROM-STRING is one of the rare functions that takes both &OPTIONAL and &KEY arguments:

READ-FROM-STRING string &OPTIONAL eof-error-p eof-value &KEY :start :end :preserve-whitespace

When a function takes both types of arguments, all the optional arguments must be specified explicitly before any of the keyword arguments may be specified. In the example above, :START becomes the value of the optional EOF-ERROR-P parameter and 3 is the value of the optional EOF-VALUE parameter.

To get the desired result, you should use (read-from-string "foobar" t nil :start 3). If you need to understand and use the optional arguments, please refer to CLTL2 under READ-FROM-STRING, otherwise, this will behave as desired for most purposes.

The other functions with this peculiarity in the COMMON-LISP package are PARSE-NAMESTRING, WRITE-LINE and WRITE-STRING.

5.4. Why can't I apply #'AND or #'OR?

Here's the simple, but not necessarily satisfying, answer: AND and OR are macros, not functions; APPLY and FUNCALL can only be used to invoke functions, not macros and special operators.

OK, so what's the real reason? The reason that AND and OR are macros rather than functions is because they implement control structure in addition to computing a boolean value. They evaluate their subforms sequentially from left/top to right/bottom, and stop evaluating subforms as soon as the result can be determined (in the case of AND, as soon as a subform returns NIL; in the case of OR, as soon as one returns non-NIL); this is referred to as "short circuiting" in computer language parlance. APPLY and FUNCALL, however, are ordinary functions; therefore, their arguments are evaluated automatically, before they are called. Thus, were APPLY able to be used with #'AND, the short-circuiting would be defeated.

Perhaps you don't really care about the short-circuiting, and simply want the functional, boolean interpretation. While this may be a reasonable interpretation of trying to apply AND or OR, it doesn't generalize to other macros well, so there's no obvious way to have the Lisp system "do the right thing" when trying to apply macros. The only function associated with a macro is its expander function; this function accepts and returns and form, so it cannot be used to compute the value.

The Common Lisp functions EVERY and SOME can be used to get the functionality you intend when trying to apply #'AND and #'OR. For instance, the erroneous form: (apply #'and *list*) can be translated to the correct form: (every #'identity *list*).

5.5. Why are my structure contents wrong?

Most probably, one of your structure slots is called `p'. The accessor for this slot will clash with the default predicate that defstruct defines for your structure; so if you have (defstruct bar (p 1) (q 2)), you could easily see (make-bar) return #S(bar :p t :q 2).

To avoid this, use the :predicate defstruct option to eliminate or rename the predicate function, or else use a different slot name.

5.6. Why isn't there a DEEP-COPY function in the language?

Copying an arbitrary structure or object needs context to determine what is the correct copy.

For instance, consider a queue data structure, most easily implemented as a pair of pointers, one pointing to the head of the queue and another to the tail

See Kent Pitman's article on the nature of equality.

6. Debunking myths

6.1. When did LISP die?

6.2. Why is LISP slow?

6.3. How do I manipulate symbols with IMPLODE/EXPLODE?

Generally, you don't.

IMPLODE and EXPLODE were functions in old lisps where there was no string data type, so that symbols were the only way of manipulating text. Then (explode 'foo) would give you (F O O); you could then do (implode (cdr (explode 'crash))) to give you back the symbol RASH

If you are taught today about implode/explode in a lisp programming class for anything other than historical context, complain loudly to your lecturer.

6.4. Why doesn't lisp have structures or arrays?

If arrays and structures don't exist, then obviously MAKE-ARRAY and DEFSTRUCT must be figments of the imagination. Similarly, since Lisp only uses association lists to organize "database-like" information, MAKE-HASH-TABLE must also be a figment of the imagination.

Based on the nonexistence of the above figments of the imagination, Perl and Python, with arrays, associative tables, and dictionaries, must obviously be manifestly superior.