The 'case' special form first evaluates 'expr', the return value of this evaluation is then compared against all the 'value' entries:
(case expr (value-1 action-1) (value-2 action-2) ... (value-n action-n))
If 'value' is a single atom, the atom is compared against 'expr':
> (case 'a ('a "a") ('b "b")) "a"
If 'value' is a list, each of the elements of the list are compared against 'expr':
> (case 'a ((1 2 3 4) "number") ((a b c d) "alpha")) "alpha"
The 'action' associated with the first 'value' that matches 'expr' is evaluated and returned as the result of the 'case' special form.
If no 'value' matches, NIL is returned:
> (case 'c ('a "a") ('b "b")) NIL
If the last 'value' is the symbol T and
no other 'value' has matched 'expr', then 'case' will evaluate the 'action'
associated
> (case 3 (1 "one") (2 "two") (t "no match")) "no match"
If there are multiple T entries, the first is considered to be the end of the 'case':
> (case 9 (1 "one") (t "first t") (t "second t")) "first t"
Note: The 'case' special form does not work with a list or string as the 'expr' because 'case' uses eql which cannot compare lists or strings:
> (case "a" ; doesn't work! ("a" 'a) ("b" 'b)) NIL
The cond special form can be used to test Lisp expressions that cannot be handled by 'case'.
(case) => NIL (case 'a) => NIL (defun c-type (expr) (case (type-of expr) (flonum "float") (fixnum "integer") (string "string") (cons "non-empty list") (nil "empty list") (t "other"))) (c-type 1.2) => "float" (c-type 3) => "integer" (c-type "ab") => "string" (c-type '(a b)) => "non-empty list" (c-type '()) => "empty list" (c-type 'a) => "other"
See also: