esolang

Unarian

Every function maps one natural number to another, or fails. There is +1, there is -1, and that is all.

Crb233, 2021 · Turing complete: yes · ●●○ moderate · esolangs.org

Tested with 8 known programs · Test method

Run it

Expected: 1

The idea

Crb233 made Unarian in 2021. A program holds a single natural number, and every function is a partial function on it. The two built-ins are + and -, and - fails on 0. New functions come from two ways of combining old ones. Writing them side by side runs them one after the other. Writing | between them tries them in turn until one does not fail.

Read the story

Failure is the only test there is. - 0 | + checks for zero by trying to take 1 away. Recursion does the looping. To keep more than one number around, a program packs them into the digits of the one it has; the Fibonacci keeps three registers in the bits of octal digits.

The machine

CodeDoes
+Add 1
-Take 1, and fail on 0
f gRun f, then g on its result; fail if either fails
f \| gRun f; if it fails, run g on the same input
{ ... }Group
name { ... }Define a function; main runs first
#Comment to the end of the line

| binds looser than writing side by side, and an empty alternative passes its input through. The first number in the input goes to main, and the result is printed in decimal, or - for a failure.

Example

The three-function program.

The three-function program. 0 counts down to 0, if=0 fails unless its input is 0, and main gives 1 for 0 and 0 for everything else.

Doubling by recursion. Take one, double the rest, add two; on 0 the empty alternative stops it.

Expected: 42

The Collatz counter, without its comments. From 7 the sequence takes 16 steps to reach 1.

Expected: 16

Everything is counted in ones, so it is slow. The Collatz count from 7 takes 2,232 evaluation steps, and the Fibonacci needs 253,543 steps to find that f(5) is 5.

Patterns it teaches

Further

Declarative paradigm

Functional paradigm

Write your own in Run Convert a program from Unarian

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run unarian -e '0 { - 0 | }
if=0 { { - 0 | + } - }
main { if=0 + | 0 }' --input '0'
shib manual unarian

Source

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

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

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