As this writing is a collection of some programs that have not been documented before now, this page will be gradually updated and refined whenever I recall more and more of my ideas concerning the topic and feel the want. I will perhaps add more such programs, eventually and as they're written.
This page exists to showcase my APL programs and explain how they work. Most of the APL I write is for solving simple mathematical problems or other such things in a single line of code; rather than document a single APL program, I decided to collect some of the APL I've written and document that. The APL language is fun, given its very concise and carefree nature; all or most loops are implicit, memory usage and storage exhaustion aren't a concern, and programs are written purely for their results, rather than caring precisely how that result is arrived at or how it is transformed. APL is proof that good notation goes a long way.
This article contains code fragments, some of which are under the GNU Affero General Public License version three. These programs are all licensed under the GNU Affero General Public License version three.
I will firstly be documenting pieces of my Masturbation interpreter written in APL, relating to my 2017-02-02 article. The first fragment is the searching routine, for brackets:
⍝ This is a searching routine for [ and ]; it returns the index of the corresponding bracket. ∇ z←search x;⎕IO ⎕IO←0◊z←0⍳⍨+\(-x=convert'[')+x=convert']' ∇
convert function merely coerces the character argument into an integer, as
x is an integer array. The
⎕IO is set to zero for zero-based-indexing. So, using boolean = and unary negation, the
x array is used to produce two arrays where the open brackets are negative one and the closing brackets are one and the two are then summed to provide a single array consisting of negative one, zero, and one. This array is then scanned over to produce sums of the elements at each step and then the location of the first zero is located to complete the routine. The execution looks as so, with two nested brackets and some characters inside of them:
¯1 0 ¯1 0 0 1 0 1 ⍝ This is the joined array, reducing each element to its integer. ¯1 ¯1 ¯2 ¯2 ¯2 ¯1 ¯1 0 ⍝ This is the scanned array, see how the nesting is shown. ^ ⍝ This is the first zero, which is past the outer nesting for this.
The next fragment is a predicate for determining if the program uses brackets properly, meaning in a balanced and ordered fashion:
∇ z←balanced x;⎕IO;t ⍝ This predicate determines whether [ and ] are balanced properly or not. ⎕IO←0◊t←0=+/(-x=convert'[')+x=convert']'◊z←t∧~∨/1=+\(-x=convert'[')+x=convert']' ∇
Note that APL executes right-to-left, but pieces separated by the diamond are executed left-to-right. The
⎕IO is set to zero here, as well, for zero-based indexing. To simplify the predicate, this executes in two parts, with the first merely performing the same trick as the
search function, but reducing addition across instead of scanning and so leading to a single sum, which is then compared to zero. Another similar process is then used to scan addition across the same array, seek out any ones, reduce that to a single value with an or, and then see if the previous test was true whereas this test was false; if a single one was found, that indicates a closing bracket was used before a corresponding opening bracket was, which is incorrect. The execution looks as so, with an improperly nested example this time:
¯1 0 ¯1 0 1 1 0 1 ⍝ This is the joined array, reducing each element to its integer. 1 ⍝ This is the sum thereof, it's not zero, so it's not balanced. ¯1 ¯1 ¯2 ¯2 ¯1 0 0 1 ⍝ This is the scanned array, notice it doesn't end with zero. 0 0 0 0 0 0 0 1 ⍝ These are the elements that equal one. 1 ⍝ This is the result of reducing or thereof; it's negation is zero, false.
This is a simple line that produces ever more accurate values of e for larger values of n:
All this does is generate an array of integers beginning from
⎕IO, adjust it so that it begins from zero, obtain the factorial of each integer, have each factorial replaced by its reciprocal, and then reduce this to a single sum.
This is rather recent, as of writing, and solves a trivial math problem:
This solves the problem of, given an array of N distinct numbers from zero to N, determining which lone number is missing from the array. A solution can be given in linear time and constant space. The solution is trivial, as it can be determined what the sum should be if all numbers were present, and so that is arrived at and then the difference with the sum of the array given is found and that is the answer. The execution looks as so, for N being 10:
0 6 1 5 3 2 9 7 8 ⍝ This is an example array missing four. 41 ⍝ This is the sum thereof. 1 2 3 4 5 6 7 8 9 ⍝ This is the complete array. 45 ⍝ This is the sum thereof. 4 ⍝ This is the difference of the sums, four, the answer.