Next: , Previous: , Up: Control Structures   [Contents][Index]

5.13.2 Conditional Structures

Lisp provides a number of conditional constructs, the most complex of which (cond) takes a list of conditions, the first of which evaluates to true has its associated list of forms evaluated. Theoretically this is the only conditional special form necessary—all others can be implemented as macros.

Macro: if condition true-form else-forms…

The if form is the nearest thing in Lisp to the if-then-else construct found in most programming languages.

First the condition form is evaluated, if it returns true the true-form is evaluated and its result returned. Otherwise the result of an implicit progn on the else-forms is returned. If there are no else-forms false is returned.

Note that one of the true-form or the else-forms is completely ignored—it is not evaluated.

(if (special-form-p if)
    "`if' is a special form"
  "`if' is not a special form")
    ⇒ "`if' is not a special form"
Macro: when condition true-forms…

condition is evaluated, if it is true the result of an implicit progn on the true-forms is returned, otherwise false is returned.

(when t
  (message "Pointless")
    ⇒ foo
Macro: unless condition else-forms…

This special form evaluates condition, if its computed value is true, () is returned. Otherwise the else-forms are evaluated sequentially, the value of the last is returned.

Special Form: cond clause…

The cond special form is used to choose between an arbitrary number of conditions. Each clause is a list; the car of which is a condition, the cdr is a list of forms to evaluate (in an implicit progn) if the condition evaluates to true. This means that each clause looks something like:

(condition body-forms…)

and a whole cond form looks like:

 (condition-1 body-forms-1…)
 (condition-2 body-forms-2…)

The condition in each clause is evaluated in sequence (condition-1, then condition-2, …), the first one which evaluates to a true value has an implicit progn performed on its body-forms. The value of this progn is also the value of the cond statement.

If the true condition has no body-forms the value returned is the value of the condition. If none of the clauses has a true condition the value of the cond statement is false.

Often you want a default clause which has its body-forms evaluated when none of the other clauses are true. The way to do this is to add a clause with a condition of t and body-forms of whatever you want the default action to be.

 ((stringp buffer-list))        ;Clause with no body-forms
 ((consp buffer-list)
  (setq x buffer-list)          ;Two body-forms
 (t                             ;Default clause
  (error "`buffer-list' is corrupted!")))
    ⇒ t
Macro: case key clauses…

This special form is similar to cond, but switches on the result of evaluating a single form key, checking for equality with a number of other values, defined by the clauses. If any of these other values is the same as the result of evaluating key, then a sequence of forms associated with the value is evaluated.

Each element of the clauses list has the format:

((value-1 value-2value-n) forms…)

Each of the values in the car of the clause is tested for equality with key, using the eql function. If any test positively, then the associated forms are evaluated and the resulting value becomes the result of the special form.

Instead of supplying a list of possible values, it is also possible to just specify the symbol t. If such a clause is encountered, and no other clauses have matched the value of key, then this clause is assumed to match by default.

If any of the values in the clauses appear multiply, then the behaviour of the construct is undefined.

Here is an example use of case:

(case foo
  ((bar baz)
    (print "It was either bar or baz"))
    (print "It was quux"))
    (print "I've no idea what it was...")))

There are also a number of special forms which combine conditions together by the normal logical rules.

Macro: or forms…

The first of the forms is evaluated, if it is true its value is the value of the or form and no more of forms are evaluated. Otherwise this step is repeated for the next member of forms.

If all of the forms have been evaluated and none have a true value the or form evaluates to false.

(or nil 1 nil (beep))           ;(beep) won't be evaluated
    ⇒ 1
Macro: and forms…

The first of the forms is evaluated. If it is false no more of the forms are evaluated and false is the value of the and statement. Otherwise the next member of forms is evaluated and its value tested. If none of the forms are false the computed value of the last member of forms is returned from the and form.

(and 1 2 nil (beep))            ;(beep) won't be evaluated
    ⇒ ()

(and 1 2 3)                     ;All forms are evaluated
    ⇒ 3
Function: not object

This function inverts the truth value of its argument. If object is true, false is returned, otherwise true is returned.

(not nil)
    ⇒ t

(not t)
    ⇒ ()

(not (not 42))
    ⇒ t

Next: , Previous: , Up: Control Structures   [Contents][Index]