match
A function is a list of patterns. The first one that fits says what to do.
Japi, 2024 · Turing complete: yes · ●●○ moderate · esolangs.org
Tested with 3 known programs · Test method
Run it
The idea
Japi made match in 2024. Every function takes one argument and is written as branches, a pattern on the left of -> and an expression on the right. A call tries the branches in order and runs the first whose pattern fits. There are no loops and no variables to change, so repetition is recursion.
Read the story
Since there is only one argument, more arguments travel as a pair or a list. The comma makes a pair and groups to the right, so 1, 2, 3, nil is a list of three, and a string is a list of characters.
The machine
| Part | Does |
|---|---|
name: p -> e; ...;; | A function, each branch ending in ; and the last in ;; |
p: guard -> e | A branch that also needs the guard to be true |
_ x 1 'a' nil x, xs | Patterns for anything, a name, literals and a pair |
+ - * / % < > = | Arithmetic and comparison |
a, b | A pair |
a then b | Run a, give b |
f(x) | Call, with the commas inside making one list |
print, println, readln and error are built in, and the program starts by calling main. Numbers are 64-bit floats and print the way the Rust interpreter prints them, so 10 and not 10.0. include reads another file, and is left out because a run has no other files.
Example
The sum of a list from the specification.
The sum of a list from the specification. An empty list is 0, and otherwise the head is added to the sum of the tail.
The truth machine matches the line it reads as a list of characters. On 1 it would print 1 and call itself forever.
filter keeps what a function passed with the list says yes to. With its even and the sum above it adds 2, 4 and 6.
Further
See also
Declarative paradigm
Functional paradigm
Write your own in Run Convert a program from match
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: match.
Command line
shib run match -e 'main: _ -> println("Hello, world!");;'shib manual matchSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/match.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org