3 ms·
Trying to pick up some APL over the last year, I've found it mildly annoying to find APL code without explanation. I'm expecting that's fine for experienced use
by jodrellblank 7y ago
Trying to pick up some APL over the last year, I've found it mildly annoying to find APL code without explanation. I'm expecting that's fine for experienced users, even trying to work it out is useful, but it's hard to get to a point of being able to do that without some hints.
This fib code in the parent comment translates to:
def fib(n):
if (1 >= n):
return n
else:
return fib(n-1) + fib(n - 2)
and it does so with:
fib ← { "scriptblock" function declaration
implicit variable name ⍵ (omega) for the right side argument
1≥⍵ : ⍵ guard case, if 1>=⍵, return ⍵
⋄ statement separator, like ; in C-languages
if no guard clauses matched, remaining code runs
∇ recursive call to the function it appears in
∇(⍵-1)+∇⍵-2 fib (⍵-1) + fib (⍵-2)
}
More notably, the kind of tacit programming support looks like this:
(××∘⌊|+1≤2||) N Rounding to nearest even number (favouring away from 0)
which evaluates to a function that takes N on the right, and the tacit train looks like this, from TryAPL.org visualization:
┌─┼────┐
× ∘ ┌─┼───┐
┌┴┐ | + ┌─┼───┐
× ⌊ 1 ≤ ┌─┼─┐
2 | | <- N goes in here
The tree evaluates from the lower right. Each triple is a fork, N goes into the right prong, the left prong, then the result of both of those goes into the center prong. Unless there's a constant on the left prong, then that's used as-is. So:
|N for the magnitude; 2|(that) for the remainder of dividing by 2; 1<=(that) to find whether to add 0 or 1 for the rounding, (|N)+(that) to re-do the magnitude of N and add the 0 or 1. xN to get the direction +1 or -1, multiplied by the floor of the calculated result.
N enters in several places, which avoids having to store temporary variables and name them, when you aren't actually interested in new variables.
And this works on a single number, a vector or multi-dimensional matrix of numbers.