You can test multiple conditions using boolean logic.
Boolean logic refers to combining and changing the results of predicates using and
,or
, and not
.
It follows the common sense way you look at things.
Is this and that true? Only if both are true. Is this or that true?
Yes, if either -- or both! -- are. Is this not true? Yes, if it's false.
x | y | (and x y) |
(or x y) |
(not x) |
(not y) |
---|---|---|---|---|---|
false | false | false | false | true | true |
true | false | false | true | false | true |
true | true | true | true | false | false |
false | true | false | true | true | false |
and
, or
, and not
can be combined to create a more complex condition.
Here is an example containing several conditions for confirming if a calendar year is a leap year.
(defn leap-year?
"Every four years, except years divisible by 100, but yes for years divisible by 400."
[year]
(and (zero? (mod year 4))
(or (zero? (mod year 400))
(not (zero? (mod year 100))))))
;;
(leap-year? 1984)
####Note::Try explain this function to another student If it seems complicated, take each of the conditions by themselves to see what they do. Try the conditions with different years
()
If you get stuck with the exercise, ask a volunteer to help.