Lisp Formatting Advice

Let the computer do the work for you. Find and install an editor which has a Lisp mode and find out how to use this mode. By letting the computer do the drudgework of indenting and counting parentheses, you make your life easier and can focus your attention on more important issues like understanding why and how the code works (or doesn't). A popular choice is Emacs which, in addition to being able to format Lisp, is itself written in large part in a Lisp dialect, Elisp. This offers the great advantage that you can customize and extend the program by writing Lisp code to add functionality. (Note, however, that Elisp is a somewhat old-school style of Lisp with a few important differences from more modern dialects such as Common Lisp and Scheme.)

Lisp programmers generally prefer code formatted in this manner:

(defun hello-world (arg1 arg2 arg3 arg4 arg5
                    arg6 arg7)
  ;; Most editors indent two spaces by default
  (print "Hello World!")
  (if arg1
      (some-function-that-takes-lots-of-args arg1 arg2 arg3 arg4
                                             arg5 arg6 arg7)
      (some-other-function "Bob the Wonder Llama")))
While you will see some variation in style between different Lisp programmers' code, there is remarkable unanimity on the basics--the Lisp world does not suffer from "brace wars" the way curly brace languages do. (LOOP formatting, however, does tend to be a bit more freeform, as there are too few parentheses to make the indentation obvious and automatic. Some consider this one of LOOP's biggest flaws.)

This page is linked from:

Frequently Asked Questions