Next: , Previous: , Up: baselib expressions   [Index]


4.4.3 Conditionals

Syntax: if ?test ?consequent ?alternate
Syntax: if ?test ?consequent

?test, ?consequent, and ?alternate must be expressions.

When the syntax use has both the ?consequent and the ?alternate forms: ?test is evaluated; if it yields a true value, the ?consequent is evaluated and its return values are returned; otherwise the ?alternate is evaluated and its return values are returned. The ?consequent and ?alternate expressions are in tail context if the if expression itself is. The ?consequent and ?alternate are required to return the same number of values, unless they raise an exception.

When the syntax use has only the ?consequent form: ?test is evaluated; if it yields a true value, the ?consequent is evaluated and its return values are discarded. In this case the syntax use always returns zero values.

NOTE The specification of the one–armed if expression breaks compliance with R6RS. According to the standard: when ?test returns a true value the return value of ?consequent is returned; otherwise the expression returns unspecified values.

(if (> 3 2) 'yes 'no)           ⇒ yes
(if (> 2 3) 'yes 'no)           ⇒ no
(if (> 3 2)
    (- 3 2)
    (+ 3 2))                    ⇒ 1

(if #t #t)                      ⇒ no values
(if #f #f)                      ⇒ no values