esolang

Cut

Two commands that count by their own place in the program. The only output is one number.

A, 2019 · Turing complete: no · ●○○ easy · esolangs.org

Tested with 6 known programs · Test method

Run it

Expected: 3

The idea

The esolangs.org user A made Cut in 2019 as a joke. A program has one accumulator that starts at 0 and is printed when the program ends. The first character counts by 1, the second by 2, the third by 3, and so on. A + adds its count and a - takes it away. There are no loops and no input, so every program stops.

Read the story

The specification calls writing the shortest program for a number an interesting puzzle. Its 3 is +-+-+, which is 1 - 2 + 3 - 4 + 5.

The machine

CharDoes
+Add its place in the program
-Subtract its place in the program
anything elseNothing, but it still takes a place

The specification has a C and a Python implementation, and they disagree on other characters. The C one subtracts for them; the Python one skips them. This implementation follows the Python one, because the quine needs it. It reads only the first line, and a ~ ends the program.

Example

The quine is the digit 0.
Expected: 4
Expected: 0

The quine is the digit 0. It is not a command, so the accumulator stays at 0 and the program prints itself.

With m characters the largest number is 1 + 2 + ... + m. A character that is not a command adds nothing but keeps its place, so every number from 0 up to that one fits in m characters, and that gives the shortest program. 2 is 0+, 3 is ++ and 4 is -++, all shorter than the specification's. 100 takes 14 characters.

Expected: 2
Expected: 3
Expected: 100

Patterns it teaches

  • Code golf: the shortest program for a number is a puzzle with a neat answer.

Further

Also in Jokes

Write your own in Run Convert a program from Cut

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run cut -e '+'
shib manual cut

Source

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

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

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