r7rs-small-texinfo

Unnamed repository; edit this file 'description' to name the repository.
git clone https://kaka.farm/~git/r7rs-small-texinfo
Log | Files | Refs | README | LICENSE

conditionals.texinfo (8483B)


      1 @node Conditionals derived
      2 @subsection Conditionals
      3 
      4 @deffn syntax cond @svar{clause@sub{1}} @svar{clause@sub{2}}@dots{}
      5 @deffnx {auxiliary syntax} else
      6 @deffnx {auxiliary syntax} =>
      7 
      8 @cindex true
      9 
     10 Syntax: @svar{Clauses} take one of two forms, either
     11 
     12 @display
     13 @code{(}@svar{test} @svar{expression@sub{1}} @dots{}@code{)}
     14 @end display
     15 
     16 where @svar{test} is any expression, or
     17 
     18 @display
     19 @code{(}@svar{test}@code{ => }@svar{expression}@code{)}
     20 @end display
     21 
     22 The last @svar{clause} can be an ``else clause,'' which has the form
     23 
     24 @display
     25 @code{(else }@svar{expression@sub{1}} @svar{expression@sub{2}} @dots{}@code{)}.
     26 @end display
     27 
     28 Semantics: A @code{cond} expression is evaluated by evaluating the
     29 @svar{test} expressions of successive @svar{clause}s in order until one of
     30 them evaluates to a true value (@xref{Booleans}). When a @svar{test}
     31 evaluates to a true value, the remaining @svar{expression}s in its
     32 @svar{clause} are evaluated in order, and the results of the last
     33 @svar{expression} in the @svar{clause} are returned as the results of the
     34 entire cond expression.
     35 
     36 If the selected @svar{clause} contains only the @svar{test} and no
     37 @svar{expression}s, then the value of the @svar{test} is returned as the
     38 result. If the selected @svar{clause} uses the @code{=>} alternate form,
     39 then the @svar{expression} is evaluated. It is an error if its value is not
     40 a procedure that accepts one argument. This procedure is then called on the
     41 value of the @svar{test} and the values returned by this procedure are
     42 returned by the @code{cond} expression.
     43 
     44 If all @svar{test}s evaluate to @code{#f}, and there is no else clause, then
     45 the result of the conditional expression is unspecified; if there is an else
     46 clause, then its @svar{expression}s are evaluated in order, and the values
     47 of the last one are returned.
     48 @end deffn
     49 
     50 @lisp
     51 (cond ((> 3 2) 'greater)
     52       ((< 3 2) 'less))   @result{} greater
     53 
     54 (cond ((> 3 3) 'greater)
     55       ((< 3 3) 'less)
     56       (else 'equal))     @result{} equal
     57 
     58 (cond ((assv 'b '((a 1) (b 2))) => cadr)
     59       (else #f))         @result{} 2
     60 @end lisp
     61 
     62 @deffn syntax case @svar{key} @svar{clause@sub{1}} @svar{clause@sub{2}}@dots{}
     63 
     64 Syntax: @svar{Key} can be any expression. Each @svar{clause} has the form
     65 
     66 @display
     67 @code{((}@svar{datum@sub{1}} @dots{}@code{) }@svar{expression@sub{1}} @svar{expression@sub{2}} @dots{}@code{)},
     68 @end display
     69 
     70 where each @svar{datum} is an external representation of some object. It is
     71 an error if any of the @svar{datum}s are the same anywhere in the
     72 expression. Alternatively, a @svar{clause} can be of the form
     73 
     74 @display
     75 @code{((}@svar{datum@sub{1}} @dots{})@code{ => }@svar{expression}@code{)}
     76 @end display
     77 
     78 The last @svar{clause} can be an ``else clause,'' which has one of the forms
     79 
     80 @display
     81 @code{(else }@svar{expression@sub{1}} @svar{expression@sub{2}} @dots{}@code{)}
     82 @end display
     83 
     84 or
     85 
     86 @display
     87 @code{(else => }@svar{expression}@code{)}.
     88 @end display
     89 
     90 Semantics: A @code{case} expression is evaluated as follows. @svar{Key} is
     91 evaluated and its result is compared against each @svar{datum}. If the
     92 result of evaluating @svar{key} is the same (in the sense of @code{eqv?};
     93 @xref{Equivalence predicates}) to a @svar{datum}, then the expressions in
     94 the corresponding @svar{clause} are evaluated in order and the results of
     95 the last expression in the @svar{clause} are returned as the results of the
     96 @code{case} expression.
     97 
     98 If the result of evaluating @svar{key} is different from every @svar{datum},
     99 then if there is an else clause, its expressions are evaluated and the
    100 results of the last are the results of the @code{case} expression; otherwise
    101 the result of the case expression is unspecified.
    102 
    103 If the selected @svar{clause} or else clause uses the @code{=>} alternate
    104 form, then the @svar{expression} is evaluated. It is an error if its value
    105 is not a procedure accepting one argument. This procedure is then called on
    106 the value of the @svar{key} and the values returned by this procedure are
    107 returned by the @code{case} expression.
    108 @end deffn
    109 
    110 @lisp
    111 (case (* 2 3)
    112   ((2 3 5 7) 'prime)
    113   ((1 4 6 8 9) 'composite))     @result{}  composite
    114 (case (car '(c d))
    115   ((a) 'a)
    116   ((b) 'b))                     @result{}  unspecified
    117 (case (car '(c d))
    118   ((a e i o u) 'vowel)
    119   ((w y) 'semivowel)
    120   (else => (lambda (x) x)))     @result{}  c
    121 @end lisp
    122 
    123 @deffn syntax and @svar{test@sub{1}}@dots{}
    124 
    125 Semantics: The @svar{test} expressions are evaluated from left to right, and
    126 if any expression evaluates to @code{#f} (@xref{Booleans}), then @code{#f}
    127 is returned. Any remaining expressions are not evaluated. If all the
    128 expressions evaluate to true values, the values of the last expression are
    129 returned. If there are no expressions, then @code{#t} is returned.
    130 @end deffn
    131 
    132 @lisp
    133 (and (= 2 2) (> 2 1))           @result{}  #t
    134 (and (= 2 2) (< 2 1))           @result{}  #f
    135 (and 1 2 'c '(f g))             @result{}  (f g)
    136 (and)                           @result{}  #t
    137 @end lisp
    138 
    139 @deffn syntax or @svar{test@sub{1}}@dots{}
    140 
    141 Semantics: The @svar{test} expressions are evaluated from left to right, and
    142 the value of the first expression that evaluates to a true value
    143 (@xref{Booleans}) is returned. Any remaining expressions are not evaluated.
    144 If all expressions evaluate to @code{#f} or if there are no expressions,
    145 then @code{#f} is returned.
    146 @end deffn
    147 
    148 @lisp
    149 (or (= 2 2) (> 2 1))            @result{}  #t
    150 (or (= 2 2) (< 2 1))            @result{}  #t
    151 (or #f #f #f) @result{}  #f
    152 (or (memq 'b '(a b c))
    153     (/ 3 0))                    @result{}  (b c)
    154 @end lisp
    155 
    156 @deffn syntax when @svar{test} @svar{expression@sub{1}} @svar{expression@sub{2}}@dots{}
    157 
    158 Syntax: The @svar{test} is an expression.
    159 
    160 Semantics: The test is evaluated, and if it evaluates to a true value, the
    161 expressions are evaluated in order. The result of the @code{when} expression
    162 is unspecified.
    163 @end deffn
    164 
    165 @lisp
    166 (when (= 1 1.0)
    167   (display "1")
    168   (display "2")) @result{} @r{unspecified}
    169 @print{} 12
    170 @end lisp
    171 
    172 @deffn syntax unless @svar{test} @svar{expression@sub{1}} @svar{expression@sub{2}}@dots{}
    173 
    174 Syntax: The @svar{test} is an expression.
    175 
    176 Semantics: The test is evaluated, and if it evaluates to @code{#f}, the
    177 expressions are evaluated in order. The result of the @code{unless}
    178 expression is unspecified.
    179 @end deffn
    180 
    181 @display
    182 
    183 (unless (= 1 1.0)
    184   (display "1")
    185   (display "2")) @result{} @r{unspecified}
    186 @print{} @r{nothing}
    187 
    188 @end display
    189 
    190 @deffn syntax cond-expand @svar{ce-clause@sub{1}} @svar{ce-clause@sub{2}}@dots{}
    191 
    192 Syntax: The @code{cond-expand} expression type provides a way to statically
    193 expand different expressions depending on the implementation. A
    194 @svar{ce-clause} takes the following form:
    195 
    196 @display
    197 @code{(}@svar{feature requirement} @svar{expression} @dots{}@code{)}
    198 @end display
    199 
    200 The last clause can be an ``else clause,'' which has the form
    201 
    202 @display
    203 @code{(else }@svar{expression} @dots{}@code{)}
    204 @end display
    205 
    206 A @svar{feature requirement} takes one of the following forms:
    207 
    208 @itemize
    209 @item
    210 @svar{feature identifier}
    211 
    212 @item
    213 @code{(library }@svar{library name}@code{)}
    214 
    215 @item
    216 @code{(and }@svar{feature requirement} @dots{}@code{)}
    217 
    218 @item
    219 @code{(or }@svar{feature requirement} @dots{}@code{)}
    220 
    221 @item
    222 @code{(not }@svar{feature requirement}@code{)}
    223 
    224 @end itemize
    225 
    226 Semantics: Each implementation maintains a list of feature identifiers which
    227 are present, as well as a list of libraries which can be imported. The value
    228 of a @svar{feature requirement} is determined by replacing each
    229 @svar{feature identifier} and @code{(library }@svar{library name}@code{)} on
    230 the implementation's lists with @code{#t}, and all other feature identifiers
    231 and library names with @code{#f}, then evaluating the resulting expression
    232 as a Scheme boolean expression under the normal interpretation of
    233 @code{and}, @code{or}, and @code{not}.
    234 
    235 A @code{cond-expand} is then expanded by evaluating the @svar{feature
    236 requirement}s of successive @svar{ce-clause}s in order until one of them
    237 returns @code{#t}. When a true clause is found, the corresponding
    238 @svar{expression}s are expanded to a @code{begin}, and the remaining clauses
    239 are ignored. If none of the @svar{feature requirement}s evaluate to
    240 @code{#t}, then if there is an else clause, its @svar{expression}s are
    241 included. Otherwise, the behavior of the @code{cond-expand} is unspecified.
    242 Unlike @code{cond}, @code{cond-expand} does not depend on the value of any
    243 variables.
    244 
    245 The exact features provided are implementation-defined, but for portability
    246 a core set of features is given in appendix B.
    247 @end deffn