esolang
Learn · Ideas24 languages show it

Concatenative

Write two programs next to each other and you have composed them. No variables, no arguments, only a row of words.

●○○ easy · where to see it

The idea

In a concatenative language, putting programs side by side means "do this, then that". If A and B are programs, A B is the program that runs A, then B on whatever A left behind.

Read the story

That only works if every word takes the same kind of thing and gives the same kind of thing back. Usually that thing is a stack; each word takes a stack and returns a new one. So words never name their arguments; they find them on the stack.

Forth (Charles Moore, around 1970) works this way in practice. Joy (Manfred von Thun) made it a theory; a Joy program is a function, and concatenation of text is composition of functions.

How it works

Numbers push themselves, and words act on the stack.

Numbers push themselves, and words act on the stack.

2 3 + 4 *
2      [2]
3      [2 3]
+      [5]          pop 3 and 2, push 5
4      [5 4]
*      [20]

Defining a word is cutting and naming

Because there are no variables, any run of words can be cut out and given a name. Nothing inside it refers to anything outside.

3 dup *              [9]       dup copies the top
3 dup * dup *        [81]

: square  dup * ;    define it once
3 square square      [81]      the same program, shorter

The definition is pure text substitution. Replace square by dup * and you get the original back. Both versions leave [81].

Compare a language with variables. To pull y = x * x out of z = x * x + 1 you must decide what the new function's parameter is and rename. In a concatenative language there is nothing to decide.

Quotations

To pass code around, wrap it, as [dup *] in Joy or (…) in Underload. A quotation is pushed as data; another word runs it. That gives loops and conditionals without special syntax, and it is how Underload's quine works (see quine).

Step by step

Filter the digits, then fold from 0, adding each digit’s value.

map: upper-case the letters

Expected output

HELLO, WORLD 42!

Output


  • input reads all input into one list.
  • The quotation tests "is it from 97 (a) to 122 (z)?" and if so takes 32 away, which makes it upper case.
  • map runs it on every element and collects the results; EMIT prints the list.

filter: keep only digits

Expected output

987

Output


fold: sum the digits

Filter the digits, then fold from 0, adding each digit’s value. num turns 24 into its two characters.

Expected output

24

Output


fold: reverse

Start from the empty list and put each element in front of the accumulator.

Expected output

desserts

Output


Why it matters

A kernel without quotations cannot express map or fold, because its programs cannot hold code as a value. Quotations and CALL make them ordinary words. See code as data and Underload, whose stack holds nothing but code.

Try this

  1. Evaluate 4 dup dup * * by hand. What does dup dup * * compute?
  2. Write cube in terms of square.
  3. Hard: swap exchanges the top two. Using swap, dup and *, turn a stack [x y] into [x·x·y].

Seen in

  • Forth. The working programmer's concatenative language; you extend it by defining words with : and ;.
  • Joy. The purist version, with quotations as first-class values.
  • Underload. A concatenative tarpit whose only data is quoted programs.
  • FALSE. A tiny Forth-like stack language whose 1024-byte compiler inspired Brainfuck's 240-byte one (esolangs.org).
  • GolfScript. Concatenative and built for short programs.

Further

Where to see it