esolang

Recur

No calls and no call stack. You keep the call stack yourself, on the only stack there is.

D, 2023 · Turing complete: yes · ●●○ moderate · esolangs.org

Tested with 4 known programs · Test method

Run it

Expected: 4

The idea

D made Recur in 2023, after a question on Computer Science Stack Exchange that asked whether iteration can replace recursion. The language has no calls. It has natural numbers on a stack, numbered variables, loops that never end by themselves and two ways to break out of one. A recursive function such as Ackermann's keeps its pending calls on the stack by hand.

Read the story

There is no subtraction either. The predecessor counts up from 1 until it meets its argument.

The machine

CommandDoes
123Push the number
sAdd 1 to the top
,Pop v and a, store a in variable v; leave the loop if there is no a
!Pop v, push variable v
[ ... ]Repeat forever
=Pop two numbers, leave the loop if they are equal

A command with too few numbers on the stack does nothing. The specification has no input or output. The numbers of the input start on the stack, and the run prints the stack it ends with.

Example

The predecessor stores its argument in variable 0 and 1 in variable 2.
Expected: 14

The predecessor stores its argument in variable 0 and 1 in variable 2. Each turn it adds 1 to the number on the stack and to variable 2, and it leaves when variable 2 equals variable 0. The stack is then one behind. On 0 it never stops. The doubling adds 2 to the stack for every 1 it counts.

Patterns it teaches

  • Stack machines: the stack doubles as the call stack.
  • Counter machines: add 1 and compare are all there is, so even the predecessor has to count.
  • Turing completeness: thespecification translates brainfuck into Recur, with the tape kept in variables.

Further

Stack-based

Write your own in Run Convert a program from Recur

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run recur -e '0,0 1 2,[2!0!=s2!s2,]' --input '5'
shib manual recur

Source

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

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

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