How do I define a custom function?
In Algosim, functions are first-class objects.
To create a function, you use the ↦ operator. You can insert this operator by pressing Ctrl+Shift+T or by typing “\mto” followed by space or a punctuation character.
Typically, you want to save the function to a variable. To do this, you use the usual assignment operator ≔. You can insert this operator by typing a colon (:) followed by an equals sign (=) and then space or a punctuation character.
Example:
f ≔ x ↦ x^2 + 1
custom function
f(5)
26
A function may depend on several variables:
Q ≔ (x, y, z) ↦ 3⋅x^2 + y^2 − z^2 + 2⋅x⋅y
custom function
Q(4, −2, 1)
35
If a function depends on more than a single variable, you may choose to pass a single list or vector of arguments instead of each argument separately:
P ≔ ❨4, −2, 1❩; Q(P)
35
Functions are first-class objects:
SequenceList(1, 10) @ (n ↦ prime(n))
2 3 5 7 11 13 17 19 23 29
filter(ans, (n ↦ even(sum(digits(n)))))
2 11 13 17 19