Comparisons, Conditionals and Predicates

As with any Turing-complete language, Scheme has a mechanism for comparing different values and branching two or more possibilities based on the comparison.

Comparisons

The usual set of equality, and inequality comparators are available: (equal?), (<) (<=), (>), and (>=) . There are also the logical functions, (not), (and), and (or), as well as specialized equivalence forms for specific types (string=?), etc...

Conditionals

The basic conditional special forms are (if), (case) and (cond). (If) is used for a simple two-way branch or skip:

   (if (condition) (true-expression))
  
or
   (if (condition) (true-expression) (false-expression))
  

The if form returns the value of the clause that it runs; if the condition is false and there is no else clause, the result is undefined.

the (case) special form performs a multi-path branch based on a single value:

   (case value (comparison-1 (block-1))
               (comparison-2 (block-2))
                 ; ...
               (else (default-block)))
  

If value matches one of the comparison values in a list, then the following expression block is executed.

The expression sections are blocks, and can take more than one expression.

The (cond) special form is the most general of the conditionals; it accepts a set of condition-expression pairs and selects through them in order. As with (case), the expression clause is a block.

   (cond ((cond-1) (block-1))
         ((cond-2) (block-2))
         ; ...
         (else (default-block))
  

Predicates

In addition to the selectors and comparators, there are a set of predicates, special forms which accept an argument and determine the arguments membership in a particular type. Predicates are customarily suffixed with a question mark to indicate what they are, but exceptions exist. Important predicates include (null?), (list?), (atom?), (string?), (char?). These dynamic type-checking forms are used in place of the static type checking used in most languages.

Contents Previous Next