A small program
By the end of this lesson you can write a whole program from a description: plan the cells, build it in parts, and test it.
One gap
This program reads a digit and prints it, and it is almost what is asked for. Change it so that it prints the next digit: 3 gives 4. It is tried with 3, 0 and 8:
Hint
A digit in a cell is a number. What is the next number?
Notice that one plus sign is all it took: ,+. You did this in Lesson 6. Before you go on, type 9 in the input box. It prints a colon, because number 58 comes after 57 in the table, and the program knows nothing about digits.
Whether that matters depends on what was asked. Make it a habit to try the edges: the smallest input, the largest, and nothing at all.
A plan and a gap
This program should print the digits 0123456789. Its plan is written into it as words, which the machine skips, and the cells are set up for you. Write the inside of the loop:
Hint
Inside the loop you stand on the counter, cell 2. The character is one cell to the left.
Hint
Go left, print, add one, go right, take one off the counter.
Notice how little there was to write. The plan came first: cell 1 holds the character to print, and cell 2 counts the rounds. With that written down the loop nearly writes itself: <.+>−
Deciding what every cell is for, before you type a single command, is the most useful habit in brainfuck. Keep the eight symbols out of the words of the plan. (The program you started from had an empty loop, which never ends. The machine stops any run after 200,000 steps.)
A plan in words
Now the plan is three lines above the box, and the program is yours. Write a program that reads one digit and prints that many stars: 3 gives ***. It is tried with 3, 0 and 9:
1. Read the digit into cell 0 and take 48 off, so that it is a number. 2. Put 42, the star, in cell 2. 3. Loop on cell 0: print cell 2, take one off cell 0. Cells: 0 holds the number of stars still to print, 1 is free for building numbers, 2 holds the star.
Hint
Part 1: use cell 1 as a counter. Go right, 6 plus signs, then a loop that takes 8 off cell 0 each round. You end on cell 1, which is empty again.
Hint
Part 2, from cell 1: 6 plus signs and a loop that adds 7 to cell 2 each round. Part 3: go to cell 0 and loop: two right, print, two left, minus.
Notice that the program has three parts, one for each line of the plan. Each is something you have done before: taking 48 off in Lesson 8, building a number in Lesson 5, and a loop that counts down in Lesson 4. They are joined by an agreement about the cells.
Test each part before you write the next: the tape under the box shows whether part 1 really left the number, and not 48 too many. For the input 0 the last loop never starts, and nothing is printed. You did not have to do anything for that edge. It came with the loop.
On your own
This time there is only a sentence. Write a program that reads one digit and counts down from it to 0: 3 gives 3210. Plan the cells first. The hints go from a small push to the whole shape:
Hint
You need the digit twice: as a character to print, and as a number of rounds. Copy it, as in Lesson 7.
Hint
Take 48 off the copy and add 1: for the digit 3 there are four characters to print.
Hint
With the character in cell 0 and the counter in cell 1, standing on cell 1, the loop is: go left, print, minus, go right, minus.
Notice that you wrote about fifty commands from one sentence: plan the cells, build a part, test it, build the next.
Nothing larger works differently. A brainfuck program that chats is the same thing, hundreds of millions of commands long, and no person typed those: a program wrote them, part by part, with an agreement about the cells.
Check yourself
A free exercise
Change your countdown so that it ends with an exclamation mark. Or make it count up, from 0 to the digit. Nothing is checked here:
What you can do now
You can write a small program from a description: plan the cells, build it in parts, test each part, and try the edges.
The programs of A plan in words and On your own come back as the challenges That many stars and Count down from, where your program is also measured: how many commands, how many steps.