Exercise · Volume 1 · 1.4.2-3
Exercise 1.4.2-3
not stated yet in Lean solution written
| Rating | 20 |
|---|---|
| Kind | K2, decide: say whether a claim holds from the written solution |
| Section | Section 1.4.2 of Volume 1. Companion chapter: Coroutines |
The task
True or false: the three instructions CMPA PERIOD in OUT can all be removed and the program still works.
The exact wording is in your copy of the book.
Hints
Open one hint at a time. Try the exercise again after each.
Hint 1
After you remove them, which instruction decides if the character is a period? Which comparison made the last setting of the indicator?
Hint 2
Look at the one comparison that is left in IN, and ask which values of rA it sees.
Hint 3
Look at the case of a period that follows a digit.
Solution
Show the solution
False, but very nearly true.
The jump JE in OUT looks at the comparison indicator. With the three tests removed, the indicator is the one that the last comparison of the whole program left. The only comparison left is the one in IN that compares a character, after has been subtracted, with .
- If the character is a period (code ), the subtracted value is . The test finds equal, and the period is sent to OUT with the indicator on equal. So
JEis taken, and the program ends as it should. - If the character is a letter, the subtracted value is negative. IN does not make the comparison, because the jump on negative rA comes first. The indicator keeps its old value. It is equal only if a period has been seen before, which does not happen before the end.
- If the character is a digit or a special character other than the period, the comparison gives less or greater, never equal.
There are two faults.
- The indicator is not set at the start. If its first value is equal, the first letter of the input is taken for the end.
- A period that follows a replication digit is not seen. The digit makes the comparison with result less. Then IN reads the period and sends it to OUT without a new comparison, so the indicator still says less. The period is then treated as a normal character and repeated, and the program does not end.
Check
Take the input A2. The digit gives subtracted value , and the comparison with is less. The period is then sent three times with the indicator on less. No JE is taken and OUT goes on asking for characters after the end. For the input A. the period passes through the comparison and the result is equal, and the program ends as it should. This was done by hand with the code of the text, line by line.
Remarks
If one wants to remove the comparisons in OUT, one can put a CMPA PERIOD once into IN, before the call of OUT, and make the state of the indicator a part of the entry condition of OUT. That is a documented way, and it changes the characteristics of the coroutines.
This solution is drafted, and a person has not reviewed it yet. The source.
In Lean
not stated yet The exercise is not stated in Lean yet.
The status is computed from the Lean build. The node of the map.
Something is wrong? Report it.