when
Introduction
In Scheme, while if is elegant and versatile, it can become confusing when used without an explicit else. This is particularly true when the intention is to execute a single branch of code only when a condition is true, with no alternative action for the false case. In such scenarios, the when construct provides a clearer and more concise alternative.
The basic form of when looks like this:
(when test-is-true
do-this
do-that)- If the
testevaluates to true (#t), all the expressions in the body of thewhenconstruct are executed sequentially. - If the
testevaluates to false (#f), nothing happens, and no values are returned.
Example
(when (< 0 1)
(gimp-message "Condition is true!")
(gimp-message "Executing additional actions."))Contrasting if and when
To better understand the difference between if and when, consider the following example where both are used together:
(if (= 0 1)
(gimp-message "This will not run")
(when (< 0 1)
(gimp-message "The 'when' condition is true!")
(gimp-message "Executing multiple actions within 'when'.")))Explanation:
The
ifCondition:- The test
(= 0 1)checks if 0 is equal to 1. - Since this is false (
#f), theelsebranch of theifis executed.
- The test
The
whenConstruct in theelseBranch:- The
whentest(< 0 1)checks if 0 is less than 1. - Since this is true (
#t), all the expressions within the body of thewhenare executed sequentially:- First, it prints
"The 'when' condition is true!". - Then, it prints
"Executing multiple actions within 'when'.".
- First, it prints
- The
Why Use when Here?
- Using
wheninstead of anotherifsimplifies the logic when there is no need for an explicitelsebranch for the condition. whenmakes it clear that only the true branch is relevant, reducing potential confusion.
Summary
- Use
ifwhen you need both a true and false branch. - Use
whenwhen there is only a single branch for the true case, especially when multiple actions need to be executed. - Combining
ifandwhencan help structure more complex conditionals clearly and concisely.