(cond (statement1 result1 ...) ...)


Do it, if it's true.


This function is a commonly used conditional in LISP. If statement1 evaluates to true, any functions that follow within that block are performed, and the result of the last function is returned.

    • If statement1 evaluates to nil, LISP continues to statement2. If true, it follows the same procedure.
    • If false (or nil), LISP continues to search for a true statement.
    • If no true statements are found, a nil is returned.
    • If there are no result functions in the block, the result of the condition is returned.

Examples

Code Results
(cond ((zerop x) 0) (T nil))

In the previous example, the cond function looks at (zeropx) and decides if it is not nil. If so, it returns zero (0). If not, it moves to the second test, (T nil). This test is already true (T) and would return a nil. Look at the following setqs of actual examples of the above condition.

Code Results
(setq x 8) nil
(setq x 0) 0

In the next example, if d is set to 5, LISP returns the string "Equal to." If set to 6, it returns "Greater than."

Code
(cond ((< d 5) "Less than")
((= d 5) "Equal to")
((> d 5) "Greater than")
)

Tell me about...

(if test statement1 [statement2])

(repeat number statement1 [statement2 ...])

(while text statement )

LISP Compatibility

Programming Overview of LISP (LISt Processing) Language