Lesson 6 — Papers that stay
One honest difference on this page: each Run starts with fresh, empty papers, so an example that reads a paper writes it first, inside the same program — in a program on your own machine, the papers stay between runs, just as the lesson says.
Step 1: writing and reading a file
Everything so far has lived and died inside one run of a program; when the program ends, its shelves are swept clean. But a program can also keep papers: files on the disk, which stay after the program ends. writefile(path, text) writes text to a file, replacing what was there. readfile(path) gives back a file's whole text:
The book says it answers:
Shalom, garden 14
Your turn. Write the text "Remember the milk" to a file called reminder.txt, read it back onto a shelf, and say the shelf.
The worked answer — try yours first, then peek
The book says it answers:
Remember the milk
Step 2: reading a file as bytes
There is a second reader, for files whose contents are not text at all. readbytes(path) reads a file as the bytes it is, into a block of bytes — the language's way of holding raw data, where each byte is a whole number from 0 to 255, reached by number like a list item:
The book says it answers:
6 83
The 83 is the byte that writes the letter "S". Hold that thought loosely — bytes matter most downstairs, in Lesson 10, and on feathers, in Lesson 9. Here, what matters is the shape of the idea: text is for reading, bytes are for exactness.
Your turn. Write "Hi" to a file, read it back as bytes, and say how many bytes there are and what the second byte is. (The byte for "i" is 105.)
The worked answer — try yours first, then peek
The book says it answers:
2 105
Step 3: a program that remembers
Now the real lesson: a program that remembers between runs. A guest book, which anyone running the program signs:
The book says it answers:
A brand-new guest book. The guest book holds 1 name(s). Ada
Walk it through. The program starts with an empty list, then tries to read the guest book from last time. That trying is attempt: it runs a stretch, and if a problem arises inside it — and a file that is not there is a problem — the if it fails stretch runs instead. On the very first run there is no book, so the program says so and carries on with the empty list. Then it signs the book with the first word it was run with (arguments() — Lesson 8 gives it its full story), writes the whole book back as one name per line, and reports.
Run it twice on your own machine: the second run finds the book, and Ada is joined by whoever signs next. The program has a memory now — not in itself, but in its papers. (This book's proof runs each example in a fresh folder, so in these pages the book is always new; the remembering happens on your machine, and watching it happen is the lesson.)
Your turn. Change one thing in your own copy: make the guest book's file called visitors.txt instead, and the fresh-start message say "A brand-new visitors book." Run it and check the first line.
The worked answer — try yours first, then peek
The book says it answers:
A brand-new visitors book. The visitors book holds 1 name(s). Ada
attempt gets a fuller telling in the next lesson — it is the language's answer to everything that can go wrong along the way. Here it did its quietest work: turning "there is nothing here yet" from a crash into a beginning.
The ledger grows: the book is written down
The ledger learns to keep its papers. For now the book is saved as one line per gift — who, a bar, the cents — and read back whole:
The book says it answers:
Feeding America|620 Barry Cunningham|500 Lines in the book: 2
Honesty requires a note: what came back is text. The names read fine, but the cents came back as the characters "620", not the number 620 — papers hold text, and text is all a file can promise. Teaching the program to read its own numbers back is the capstone's last trick, in Lesson 11. Until then, the book is written down, and that is already something: it survives the program that wrote it.
Your turn
Make a verse keeper. Write this line to a file called verse.txt — "Your word is a lamp to my feet, and a light for my path." — then read the file back and say what it holds.
The worked answer — try yours first, then peek
The book says it answers:
Your word is a lamp to my feet, and a light for my path.
You can now…
- write text to a file with
writefileand read a
file's whole text back with readfile;
- read a file as exact bytes with
readbytes; - try something that might not be there with
attempt … if it fails, and carry on kindly when it isn't;
- build a program that remembers between runs, in its
papers.
---