MPath
(First posted on December 17th, 2021)
MPath, pronounced "em-path", is an esoteric math language that I came up with that uses prefix notation.
The language is similar to the programming language LISP because of the prefix notation and quoting.
It can also be more easily compiled because the types and function arities are known at compile-time.
Syntax and Values
Spaces and commas are treated as whitespace, which means they can indicate the end of a number or name.
Most symbols are a single character, but user-defined variable names can be up to 2 characters long, and label names can be up to 5 characters long.
The restrictions on the name lengths is to decrease ambiguity.
Below are the
-
numbers:
- decimal
regex: "[1..9][0..9]+"
example: 3289
- binary
regex: "0b[01]+"
example: 0b10101
- octal
regex: "0o[0-7]+"
example: 0o71
- hexadecimal
regex: "0x[0-9a-fA-F]+"
example: 0xbeef
-
other literal values:
- ∞ : infinity
- π : pi
- ε : Euler's number
- ∅ : null (same as undefined, invalid, or not a number)
- variable names
regex: "[a-zA-Z][a-zA-Z0-9]?"
Can be 1 or 2 characters long
- label names
regex "[a-zA-Z][a-zA-Z0-9]{2,4}"
Can be 3 to 5 characters long. They start with a letter and continue with any combination of letters or numbers
- [x, y, ... z]
list of numbers; commas between elements are allowed but they are optional
- (double) quoting a string literal
“...”
Input and output operations
These are all that you could want from a calculator
- ⧁ x
display x, which may be a variable, list, or a quoted string message
- ⧀
get numeric input
Special operations
- → X Y
let X have the same value that Y currently has, where X and Y must either both be variables or both be lists
- № L
number of elements in list L; special because it can be assigned to a positive integer to change the length
- @ x L
get index x of list L (first element is index 0); this is special because the index value can be assigned to any value in order to update the contents of the list
- ⌒ L1 L2
create a new list which has the contents of L2 concatenated onto the end of L1
- ‥ x y L
get a sub-list of the list L from index x to index y
- Ł x
create a list with x elements (x must be a positive integer); elements are initially undefined
- L x y
create a list with x elements initially set to y (x must be a positive integer)
- _ L
analogous to Python's splat operator; it “unpacks” the elements from the list L, which helps with passing the elements of a list as arguments
- {c1 ↦ r1, c2 ↦ r2, ... cn ↦ rn}
condition; see the conditions section below
- : label
label; mark the next line or next expression as a place that can be reached via a goto, must be outside of expressions
- ⇑ label
goto; make current execution flow jump up to the label named x
- ⇓ label
goto; make current execution flow jump down to the label named x
- #<declaration...>
make a special declaration about semantics
- #first index is 0
(default setting) use 0-based list indexing, so that the first element of a list is at index 0
- #first index is 1
use 1-based list indexing, so that the first element of a list is at index 1
Conditions
Conditions are another type of special operation. They allow for conditional calculation or execution. Conditions are written with curly braces with any number of sub-expression elements. Each sub-expression consists of a condition mapping to a result. The expression evaluates each of the sub-expressions cases ‘c’ until one of them evaluates to non-zero. If a case evaluates to non-zero then the whole expression evaluates to the result of the corresponding sub-expression result ‘r’, but if no case was satisfied then the result is null.
Unary functions
- - x
negative x (unary minus)
- | x
absolute value of x
- ⌊ x
the floor of x
- ⌈ x
the ceiling of x
- √ x
the square root of x
- ln x
the natural logarithm of x
- Σ L
sum; calculate the sum of numbers in list L
- Π L
product; calculate the product of numbers in list L
- Μ(capital Mu) L
max; choose the maximum value in list L
- μ L
min; choose the minimum value in list L
Binary functions
- = x y
equality; 1 if x and y are equal, otherwise 0
- ≠ x y
inequality; 1 if x and y are equal, otherwise 0
- > x y
comparison; 1 if x is greater than y, otherwise 0
- < x y
comparison; 1 if x is less than y, otherwise 0
- ≥ x y
comparison; 1 if x is greater than or equal to y, otherwise 0
- ≤ x y
comparison; 1 if x is less than or equal to y, otherwise 0
- + x y
addition; add x and y
- – x y
subtraction; subtract y from x
- * x y
multiplication; multiply x by y
- ÷ x y
division; divide x by y
- % x y
modulo; remainder of dividing x by y
- ^ x y
exponential; calculate x to the power of y
- log x y
logarithm; the log, base x, of y
- ∙ L M
dot product of list L and list M
- ⨯ L M
cross product of list L and list M
Ternary functions
- ≈ x y e
almost-equality; 1 if the difference between x and y is less than or equal to e, otherwise 0
Dealing with null and infinity
Variables can be compared with null and infinity and make decisions based on the result of the comparison.
This page was last updated on July 29th 2022.