XLISP > XLISP 2.0  -  Contents  -  Reference  -  Previous | Next


Type:   -   special form (fsubr)
Source:   -   xlcont.c


(case expr [(value action) ... ])
expr - an expression
value - an unevaluated expression or list of unevaluated expressions to compare against 'expr'
action - one or more expressions to execute if the case matches
returns - the value of the last expression of the matching case


The 'case' special form is a selection control form. 'case' evaluates 'expr'. This value is then compared against all the 'value' entries. If 'value' is a single atom, the atom is compared against 'expr'. If 'value' is a list, each of the elements of the list are compared against 'expr'. 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, a NIL is returned. If the last 'value' is the  T  symbol and no other 'value' has matched 'expr', then 'case' will evaluate the 'action' associated with  T . If there are multiple  T  entries, the first is considered to be the end of the 'case'.


(case 'a ('a "a"))                     ; returns "a"
(case 'a ('b "b"))                     ; returns NIL
(case 9 (1 "num") (t "ho") (t "hi"))   ; returns "ho"
(case 'a ((1 2 3 4) "number")
         ((a b c d) "alpha"))          ; returns "alpha"
(case 'a)                              ; returns NIL
(case)                                 ; returns NIL
(defun print-what (parm)               ; define a function
  (case (type-of parm)                 ; check PARM type
        (flonum   (print "float"))
        (fixnum   (print "integer"))
        (string   (print "string"))
        (cons     (print "list"))
        (T        (print "other")))    ; otherwise
  NIL)                                 ; and always return NIL
(print-what 1.2)                       ; prints  "float"   returns NIL
(print-what 3)                         ; prints  "integer" returns NIL
(print-what "ab")                      ; prints  "string"  returns NIL
(print-what '(a b))                    ; prints  "list"    returns NIL
(print-what 'a)                        ; prints  "other"   returns NIL

Note: The 'case' special form does not work with a list or string as the 'expr'. This is because 'case' defines the test used to be the eql test which does not work with lists or strings, only symbols and numbers.

Common Lisp: In XLISP, you can use the value  T  as the last value to get the effect of 'otherwise'. Common Lisp uses the symbol 'otherwise' and  T  for this. If you are porting code from Common Lisp to XLISP, be careful to make sure  T  is used instead of 'otherwise' in 'case' statements. Note that no error will be generated, XLISP will just try to match the 'expr' to 'otherwise'.

See the case special form in the XLISP 2.0 manual.

  Back to Top

XLISP > XLISP 2.0  -  Contents  -  Reference  -  Previous | Next