7.2 Case and Selectq Statements

Case is a form of conditional in which a ”key” value is compared against a set of values in order to choose a corresponding set of forms to evaluate. The ”key” value must be an integer. This is a restricted cond, and therefore can be compiled into more efficient code. The compiler expends some effort to examine special cases (for example compact vs. non-compact sets of cases and short vs. long sets of cases).

(case I:form [U:case-clause]): any open-compiled fexpr
Case selects a case-clause (one of the U’s), to evaluate based on the value of I. The expression I should evaluate to an integer. Each clause has the form (case-expression form), where the case-expression has one of the following forms.
nil the default case
(i1 i2 ... in) where each ik is an integer
((range low high))where low and high are integers and low is less
than or equal to high

The following example illustrates the use of case

    (case i ((1) (print "first"))  
            ((2 3) (print "second"))  
            (((range 4 10)) (print "third"))  
            (nil (print "fourth")))

(selectq I:form [U:selectq-clause]): any macro
This function selects an action based on the value of the ”key” I. Each selectq-clause is of the form (key-part [action]). The key-part is a list of keys, t, or otherwise. If there is only one key in a key-part it may be written in place of a list containing it, provided that the key is not a list, nil, t, or otherwise.

After I is evaluated, it is compared against the members of each key-part in turn. If the key is eq to any member of the key-part then each of the forms in that selectq-clause are evaluated. The value of the last form of the list is the value of the selectq. If a selectq-clause with key-part t or otherwise is reached, its forms are evaluated without further testing. A t or otherwise clause should be the last clause. If no clause is satisfied then nil is returned.

    (selectq (car w)  
      ((nil) nil)  
      (end (print 'done) 'end)  
      ((0 1 2 3 4 5 6 7 8 9) 'digit)  
      (otherwise 'other))