Firstreplace
Slashes by accident. Each pattern replaces its first match once, and then the program is done.
Abyxlrz · Turing complete: no · ●○○ easy · esolangs.org
Tested with 2 known programs · Test method
Run it
The idea
Abyxlrz was writing an interpreter for slashes in SmileBASIC and could not make it replace every match of a pattern. It replaced only the first one. That accident became Firstreplace.
Read the story
The program never rewrites itself. Its first line is a piece of text, and every pair of lines after it changes that text once. So every program stops, after one step per pair.
The machine
| Line | Is |
|---|---|
| 1 | The initial state |
| 2, 4, 6 ... | A pattern |
| 3, 5, 7 ... | Its replacement |
Each pair replaces the first place its pattern occurs in the state, and a pattern that does not occur changes nothing. The output is the state after the last pair. A pattern or replacement that is exactly [INPUT] becomes a line of the input.
Example
In slashes the first rule would never stop, as eses holds es again.
In slashes the first rule would never stop, as eses holds es again. In this implementation it is used once. The second program changes only the first of its three ab.
Patterns it teaches
- String rewriting: with each rule used once, nothing can loop.
Further
See also
Also in Rewriting
Write your own in Run Convert a program from Firstreplace
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: firstreplace.
Command line
shib run firstreplace -e 'Test
es
eses'shib manual firstreplaceSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/firstreplace.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org