Scheme Conditional Statements
In Scheme there are two types of conditional statements: cond and if. Which one you use is ultimately up to you but in my opinion cond is much easier to write and follow. After this tutorial you can make that judgement for yourself. It is important to get really good at writing if statements as they are required for writing recursive functions which is the essence of scheme.
These conditional statements have two cases.
[TABLE="class: grid, width: 262"]
(define x 3) (define y 2) (cond [(= x 5) (+ x 2)] [else (+ x y)])[/TD]
(if (= x 5) (+ x 2) (+ x y))[/TD]
These two statements produce the same result. The first one uses cond which is a sequence of expressions in the form [(expression) (result)]. The second one using if has the form (if expression result1 result2). If expression is true then the result produced is result1 otherwise result2 is produced. If x is equal to 5 then the expression (+ x 2) is evaluated and the result is produced. Otherwise, the expression (+ x y) is produced after it is evaluated. When using if if you want a third condition you must have a nested if statement in result2.
With three or more conditions we will quickly see how messy these conditionals get. When using if statements we are actually making use of nested if statements.
[TABLE="class: grid, width: 523"]
(define (f s) (cond [(equal? "one" s) 1] [(equal? "two" s) 2] [(equal? "three" s) 3] [(equal? "four" s) 4] [(equal? "five" s) 5] [else 0]))[/TD]
(define (f s) (if (equal? s "one") 1 (if (equal? s "two") 2 (if (equal? s "three") 3 (if (equal? s "four") 4 (if (equal? s "five") 5 0))))))[/TD]
With the cond example it is easy to quickly see what each case is and what the result is. It takes more reading to figure out what is going on when using if and modifying the code is a bit harder since there is a lot of parenthesis being used. In the if example if all the if tests fail then the value produced is 0. At some point we must have a final value that will be produced if all conditions fail. This is the "else" value.
Scheme includes equality functions for various types. To compare if two string are equal use the function string=?:
(string=? "5" "3")
This function will return false as the two strings are not the same.
To compare if numbers are equal, use the = operator.
(= 5 5)
The output of this is:
#t is how scheme represents true and equivalently #f means false.
Generic equality function
The equal? function is used to test if two objects of any type are equal.
(equal? "5" 3);; #f (equal? 4 4) ;; #t (equal? 5.3 5.2) ;; #f (equal? "test" "x") ;; #f
The and operation is used in Boolean expressions when we need to make sure that two or more expressions are true at the same time.
An example expression:
(and (= x 5) (= y 3))
To trace what is happening with this expression consider the case where x = 5 and y = 4.
(and (= x 5) (= y 3))
First, we substitute in x = 5 and the expression becomes (and (= 5 5) (= y 3)). Expressions are evaluated by evaluating the inner most expressions first from left to right. So first (= 5 5) is obviously true so the expression becomes:
(and true (= y 3))
Now we substitute y = 4 and get the expression (and true (= 4 3)). The expression (= 4 3) is false so we get the expression (and true false). For the and expression to be true all operands must be true in the list. Since we have one false the and expression becomes false.
For or expression at least one of the operands must be true. Consider the expression (or (= x 5) (= y 3)) with x = 5 and y = 4 as before.
The expression will simplify to (or true false) as before. For the expression to be true, at least one of the operands to the or function must be true. So in this case the result is true.
Edited by chili5, 21 August 2011 - 06:12 AM.