Exercise 4: Make a decision¶
Aim: test a boundary and both branches of if. Save ex04.fib.
Type and predict¶
(defun main () -> i64
(let ((temperature 18)
(limit 20))
(do
(println (< temperature limit))
(println (= temperature limit))
(println (if (< temperature limit) "bring a coat" "leave the coat"))
0)))
Comparisons produce booleans, true or false. if chooses one of its two
branches and produces that branch's value. Both branches here produce str.
It does not execute both branches and discard one answer.
Change it¶
- Run temperatures
19,20and21. Write the full expected output first. - Replace
<with<=. Which of those three cases changes? - Use
(and (>= temperature 10) (< temperature 20))as the condition. Test9,10,19and20.
Break and repair¶
Replace the string in one branch with a number. The two result types no longer agree. Restore a string in that branch; do not “fix” it by deleting the test.
Checkpoint¶
Describe the exact interval in drill 3 in words, including both ends. Explain
why testing only temperature 18 would miss a boundary mistake.