esolang

Unlambda

Functional programming with everything taken away: no variables, no names, no numbers. Just s, k, a backtick for "apply", and a function that prints.

David Madore, 1999 · Turing complete: yes · ●●● hard · esolangs.org

Tested with 4 examples and 1 known program · Hello World on Try It Online: identical · Test method

Run it

Hello World: Unlambda (subset)

Expected output

Hello, world!

Output


The idea

The lambda calculus builds every program from functions with named parameters. Combinatory logic showed, back in the 1920s, that you don't even need the names: two fixed functions, S and K, can be glued together into any computable function.

Read the story

David Madore took that result literally. Unlambda is the S and K combinators, a prefix operator for application, and a few extras so it can talk to the outside world. There is no way to write λx. anything. You have to do the "bracket abstraction" (compiling away the variables) in your head.

Then he added two features to make it harder: d, which delays evaluation, and c, call with current continuation, which lets a function grab "the rest of the program" and jump back into it later.

The machine

A program is one expression. `FG means "apply F to G". Evaluation is eager: F is evaluated, then G, then F is applied to G.

FunctionDoes
iIdentity: returns its argument
k``kxy = x (the second argument is thrown away)
s```sxyz = ``xz`yz (give z to both, apply one result to the other)
vSwallows any argument and returns itself
.xPrints the character x, returns its argument
rSame as . followed by a newline
d`dF is not evaluated; F is evaluated only when the result is applied
cCall/cc: applies its argument to the current continuation

Unlambda 2 added e (exit) and @, ?x, | for reading input.

Example

.H is a function that prints H and then behaves like i.

.H is a function that prints H and then behaves like i. So:

`r`.i`.Hi        prints "Hi" and a newline

Read it from the inside out: `.Hi prints H and returns i; `.i of that prints i; r prints the newline.

s duplicates its third argument, which is the seed of all repetition:

```s.H.ii        s .H .i i  =  `.H i  applied to  `.i i   prints "Hi"

And self-application makes a loop. Let W = ``s``s`k.*ii . Then `Wx prints * and becomes `xx . So `WW prints stars forever:

```s``s`k.*ii``s``s`k.*ii

Eager evaluation matters. In ``.a`.bii the argument `.bi runs first, so this prints ba.

Run them yourself.

Expected: stars until the run is stopped

Checked examples

Hello World: Unlambda (full)

Expected output

Hello, world!

Output


Hello World: Unlambda (full)

Expected output

Hello, World

Output


Hello World: Unlambda (full)

Expected output

Hello, World

Output


Quirks

  • Unlambda (subset) implements part of the language, as described above.
  • A run stops after 3,000,000 steps.

Try this

  1. Predict, then run: ```k.a.bi . Why is b never printed?
  2. Write a program that prints ab using s once.
  3. Hard: write a W with `Wx = `xx that prints nothing. Then explain why a naive JavaScript interpreter runs out of stack on `WW , while Madore's interpreter loops forever. (Hint: recursion in JavaScript.)

Patterns it teaches

  • Combinators: S and K are enough for everything; i is just ``skk .
  • Lambda encoding: numbers, booleans and lists all have to be built as functions.
  • Fixed points: the star loop above is W W, the same self-application trick that powers the Y combinator.
  • Turing tarpit: universal, and nearly impossible to write by hand.
  • Turing completeness: it contains the SKI calculus, so whether an expression halts is undecidable.

Further

Functional paradigm

Write your own in Run Convert a program from Unlambda

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run unlambda -e '`.!`.d`.l`.r`.o`.w`. `.,`.o`.l`.l`.e`.Hi'
shib manual unlambda

Source

Module
langs/classic.js

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

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