Algebraic Programming Language
- Not to be confused with APL (A Programming Language).
Algebraic Programming Language or (APL for short) is an esolang by User:PythonshellDebugwindow based on algebra.
Syntax
Every APL program is a series of algebraic expressions and equations. Number literals are in base-10 and match the regex [0-9]+(\.[0-9]+)?
.
Operators
Builtins
Operator | Operation |
---|---|
-A | Negation |
A + B | Addition |
A - B | Subtraction |
A * B | Multiplication |
A / B | Division |
A % B | Modulo |
A ** B | Exponentiation |
A & B | Short-circuit logical AND (false -> 0 or 0.0) |
A | B | Short-circuit logical OR (false -> 0 or 0.0) |
$A | Return |
(A) | Grouping (not really an operator) |
Standard order of operations applies.
Multiplication shorthand
Multiplication is implied, so this:
a * b
is equal to this:
ab
where a and b are both variables. This code, however:
1(2)
will raise an error, as bracket-multiplication notation is invalid syntax. The multiplication dot ·
is also unused (but can be used in custom operators).
Custom operators
You can define custom operators in APL. They have the highest precedence except for ()
brackets and functions (see #Functions). Here is an operator that you call like a ~ b
that finds the mean of two numbers:
a ~ b = (a + b) / 2
One called like x@
that doubles a number:
a@ = a * 2
This is valid:
^a^b^c^ = a + b + c
So is this:
~a`b``c~ = (a / b) % c
a
and b
can be any lowercase Latin (including accents), Cyrillic, or Greek letters, and operator symbols can be any non-alphanumeric (in Latin, accented Latin, Cyrillic, or Greek scripts) and non-+-*/%&|()={}$
symbol.
Functions
Functions are an alternative to operators, with the same effect (but they can be more readable). No functions exist by default, but you can create one like so:
F() = 123
With an argument:
F(x) = x * 2
With two arguments:
F(x, y) = x + y
And so on. x
and y
here are arguments. Arguments can be any lowercase Latin (including accents), Cyrillic, or Greek letters, and function names can be any sequence of uppercase Latin (including accents), Cyrillic, and Greek letters.
Multiline functions and operators
Multiline functions and operators can be made by using {}
:
MULTILINE() = { 123 456 }
When MULTILINE is called, 123 will printed and 456 will be returned. The last evaluated statement will be returned, unless $
(the return operator) is used:
MULTILINE() = { $123 456 }
Now when MULTILINE is called, 123 will be returned and nothing printed. $ can also be used as the RHS of &
or |
:
!x = { x & $0 $1 }
This function to compute the logical complement ("not") of x returns 0 if x is not equal to zero, otherwise it returns 1. x & $0
will never output x, because it has a conditional return.
Execution
Lines without =
are executed. Their result is printed to STDOUT. All variables on these lines (any lowercase Latin (including accents), Cyrillic, or Greek letters) are set initially to user input in the order they first appear in the line. So this:
a + b + d c + e + b
would ask the user to input a, b, d, c, and e, in that order.
If a line has 1 =
with just a variable name as LHS, that variable won't be input by the user, but will instead be set to RHS, so this:
n = 123 n
wouldn't take any user input.
Examples
Print the ASCII values for Hello, World!
72 101 108 108 111 44 32 87 111 114 108 100 33
Numeric cat
n
Truth-machine
x? = x & x? n?
Floor and ceiling functions
FLOOR(n) = n - n % 1 CEIL(n) = { n % 1 & $(n - n % 1 + 1) n }
While loop function (accepts condition and code functions as arguments)
WHILE(x, c) = x() & ((c() | 1) & WHILE(x, c))
If statement (accepts code function as argument)
IF(x, c) = x & c()
Computational class
APL is Turing-complete, as its variables are unbounded, and prime factorization can be used (along with the while-loop function described in the Examples section) for Turing-completeness.