Lesson 17
Rules that change while running
[ name ] [ body ] DEFINE binds a name while the program runs. A lowercase name becomes a word; anything else becomes a reading rule, and an existing rule with that pattern is rebound.
The lesson
The main program was read before it ran, so new rules take effect only in text read later, the text you CALL. The language rewrites itself as it goes.
The kernel reference in the Docs lists every instruction. The example's Step button runs it in the Studio one instruction at a time.
Example
Output
Exercise 1
Define a rule . that copies one input byte, then build a text of as many dots as there are input bytes and CALL it.
Run tries your program on the input below. Check runs it on every test.
| Input | Expected output |
|---|---|
hello | hello |
dots | dots |
(empty) | (nothing) |
Output
A hint
LEFT TIMES [ . ] CAT END grows the text; then CALL.
One answer
Answer: Define a rule . that copies one input byte, then build a text of as many dots as there are input bytes and CALL it.
Expected output
hello
| Input | Expected output |
|---|---|
hello | hello |
dots | dots |
(empty) | (nothing) |
Output
Exercise 2
Print aabb by calling the text ++ twice, rebinding + in between.
Run tries your program on the input below. Check runs it on every test.
| Input | Expected output |
|---|---|
(empty) | aabb |
Output
A hint
DEFINE +, CALL, DEFINE + again, CALL again.
One answer
Answer: Print aabb by calling the text ++ twice, rebinding + in between.
Expected output
aabb
| Input | Expected output |
|---|---|
(empty) | aabb |
Output