if
Introduction
In its simplest form, the if conditional in Scheme evaluates a test and, based on the result, executes one of two possible code blocks. The simplest form looks like this:
(if test-is-true
do-this)- If the
testevaluates to true (#t), the code block in the consequent is executed. The block may return a value or perform other actions, such as assigning a variable or printing output.
Example
(if (< 0 1)
(gimp-message "True!"))- In this case, the
testis(< 0 1)(checking if 0 is less than 1). - Since the test evaluates to true (
#t), the code block(gimp-message "True!")is executed, which prints"True!".
Adding an Else Condition: if-else
When using an if conditional with an alternative code block (the else case), the structure looks like this:
(if test
do-this
else-do-this)- If the
testevaluates to true (#t), the consequent code block is executed. - If the
testevaluates to false (#f), the alternative code block is executed.
(if test
consequent
alternative)How It Works
Test Expression:
- The
testexpression is evaluated first.
- The
Result Based on Test:
- If the
testevaluates to true (#t), the consequent code block is executed. - If the
testevaluates to false (#f), the alternative code block is executed.
- If the
Both the consequent and alternative code blocks can perform any valid Scheme operation, including returning values, modifying variables, or running procedures.
Examples
Example 1: Returning a Value
(if (< 0 1)
1
0)- Here, the
testis(< 0 1)(checking if 0 is less than 1). - Since the test evaluates to true (
#t), the consequent block (1) is executed and its value is returned.
Result: 1
Example 2: Evaluating a begin Block
In cases where you need to perform multiple actions when the condition is true or false, you can use begin or a let to group them together.
(if (= 0 1)
(begin
(gimp-message "This won't run")
1)
(begin
(gimp-message "False condition met, calculating...")
(* 3 4)))- In this example, the
testis(= 0 1)(checking if 0 equals 1). - Since the test evaluates to false (
#f), the alternative block is executed:- First, it prints
"False condition met, calculating...". - Then, it calculates
(* 3 4)and returns12.
- First, it prints
Result: Prints “False condition met, calculating…” and returns 12.
Example 3: Evaluating a let Statement
Using a let allows us to declare local scope variables withing the code block.
(if (= 1 1)
(let (x -1)
(gimp-message "True condition met, calculating...")
(* x 10))
(let (y 4)
(gimp-message "This won't run")
(* 3 y)))- In this example, the
testis(= 1 1)(checking if 1 equals 1). - Since the test evaluates to true (
#t), the consequent block is executed:- First, it prints
"True condition met, calculating...". - Then, it calculates
(* -1 10)and returns-10.
- First, it prints
Result: Prints “True condition met, calculating…” and returns -10.
Summary
- The
ifconditional is a powerful tool in Scheme for evaluating tests and executing corresponding code blocks. - It can handle both simple expressions and complex code blocks that return values, modify variables, or perform side effects.
- Remember: If there is no explicit
elseblock, theifonly evaluates and executes the consequent if the test is true. Otherwise it evaluates and executes the alternative.