esolang

Pair

Nil, pairs and functions, and nothing built in. Numbers, text and the state monad all come from a prelude.

Hakerh400, 2022 · Turing complete: yes · ●●○ moderate · esolangs.org

Tested with 18 known programs · Test method

Run it

Expected: Hello, World!

The idea

Hakerh400 made Pair in 2022. A value is nil, a pair or a function, and every value can be called. Calling a pair (a, b) with x calls x a b. Calling nil with x gives a function of two arguments that keeps the first if x is a pair and the second if x is nil. That one rule is the only test the language has, and if is built from it.

Read the story

There are no built in functions. A number n is a list of n nils, a character is the number of its code and a string is a list of characters. The specification gives a prelude of 228 definitions, from id and foldr up to integers, matrices and a state monad, and every program starts with it.

The machine

FormMeans
f a b = bodyA definition; calls are left associative
# () []Nil
(a, b, c)(a, (b, c))
[a, b](a, (b, #))
a \| b \| ca (b c)
5 'x' "ab"A list of nils, a character code, a list of codes
do x <- e returnSugar for bind, seq and pure

main is called with the input as a string, and the string it gives back is the output. Evaluation is lazy, so repeat '1' and nats are fine as long as only part of them is read.

Example

The primes.
Expected: desserts

The primes. nats is the infinite list of naturals, and take reads only what it needs.

Expected: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

The example of the state monad. The specification says it outputs the list [n, 1, 2, 3, 4].

Expected: [5, 1, 2, 3, 4]

The prelude's arithmetic and comparisons, such as add, div and eq, are marked in the specification as optimized. They run natively and give what their definitions give, so the 99 bottles of beer, the brainfuck and Deadfish interpreters and the digit of pi in the specification all run.

Patterns it teaches

  • Lambda encoding: numbers are lists of nils, and nil is the test.
  • Combinators: the prelude defines S, K, I and iota.
  • Quine: two lines that print themselves, given the prelude.

Further

Functional paradigm

Write your own in Run Convert a program from Pair

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run pair -e 'main = id' --input 'cat'
shib manual pair

Source

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

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

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