Exercise 08 Conditionals - MikeyYeahYeah/clojure-koans GitHub Wiki
Exercise 08 - Conditionals
Working with If statements
The false? is asking if the expression that follows is a false statement. If the expression is false, then it satisfies the false? test and returns a true. Being as the statement is true, the first value is returned which is :a. Otherwise the 2nd value is returned.
(if (false? (= 4 5))
:a
:b)
;;=> :a
What if there were no second value? If the condition were false and there is no 2nd value, then a nil is returned.
(if (> 4 3)
:a)
;;=> :a
(if (> 3 4)
:a)
;;=> nil
Working with Multiple Paths (cond)
In this example, we can see that there are multiple paths to test against.
(let [x 5]
(= :your-road (cond (= x 3) :road-not-taken
(= x 4) :another-road-not-taken
:else :your-road)))
Testing if a condition isn't true (if-not)
- zero? 1
- Is this number equal to zero?
- False
- Is this number equal to zero?
- if-not (false)
- If the expression is not true, this satisfies the if-not condition meaning its true and the first value will be evaluated.
(if-not (zero? 1)
:doom
:gloom)
;;=> :doom