Divzeros
One expression, evaluated over and over. The only way out of a loop is to divide by zero.
2007 · Turing complete: yes · ●●● hard · esolangs.org
Tested with 3 known programs · Test method
Run it
The idea
Divzeros is from 2007. A program is a list of functions, each a single expression, and a main expression at the end. There are no statements and no conditions. The main expression runs again and again, and so does every [x] inside it. Each run is an iteration with a number and a value, and # reads them back. A loop ends when it divides or takes a modulo by zero, or asks for an iteration that has not happened yet.
Read the story
Choices come from laziness. x*y does not look at y when x is 0, so 0*?'a prints nothing. The bit operators are INTERCAL's mingle $ and select ~, on integers of any size.
The machine
| Operator | Does |
|---|---|
?x ? | Print the character x and give x; read a character |
# #x | The iteration number; the value of iteration x-1 |
@ | The function's parameter |
<x >x _x !x | The left and right halves of an unmingle, minus, not |
* / % | Skip the right side when the left is 0; quit on a divisor of 0 |
+ - & ^ \| | Arithmetic and bits; & skips on 0 and \| on -1 |
$ ~ | Mingle and select, as in INTERCAL |
[x] | Run x as a loop until it quits; give its last value |
name=x; name(y) | Define a function; call it with y as its parameter |
Numbers are decimal, hex after a backtick, or 'c for a character. fn("ab") is short for fn('a)+fn('b).
Example
The Hello World adds up thirteen printed characters and divides the sum by zero, so the main expression…
The Hello World adds up thirteen printed characters and divides the sum by zero, so the main expression runs once.
A loop prints the digit for its iteration number and divides by 9 minus that number. At iteration 9 the divisor is 0 and the loop quits.
The Equal selects a number with itself. That packs all its one bits to the right, so the lowest bit is 1 for anything but 0.
The beer program counts its verses with # in the main expression and stops when NoMore prints its line and divides it by zero. The examples do not all agree on what #x counts. In this implementation ## is the iteration before this one, as the copy program uses it.
Patterns it teaches
- Hostile design: no if and no while, only laziness and a division by zero.
Further
See also
Declarative paradigm
Write your own in Run Convert a program from Divzeros
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: divzeros.
Command line
shib run divzeros -e '1/??' --input 'ab cd'