Go to the first, previous, next, last section, table of contents.

Indenting cond

Be careful about parentheses and indenting with cond. Notice that the expressions within a test-action clause are indented by only one character, but that's very significant. Without that indenting, a cond is very hard to read.

Suppose we replace the following awkward if expression with a cond.

;; awkward if expression requiring begins to sequence actions in branches
(if (a)
    (begin (b)
           (c))
    (begin (e)
           (f)))

We could write it like this:

(cond ((a)
       (b)
       (c))
      (else
       (e)
       (f)))

Sometimes, when the clauses of a cond are small, a whole clause will be written out horizontally. The above example is likely to be written like this:

(cond ((a) (b) (c))
      (else (d) (e)))

Also be careful about the parentheses around condition expressions. Notice that the parentheses around (a) are there because the condition is call to a with zero arguments, not because you always put parentheses around the condition expression. (Notice that there are no parentheses around else, and there wouldn't be parentheses around a if we just wanted to test the value of the variable a, rather than call it and test the result.)


Go to the first, previous, next, last section, table of contents.