elisp: Conditionals

 
 10.2 Conditionals
 =================
 
 Conditional control structures choose among alternatives.  Emacs Lisp
 has four conditional forms: ‘if’, which is much the same as in other
 languages; ‘when’ and ‘unless’, which are variants of ‘if’; and ‘cond’,
 which is a generalized case statement.
 
  -- Special Form: if condition then-form else-forms...
      ‘if’ chooses between the THEN-FORM and the ELSE-FORMS based on the
      value of CONDITION.  If the evaluated CONDITION is non-‘nil’,
      THEN-FORM is evaluated and the result returned.  Otherwise, the
      ELSE-FORMS are evaluated in textual order, and the value of the
      last one is returned.  (The ELSE part of ‘if’ is an example of an
      implicit ‘progn’.  SeeSequencing.)
 
      If CONDITION has the value ‘nil’, and no ELSE-FORMS are given, ‘if’
      returns ‘nil’.
 
      ‘if’ is a special form because the branch that is not selected is
      never evaluated—it is ignored.  Thus, in this example, ‘true’ is
      not printed because ‘print’ is never called:
 
           (if nil
               (print 'true)
             'very-false)
           ⇒ very-false
 
  -- Macro: when condition then-forms...
      This is a variant of ‘if’ where there are no ELSE-FORMS, and
      possibly several THEN-FORMS.  In particular,
 
           (when CONDITION A B C)
 
      is entirely equivalent to
 
           (if CONDITION (progn A B C) nil)
 
  -- Macro: unless condition forms...
      This is a variant of ‘if’ where there is no THEN-FORM:
 
           (unless CONDITION A B C)
 
      is entirely equivalent to
 
           (if CONDITION nil
              A B C)
 
  -- Special Form: cond clause...
      ‘cond’ chooses among an arbitrary number of alternatives.  Each
      CLAUSE in the ‘cond’ must be a list.  The CAR of this list is the
      CONDITION; the remaining elements, if any, the BODY-FORMS.  Thus, a
      clause looks like this:
 
           (CONDITION BODY-FORMS...)
 
      ‘cond’ tries the clauses in textual order, by evaluating the
      CONDITION of each clause.  If the value of CONDITION is non-‘nil’,
      the clause succeeds; then ‘cond’ evaluates its BODY-FORMS, and
      returns the value of the last of BODY-FORMS.  Any remaining clauses
      are ignored.
 
      If the value of CONDITION is ‘nil’, the clause fails, so the ‘cond’
      moves on to the following clause, trying its CONDITION.
 
      A clause may also look like this:
 
           (CONDITION)
 
      Then, if CONDITION is non-‘nil’ when tested, the ‘cond’ form
      returns the value of CONDITION.
 
      If every CONDITION evaluates to ‘nil’, so that every clause fails,
      ‘cond’ returns ‘nil’.
 
      The following example has four clauses, which test for the cases
      where the value of ‘x’ is a number, string, buffer and symbol,
      respectively:
 
           (cond ((numberp x) x)
                 ((stringp x) x)
                 ((bufferp x)
                  (setq temporary-hack x) ; multiple body-forms
                  (buffer-name x))        ; in one clause
                 ((symbolp x) (symbol-value x)))
 
      Often we want to execute the last clause whenever none of the
      previous clauses was successful.  To do this, we use ‘t’ as the
      CONDITION of the last clause, like this: ‘(t BODY-FORMS)’.  The
      form ‘t’ evaluates to ‘t’, which is never ‘nil’, so this clause
      never fails, provided the ‘cond’ gets to it at all.  For example:
 
           (setq a 5)
           (cond ((eq a 'hack) 'foo)
                 (t "default"))
           ⇒ "default"
 
      This ‘cond’ expression returns ‘foo’ if the value of ‘a’ is ‘hack’,
      and returns the string ‘"default"’ otherwise.
 
    Any conditional construct can be expressed with ‘cond’ or with ‘if’.
 Therefore, the choice between them is a matter of style.  For example:
 
      (if A B C)
      ≡
      (cond (A B) (t C))
 

Menu