Scheme Indentation
The cardinal rules of indentation:
- A line should be indented past the position of the parenthesis that
starts the list to which the beginning of the line is an element.
- Ok:
(define f (define f (define f
(lambda () 3)) (lambda () 3)) (lambda () 3))
- Wrong:
(define f (define f
(lambda () 3)) (lambda () 3))
- This rule must not be violated under any circumstances. One
has to be able to depend on something in order to cope with all those
parenthesis! Points will be deducted if this rule is broken, as they would
be for a grammar mistake in prose writing.
- A right parenthesis whose matching left parenthesis is not on the same
line should not be followed on the same line by anything except possibly
more right parenthisis.
- Ok:
(((x
y))
z)
- Wrong:
(((x
y)) z)
- If a line begins at the same indentation level as
the previous line, it should be indented the same amount.
- The level of each element of a list is one more
than the level of the list.
- Ok:
(list (list
b b
c) c)
- Wrong:
(list (list
b b
c) c)
- Violation of this rule is sloppy programming; the equivalent
of needlessly changing tense in prose.
- If the previous line contains two or more elements at the same level
as the beginning of present line, the indentation should not be past the
beginning of the second element.
- Ok:
(if (zero? x) (if (zero? x)
y) y)
- Wrong:
(if (zero? x)
y)
- Ignoring this rule is also sloppy, but does not look as bad as
violating the previous rule.
Room for variation
As the ok examples show, these rules still allow a lot of freedom. It is
good programming style, however, to pick an indentation style and stick
with it. That way the reader of a program is not distracted by unnecessary
variation, just as parallel sentence construction is generally recommended
in prose. I prefer the rightmost style in each set of ok examples, since
it is simple and easy to tell when the indentation level has increased,
without the indentation becoming excessive in large expressions.
There are no absolute rules for how much to put on a line. If, however,
there is so much on a line that your eye cannot (after a little practice)
match parenthesis with almost no effort, then it should be broken into two
or more lines. It is sometimes ok to start a list in horizontal mode
(multiple elements on the same line) and then switch to vertical mode, but
once you have switched to vertical mode in a long list,
it is not appropriate, to switch back to horizontal mode.
- Ok:
(a b (a
c b
d c
e) d)
- Confusing:
(a b
c
d e)
Emacs makes life easy
In scheme-mode the emacs editor will automatically indent the
new line in good style when you press return, and it may be used
to repair indentation that is messed up through program modification.
There is no excuse for bad indentation. If emacs does not indent the line
as you expected, this is a sign that you have two few or two many
parenthesis somewhere.
If you make a point of following the above rules, you will quickly get used
to them and they will allow you to quickly parse even very complicated
programs. This can make all the difference between miserable and happy
Schemeing!
chaynes@indiana.edu