We often want our lisp programs to make decisions. For instance, we might generate (with-sound) calls using a list of frequencies, and use different instruments depending upon the frequency. Or, we might analyze a soundfile, and take different actions depending upon the maximum amplitudes in a particular region.

In order to do these kinds of things, we need logical tests. Lisp provides two commands for this purpose: (if) and (cond).

(if)

The body of an if statement has three sections, quickly summarized as "if," "then," and "else." In full syntax:

(if (test-clause)
(then-clause)
(else-clause))

For instance, "if the current frequency is greater than 440, generate a sound from fm-violin; else generate a sound from grani." Try -

Cond is a more generic logic test than if; it allows you to specify many different conditions, instead of just one. This first example squares positive numbers, and returns the negative square of a negative number. In any other case it returns zero. ("t" automatically evaluates to true -- the equivalent of our else condition in the (if) statement).