Exercise · Volume 1 · 1.4.2-2
Exercise 1.4.2-2
not stated yet in Lean solution written
| Rating | 20 |
|---|---|
| Kind | K3, find a value: compute a number, a set or a closed form from the written solution |
| Section | Section 1.4.2 of Volume 1. Companion chapter: Coroutines |
The task
Say what would happen if the program of the text started with the coroutine IN instead of OUT.
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
Follow the first call of IN to OUT. Which instruction of IN is the next one to be run when OUT first jumps back to IN?
Hint 2
Write the first card of the output for the sample input and compare it with the correct one.
Solution
Show the solution
Control starts at the beginning of IN. It fetches the first character, and in the sample this is the letter A. IN then jumps to OUT, and this is the first entry to OUT. The linkage of OUT stores the return point and goes to the start of OUT, which sets up the output area and clears the word counter. At the end of this set-up, OUT asks IN for a character. IN is now resumed at the point after its own call of OUT, as if OUT had accepted the first character. So OUT never saw it.
The first character of the input is therefore lost, and every later character is sent to OUT as a normal part of the stream. The rest of the work goes on correctly, and the program does not stop in a wrong way.
The cure is to start OUT first, as the text does. Or one can do the set-up of OUT by hand before IN starts, and let the first call of OUT from IN enter the part of OUT that takes a character.
Check
For the sample input the correct output is ABB BEE EEE E44 446 66F GZY W22 220 0PQ 999 999 999 R.. If the first letter is lost, the program makes the groups of the string without its first character: BBB EEE EEE 444 466 6FG ZYW 222 200 PQ9 999 999 99R .. The two strings were made by a short decoder in Python and they agree with a hand trace of the first calls.
Remarks
The same loss happens if the first entry of any coroutine pair is made to the routine that produces data. The one that first consumes must be started first, or the first item must be given to it by another way.
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.