esolang

recs

Partial recursive functions and the lambda calculus in one expression, reduced by name.

Jan jelo, 2024 · Turing complete: yes · ●●● hard · esolangs.org

Tested with 2 known programs · Test method

Run it

Expected: (list 1 2 6 24 120 720 5040 40320 362880 3628800)

The idea

Jan jelo made recs in 2024. It puts the two classic models of computation side by side. The functions of recursion theory are there by their old names, Z, S, P, C, R and M. Next to them are lambdas, let, lists and a few arithmetic shortcuts. The whole program is one expression, and the interpreter prints what it reduces to.

Read the story

Arguments go in as written and are reduced only when used, which is call by name. So the specification builds its fixed point combinator from (lam x(x x)) and gets recursion without a loop. It holds a Python interpreter of 334 lines, and this implementation follows it.

The machine

FormReduces to
(Z x...) (S x...)0, and x+1
((P m n) x1 ... xm)xn
((C f g1 ... gk) x...)(f (g1 x...) ... (gk x...))
((R h g) x... n)Primitive recursion on n
((M f) x...)The least a with (f x... a) equal to 0
((lam x e) a)e with a put in for x
((fn e) a1 a2 ...)e with the reduced aN put in for #N
(let x1 e1 ... body)Nested lambdas
+ - * / √ = ifArithmetic on naturals, a test, a choice
list cons car cdrLists
pair left rightOne number for two, and back

A lambda takes only its first argument, and a function that is never applied prints as it is written.

Example

The program builds fix, the factorial and map, and maps (C fact S), the factorial of n+1, over 0 to 9.

The program builds fix, the factorial and map, and maps (C fact S), the factorial of n+1, over 0 to 9.

#2 belongs to the fn it is written in. The inner fn gets 3 and 4, so the answer is 4 and not 2.

Expected: 4

The pairing table, read row by column, and left and right taking 7 apart again. The specification's text swaps the two cases of right; its interpreter and its table agree with each other, and this implementation follows them.

Expected: (list 15 7 5 9 1 2)

Patterns it teaches

Further

Functional paradigm

Write your own in Run Convert a program from recs

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run recs -e '((fn((fn #2)3 4))1 2)'
shib manual recs

Source

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

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

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