GNU Info

Info Node: (librep.info)Conditional Structures

(librep.info)Conditional Structures


Next: Looping Structures Prev: Sequencing Structures Up: Control Structures
Enter node , (file) or (file)node

Conditional Structures
----------------------

   Lisp provides a number of conditional constructs, the most complex of
which (`cond') takes a list of conditions, the first of which evaluates
to true has its associated list of forms evaluated. Theoretically this
is the only conditional special form necessary--all others can be
implemented as macros.

 - Macro: if condition true-form else-forms...
     The `if' form is the nearest thing in Lisp to the "if-then-else"
     construct found in most programming languages.

     First the CONDITION form is evaluated, if it returns true the
     TRUE-FORM is evaluated and its result returned. Otherwise the
     result of an implicit progn on the ELSE-FORMS is returned. If
     there are no ELSE-FORMS false is returned.

     Note that one of the TRUE-FORM or the ELSE-FORMS is completely
     ignored--it is not evaluated.

          (if (special-form-p if)
              "`if' is a special form"
            "`if' is not a special form")
              => "`if' is not a special form"

 - Macro: when condition true-forms...
     CONDITION is evaluated, if it is true the result of an implicit
     progn on the TRUE-FORMS is returned, otherwise false is returned.

          (when t
            (message "Pointless")
            'foo)
              => foo

 - Macro: unless condition else-forms...
     This special form evaluates CONDITION, if its computed value is
     true, `()' is returned. Otherwise the ELSE-FORMS are evaluated
     sequentially, the value of the last is returned.

 - Special Form: cond clause...
     The `cond' special form is used to choose between an arbitrary
     number of conditions. Each CLAUSE is a list; the car of which is a
     CONDITION, the cdr is a list of forms to evaluate (in an implicit
     `progn') if the CONDITION evaluates to true.  This means that each
     CLAUSE looks something like:

          (CONDITION BODY-FORMS...)

     and a whole `cond' form looks like:

          (cond
           (CONDITION-1 BODY-FORMS-1...)
           (CONDITION-2 BODY-FORMS-2...)
           ...)

     The CONDITION in each CLAUSE is evaluated in sequence
     (CONDITION-1, then CONDITION-2, ...), the first one which
     evaluates to a true value has an implicit `progn' performed on its
     BODY-FORMS. The value of this `progn' is also the value of the
     `cond' statement.

     If the true CONDITION has no BODY-FORMS the value returned is the
     value of the CONDITION. If none of the clauses has a true
     CONDITION the value of the `cond' statement is false.

     Often you want a "default" clause which has its BODY-FORMS
     evaluated when none of the other clauses are true. The way to do
     this is to add a clause with a CONDITION of `t' and BODY-FORMS of
     whatever you want the default action to be.

          (cond
           ((stringp buffer-list))        ;Clause with no BODY-FORMS
           ((consp buffer-list)
            (setq x buffer-list)          ;Two BODY-FORMS
            t)
           (t                             ;Default clause
            (error "`buffer-list' is corrupted!")))
              => t

 - Macro: case key clauses...
     This special form is similar to `cond', but switches on the result
     of evaluating a single form KEY, checking for equality with a
     number of other values, defined by the CLAUSES. If any of these
     other values is the same as the result of evaluating KEY, then a
     sequence of forms associated with the value is evaluated.

     Each element of the CLAUSES list has the format:

          ((VALUE-1 VALUE-2 ... VALUE-N) FORMS...)

     Each of the values in the car of the clause is tested for equality
     with KEY, using the `eql' function. If any test positively, then
     the associated FORMS are evaluated and the resulting value becomes
     the result of the special form.

     Instead of supplying a list of possible values, it is also
     possible to just specify the symbol `t'. If such a clause is
     encountered, and no other clauses have matched the value of KEY,
     then this clause is assumed to match by default.

     If any of the values in the CLAUSES appear multiply, then the
     behaviour of the construct is undefined.

     Here is an example use of `case':

          (case foo
            ((bar baz)
              (print "It was either bar or baz"))
            ((quux)
              (print "It was quux"))
            (t
              (print "I've no idea what it was...")))

   There are also a number of special forms which combine conditions
together by the normal logical rules.

 - Macro: or forms...
     The first of the FORMS is evaluated, if it is true its value is
     the value of the `or' form and no more of `forms' are evaluated.
     Otherwise this step is repeated for the next member of FORMS.

     If all of the FORMS have been evaluated and none have a true value
     the `or' form evaluates to false.

          (or nil 1 nil (beep))           ;`(beep)' won't be evaluated
              => 1

 - Macro: and forms...
     The first of the FORMS is evaluated. If it is false no more of the
     FORMS are evaluated and false is the value of the `and' statement.
     Otherwise the next member of FORMS is evaluated and its value
     tested. If none of the FORMS are false the computed value of the
     last member of FORMS is returned from the `and' form.

          (and 1 2 nil (beep))            ;`(beep)' won't be evaluated
              => ()
          
          (and 1 2 3)                     ;All forms are evaluated
              => 3

 - Function: not object
     This function inverts the truth value of its argument. If OBJECT
     is true, false is returned, otherwise true is returned.

          (not nil)
              => t
          
          (not t)
              => ()
          
          (not (not 42))
              => t


automatically generated by info2www version 1.2.2.9