LearnBuild toward a chatbotThe machine
The machine
Aim. At the end of this lesson you can write a program that puts any number in any cell. You need nothing but eight characters.
Where the lessons are going
Every lesson adds one part to a single small sum. In lesson 11 one program does the whole sum and prints a word. This lesson adds the first part: getting a number, such as the 90 the sum starts with, into a cell.
A row of boxes and a pointer
Try it: a row of six boxes, a mark on one of them, and four buttons. The buttons call the mark the finger. Use them to put the number 3 in the box called "cell 1":
What you just saw
This is the whole machine: a row of boxes that each hold a number, and a mark on one of them. You can move the mark one box, and you can add one to the number or take one off. Nothing else.
The row is called the tape, a box is a cell, and the mark is the pointer. Cells are counted from 0. Each button is a command, written as one character: < > + −. The line that grew under the buttons is a program.
A program runs by itself
Try it: the same row, now with a program already typed. Press Step until the program has ended. Before each press, read the line that starts with "Next":
What you just saw
The machine reads a program from left to right, one command at a time. The command it is about to do is lit up. Nobody presses buttons any more: the program is the list of presses.
This one left 2 in cell 0 and 3 in cell 1, and the pointer on cell 1. The tape of the lesson has six cells; the real one has millions.
Write a number
Try it: type a program that leaves 12 in cell 2. You only need > and +:
What you just saw
You typed twelve plus signs. Brainfuck has no way to write "12": a number is that many plus signs.
The sum these lessons build toward starts with the number 90. Written like this, it takes ninety plus signs.
Doing it again and again
Ninety plus signs is a lot of typing. This program repeats a few commands instead. It starts with a number in the left cell and ends with a number in the right one. Change what the left cell starts at and what is added each round, then press Play, or drag Step back and forth:
Every round took 1 from the left cell and added the same amount to the right cell, until the left cell was empty. With 3 and 4 that is three rounds, and the right cell ends at 12. Set the left cell to 0 and nothing is added at all. The sliders stop at 9 to keep the run short.
The square brackets are what make it repeat. They mean: repeat what stands between us until the cell under the pointer is zero. Such a repeat is called a loop.
Try it: this is the loop with 3 and 4. Press Step until the program has ended, and watch what the two brackets ask:
What you just saw
Each time the pointer met a bracket, the bracket asked: is this cell zero? The first bracket let the program in because cell 0 held 3. The second one sent it back to the first until cell 0 was empty. Three rounds took 1 from cell 0 and put 4 into cell 1.
"Is this cell zero?" is the only question a brainfuck program can ask. Remember that. It comes back in lesson 4.
The same number, one cell further
Try it: this is the loop program of step 4. It leaves 12 in cell 1. Change it so that the 12 lands in cell 2. You may use 13 commands at most:
Hint
Every command says "one to the right" or "one to the left", never "cell 1". What happens if the pointer starts one cell further?
What you just saw
One > in front, and the whole program does its work one cell further on. A program never says where it is. It only says left and right. That turns out to be very useful in step 2 of lesson 7.
The loop also made the program shorter: 13 commands in place of the 14 you typed in step 3. For large numbers a loop saves much more.
Letters come out
Numbers can come out as letters. This program builds a number in cell 1 with a loop and then prints it. Change the number of rounds and read the line "Printed":
The last command, . (a full stop), printed the cell as one letter. A computer keeps every letter as a number, and each round here adds 8: eight rounds make 64, one more plus makes 65, and 65 is the number for A. Nine rounds give 73 and the letter I.
Try it: with eight rounds. Press "To the end". What does the program print?
What you just saw
Cell 1 reached 65 and the seventh command turned it into the letter A.
The eighth command, , (a comma), does the opposite: it takes the next letter that was typed and puts its number in the cell. That is how the chat program gets your question. Those are all eight commands; the table at the end of this page lists them.
Below zero
A cell holds a whole number of a fixed size, and the size matters. This program only takes 1 from a cell that holds 0. Choose how many bits a cell has, and read what it holds afterwards:
The cell did not go to −1. It went to the highest number it can hold: 255 for 8 bits, 65,535 for 16 and 4,294,967,295 for 32. After the highest number comes 0 again, and before 0 comes the highest number, like the mileage counter of a car.
Try it: a cell of 8 bits holds 0. Press Step once to take 1 off:
What you just saw
Not −1, but 255. The cells of these lessons mostly hold up to 255; the cells of the chat program hold up to 4,294,967,295. This going round looks like a nuisance. In step 1 of lesson 5 it is the key to everything.
What you can do now
Put any number in any cell, repeat something with a loop, and print a letter. For the sum these lessons build toward, that is the first part: the 90 can stand in a cell.
The eight commands in one table
| Command | What it does |
|---|---|
+ − | Add 1 to the cell under the pointer, or take 1 off. |
> < | Move the pointer one cell to the right or to the left. |
[ ] | Repeat everything between them until the cell under the pointer is zero. |
, | Take one letter of input and put its number in the cell. |
. | Print the cell as one letter. |
On your keyboard the minus is the ordinary hyphen key. Anything in a program that is not one of these characters is skipped.
A cell that goes up to 255 has 8 bits; one that goes up to 4,294,967,295 has 32 bits. Step 1 of lesson 5 shows what a bit is.
This lesson shows the machine with six cells. The tape of the chat program has 188,012,789 of them.