Lesson 15
Lists and CALL in the kernel
[ ... ] pushes the text between the brackets as one value, a list of characters.
The lesson
EMIT prints a whole list, CAT joins two (a number joins as one character), SIZE counts and UNCONS splits off the first character.
Code is text. CALL reads a list as a program and runs it. That is all Underload's ^ is.
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
Print the input reversed.
Run tries your program on the input below. Check runs it on every test.
| Input | Expected output |
|---|---|
hello | olleh |
stressed | desserts |
a | a |
(empty) | (nothing) |
Output
A hint
Start with an empty list and put each byte in front with READ SWAP CAT.
One answer
Answer: Print the input reversed.
Expected output
olleh
| Input | Expected output |
|---|---|
hello | olleh |
stressed | desserts |
a | a |
(empty) | (nothing) |
Output
Exercise 2
Read a digit n and print ab n times, by calling the same text n times.
Run tries your program on the input below. Check runs it on every test.
| Input | Expected output |
|---|---|
3 | ababab |
0 | (nothing) |
7 | ababababababab |
Output
A hint
Keep the code on the stack; DUP CALL inside a TIMES loop.
One answer
Answer: Read a digit n and print ab n times, by calling the same text n times.
Expected output
ababab
| Input | Expected output |
|---|---|
3 | ababab |
0 | (nothing) |
7 | ababababababab |
Output