Tetra
Nothing but parentheses. A group that starts with an empty group splits in two, and the output is how many groups are left.
Hakerh400, 2022 · Turing complete: no · ●●○ moderate · esolangs.org
Tested with 4 known programs · Test method
Run it
The idea
Hakerh400 made Tetra in 2022. A program is a list of groups, and a group is a list of groups in parentheses. There is one rule. Find the first group, leftmost and outermost, whose first element is an empty group (). Replace it by two copies of itself with that empty group taken out. Repeat until no group starts with (). Then every group left is empty, and the output is how many there are.
Read the story
Every program stops, so Tetra is total. It is not small though. Each split doubles what is inside, and six nested pairs (((((()))))) end as 2 to the power 65,536 empty groups, a number with 19,729 digits.
The machine
| Text | Is |
|---|---|
() | An empty group |
(A) | A group that holds the list A |
AB | The list A followed by the list B |
The specification gives a short Haskell program that counts without rewriting. A list is worth the sum of 2 to the worth of each group's contents, and an empty list is worth 0. So () is worth 1, (()) is worth 2 and ((())) is worth 4. This implementation counts the same way and prints the number with a line feed, as Haskell's print does. Characters other than parentheses are ignored.
Example
The example from the specification, rewritten line by line.
The example from the specification, rewritten line by line. It takes 43 steps.
Inside, () is worth 1 and (()()) is worth 2 to the power 2, which is 4. Together the contents are worth 5, so the outer group gives 2 to the power 5.
The last line is another way to write 32. One group for 2 to the power 5, and inside it a group for 4 and a group for 1.
Patterns it teaches
- String rewriting: one rule, applied until it no longer fits.
Further
See also
String-rewriting paradigm
Write your own in Run Convert a program from Tetra
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: tetra.
Command line
shib run tetra -e '(())'shib manual tetraSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/tetra.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org