esolang

rrreplace

Everything is a string, and every expression is a substitution in braces. To call a function you sometimes have to build the call as text first.

Turing complete: unknown · ●●● hard · esolangs.org

Tested with 15 known programs · Test method

Run it

Expected: Hello world!

The idea

rrreplace uses string substitution in a functional way. A program is a list of functions, one a line, as name=expression, and the function with the empty name is the main one. Every value is text, numbers too. To add 3 and 4 you repeat a 3 times and b 4 times, join them and count the characters.

Read the story

Every expression is in braces, and its first character is the instruction. Its arguments are split at :. A result is always plain text, so a colon inside it never splits arguments again. When it should, & takes a string, puts braces around it and runs it, and that is how a list built at run time becomes the arguments of a call.

The machine

ExpressionGives
{$0} {$1:}A parameter, or a range of them joined by :
{@f:a:b}The result of function f on a and b
{&text}The text run as an expression
{~s:split:join:start:stop}s split, sliced and joined again
{#s:keep}The characters of s that are also in keep
{^s:x:y}s with x replaced by y as long as x is in it
{*s:n} {*s}s repeated n times; the length of s
{?c:a:b}a if c is not empty, else b; only that one is worked out
{(} {)} {:}The characters {, } and : as text

In the main function the parameters are the words of the input. Indexes and slices count as Python's do.

Example

{@test} gives 1:2:3 as text, which is a single argument.
Expected: abcabc
Expected: aaxab

{@test} gives 1:2:3 as text, which is a single argument. So {@collapse:{@test}} calls collapse with one parameter and fails. Built as text and run with &, the colons split again.

Expected: 132

The sum adds the first two words of the input by length and leaves the others as they are.

Expected: 3:3:4

Patterns it teaches

  • String rewriting: split, join and replace are the only arithmetic.
  • Code as data: & runs a string as an expression, which is how a map builds one call for every element.

Further

Functional paradigm

Write your own in Run Convert a program from rrreplace

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: rrreplace.

Command line

shib run rrreplace -e '={$0}' --input 'hello'
shib manual rrreplace

Source

Module
langs/wiki/ports.js
Interpreter
langs/wiki/rrreplace.js

Docs: Kernel instructions · The .shib format · Command line · Adding a language

Source esolangs.org (CC0) · Edit on esolangs.org