Lisp Indentation & Spacing rules
Formatting Guidelines for Assigned Problems
Submitted .lsp files are expected to comply with the following indentation and spacing rules when they are viewed on mars using the less viewer.
Rule 1: Multi-line List Indentation
When a list is written on more than one line, the first character of the 2nd and any subsequent lines in that list should be FURTHER TO THE RIGHT than the list’s opening parenthesis.
BAD:
(defun f (x) and '(the big black cat slept on the dark
(* x 2)) red mat)
CORRECT:
(defun f (x) and '(the big black cat slept on the dark
(* x 2)) red mat)
Rule 2: No Space After Opening or Before Closing Parentheses
Do NOT put a space immediately after an opening parenthesis or immediately before a closing parenthesis.
BAD: ( this is bad )
GOOD: (this is right)
Rule 3: Space Between Consecutive Elements
When two consecutive elements of a list are on the same line, they should be separated by a space.
BAD: ((this is)(a bad)example)
GOOD: ((this is) (a good) example)
Rule 4: No Closing Parentheses on Their Own Lines
Do NOT put closing parentheses on their own lines.
REASON: Putting closing parentheses on separate lines wastes screen space for no good reason and increases the amount of scrolling that is needed to read your code!
BAD:
(defun f (x y z)
(+ x (* y z))
)
GOOD:
(defun f (x y z)
(+ x (* y z)))
Rule 5: Blank Line After Function Definitions
There should be at least one BLANK LINE after each function definition.
Rule 6: Argument Alignment in Function Calls
For each call of a function or call of AND or call of OR that is not written on a single line, make all the arguments of the call line up.
Option 1:
(myfunction argument1
argument2
argument3)
Option 2:
(myfunction
argument1
argument2
argument3)
In the second case, the arguments should line up with one of the first few letters of the function name.
Example:
(myfunction
(func x y)
(+ 2 w)
(anotherfunc w (+ y 1)))
Alternative formatting:
(myfunction (func x y)
(+ 2 w)
(anotherfunc w
(+ y 1)))
Rule 7: Opening Parentheses Alignment in IF Forms
For each IF form that is not written on a single line, make the opening parentheses of the three arguments line up.
(if (evenp n)
(func
w
(+ y 1))
(- n 3))
It is also OK to put the first two arguments on the same line as the symbol IF and put the third argument on the next line. In that case the third argument should line up with the first argument:
(if (symbolp x) (list 'yes x 'is 'a 'symbol)
(list 'no x 'is 'not 'a 'symbol))
Rule 8: Opening Parentheses Alignment in COND Forms
For each COND form, make the opening parentheses of the clauses line up.
Option 1:
(cond ((eql e 0) '(integer-zero 0))
((numberp e) (list 'num-but-not-0 e))
((consp e) (list 'nonempty-list e))
(t (list 'non-numeric-atom e)))
Option 2:
(cond ((eql e 0)
'(integer-zero 0))
((numberp e)
(list 'num-but-not-0 e))
((consp e)
(list 'nonempty-list e))
(t (list 'non-numeric-atom e)))
Option 3:
(cond
((eql e 0)
'(integer-zero 0))
((numberp e)
(list 'num-but-not-0 e))
((consp e)
(list 'nonempty-list e))
(t (list 'non-numeric-atom e)))
Rule 9: Variable Initialization Alignment in LET Forms
For each LET or LET* form, put the first local variable initialization form on the same line as the symbol LET or LET*. If more than one local variable is initialized, make all the initialization forms line up. Indent the body of the LET or LET* form by two spaces.
(let* ((diff (- new old))
(proportion (/ diff old))
(percentage (* proportion 100.0)))
(list 'widgets 'changed 'by percentage 'percent))
TAB Character Verification
Be sure to read this paragraph and do what it says even if you haven’t deliberately used TABs: It’s easy to type a TAB accidentally!
USE THE less COMMAND ON mars TO VIEW YOUR FILE, AND CHECK THAT YOU HAVE FOLLOWED RULES 1 - 9.
If your file contains TAB characters, its indentation and spacing may be wrong when it is displayed by less because less assumes a tab-width of 8 whereas the editor you used to create the file may assume a different tab-width! (The tab-width is the number of columns between tab stops; so if the tab-width is 8 then the tab stops are after the 8th, 16th, 24th, … columns.)
If you see bad indentation or bad spacing when less displays the file but not when your editor displays the file, use the -x option to tell less what tab-width to use—e.g., the command less -x4 myfile.lsp tells less to display myfile.lsp with a tab-width of 4.
Also ADD A COMMENT OF THE FOLLOWING FORM (with the right tab-width after “-x”) AT THE BEGINNING OF YOUR FILE:
;;;; TABS ARE DISPLAYED CORRECTLY WHEN THIS FILE IS VIEWED USING: less -x4 myfile.lsp