Iverson style array programming
- https://aplwiki.com/wiki/Iverson_notation
- Kenneth Iverson's notations
- APL grew out of this idea
—
- monadic: unary
- dyadic: binary
Remarks:
- Understanding the concept of ranks is very important in using APL.
- Array is (more?) to APL as table is to lua.
- APL evaluates expressions from right to left
*
is exponentiation. Multiplication is×
General
- Comment: ⍝
- Indexing starts from index origin (
IO
)
Operators
Reshape / shape (ρ )
https://aplwiki.com/wiki/Reshape
Two forms:
- Dyadic: Reshape an array.
- Monadic: Get shape of array
Dyadic examples:
> 2 4 ρ ⍳ 8
1 2 3 4
5 6 7 8
> 3 2 ρ ⍳ 6
1 2
3 4
5 6
> ⍝ Looks like truncation happens if shape doesn't fit
> 3 2 ρ ⍳ 8
1 2
3 4
5 6
Monadic example:
> ρ 2 4 ρ ⍳ 8
2 4
Index generator (⍳)
https://aplwiki.com/wiki/Index_Generator
> ⍳ 9
1 2 3 4 5 6 7 8 9
> ⍝ Set index origin so that ⍳ starts from 0
> ⎕IO ← 0
> ⍳ 9
0 1 2 3 4 5 6 7 8
Reverse along first axis (⊖)
> ⊖ ⍳ 5
5 4 3 2 1
> ⍝ Rows got reversed
> ⊖ 3 2 ρ ⍳ 6
5 6
3 4
1 2
Reverse along last axis (⌽)
> ⌽ 3 2 ρ ⍳ 6
2 1
4 3
6 5
> 3 2 ρ ⍳ 6
1 2
3 4
5 6
> ⍝ Columns got reversed
> ⌽ 3 3 ρ ⍳ 9
3 2 1
6 5 4
9 8 7
Transpose (⍉)
> ⍝ Columns got reversed
> ⍉ 2 3 ρ ⍳ 6
1 4
2 5
3 6
Drop (↓)
Drop first n elements of an array.
> 1 ↓ 1 2 3 4 5 6 7
2 3 4 5 6 7
> 2 ↓ 1 2 3 4 5 6 7
3 4 5 6 7
Take (↑)
Take first n elements of an array.
> 2 ↑ 1 2 3 4 5 6 7
1 2
> 4 ↑ 1 2 3 4 5 6 7
1 2 3 4
Replicate (/)
- Duplication.
- A dyadic operator.
> 2 / 3 4 5
3 3 4 4 5 5
> 1 / 3 4 5
3 4 5
> 3 / 3 4 5
3 3 3 4 4 4 5 5 5
> 2 / (3 4 5) (6 7)
3 4 5 3 4 5 6 7 6 7
Reduce along first axis (⌿)
> 2 4 ρ ι 8
1 2 3 4
5 6 7 8
> + ⌿ 2 4 ρ ι 8
6 8 10 12
First axis here is column-wise ??
Reduce along last axis (/)
> 2 4 ρ ι 8
1 2 3 4
5 6 7 8
> + / 2 4 ρ ι 8
10 26
Scan along first and last axis (⍀ and \)
> 2 4 ρ ι 8
1 2 3 4
5 6 7 8
> + \ 2 4 ρ ι 8
1 3 6 10
5 11 18 26
> + ⍀ 2 4 ρ ι 8
1 2 3 4
6 8 10 12
Factorial (!)
> !5
120
—
For an array, it's a map to all elements:
> 2 4 ρ ι 8
1 2 3 4
5 6 7 8
> ! 2 4 ρ ι 8
1 2 6 24
120 720 5040 40320
Depth (≡)
Nesting depth.
> ≡ 2 4 ρ ι 8
1
> ≡ ι 8
1
Roll (?)
? n
selects a number randomly from the first n numbers.
> ? 100
97
> ? 100
76
> ? 100
82
> ? 100
92
n must be greater than 0.
> ? 0
DOMAIN ERROR
?0
^
Ceiling and floor (⌈ and ⌊)
Both are monadic.
> ⌈ 3.2
4
> ⌈ 3.5
4
> ⌈ 3
3
> ⌊ 3
3
> ⌊ 3.5
3
> ⌊ 2.9
2
Exponential (*)
> ⍳ 8
1 2 3 4 5 6 7 8
> 2 * ⍳ 8
2 4 8 16 32 64 128 256
Not yet sure how this came about though:
> * ⍳ 8
2.718281828 7.389056099 20.08553692 54.59815003 148.4131591 403.4287935
1096.633158 2980.957987
Axis
- Axes are dimensions along which the array is organized
- Rank of an array = number of axes
More
- ≢ (monadic): tally. Number of major cells in an array. Like len() of python
- ≡ (monadic): depth. ie, maximum level of (array) nesting
- Depth and rank aren't same
- ⍤: rank
- ⊢: ??
Examples
> n ← 5 6 7
> n + 2
7 8 9
> +/n
18
> -/n
> ⍝ 5 - (6 - 7)
6
> ×/n
210
> */n
> ⍝ Because * is exponentiation
> ⍝ 5^(6^7) is quite a large number (a 195667-digit number)
∞
emacs
emacs: gnu-apl mode
- https://github.com/lokedhs/gnu-apl-mode
- Activate with
M-x gnu-apl-mode
- Shift to APL symbol keyboard by holding super key (ie, 'windows key')
- There are other ways like the APL-Z input method.
Keyboard
Default keyboard is like (from here):
╔════╦════╦════╦════╦════╦════╦════╦════╦════╦════╦════╦════╦════╦═════════╗
║ ~ ║ !⌶ ║ @⍫ ║ #⍒ ║ $⍋ ║ %⌽ ║ ^⍉ ║ &⊖ ║ *⍟ ║ (⍱ ║ )⍲ ║ _! ║ +⌹ ║ ║
║ `◊ ║ 1¨ ║ 2¯ ║ 3< ║ 4≤ ║ 5= ║ 6≥ ║ 7> ║ 8≠ ║ 9∨ ║ 0∧ ║ -× ║ =÷ ║ BACKSP ║
╠════╩══╦═╩══╦═╩══╦═╩══╦═╩══╦═╩══╦═╩══╦═╩══╦═╩══╦═╩══╦═╩══╦═╩══╦═╩══╦══════╣
║ ║ Q ║ W⍹ ║ E⋸ ║ R ║ T⍨ ║ Y¥ ║ U ║ I⍸ ║ O⍥ ║ P⍣ ║ {⍞ ║ }⍬ ║ |⊣ ║
║ TAB ║ q? ║ w⍵ ║ e∈ ║ r⍴ ║ t∼ ║ y↑ ║ u↓ ║ i⍳ ║ o○ ║ p⋆ ║ [← ║ ]→ ║ \⊢ ║
╠═══════╩═╦══╩═╦══╩═╦══╩═╦══╩═╦══╩═╦══╩═╦══╩═╦══╩═╦══╩═╦══╩═╦══╩═╦══╩══════╣
║ (CAPS ║ A⍶ ║ S ║ D ║ F ║ G ║ H⍙ ║ J⍤ ║ K ║ L⌷ ║ :≡ ║ "≢ ║ ║
║ LOCK) ║ a⍺ ║ s⌈ ║ d⌊ ║ f_ ║ g∇ ║ h∆ ║ j∘ ║ k' ║ l⎕ ║ ;⍎ ║ '⍕ ║ RETURN ║
╠═════════╩═══╦╩═══╦╩═══╦╩═══╦╩═══╦╩═══╦╩═══╦╩═══╦╩═══╦╩═══╦╩═══╦╩═════════╣
║ ║ Z ║ Xχ ║ C¢ ║ V ║ B£ ║ N ║ M ║ <⍪ ║ >⍙ ║ ?⍠ ║ ║
║ SHIFT ║ z⊂ ║ x⊃ ║ c∩ ║ v∪ ║ b⊥ ║ n⊤ ║ m| ║ ,⍝ ║ .⍀ ║ /⌿ ║ SHIFT ║
╚═════════════╩════╩════╩════╩════╩════╩════╩════╩════╩════╩════╩══════════╝
(Depending on the font used, this may look different on different devices.)
vim
vim-apl: https://github.com/zoomlogo/vim-apl
Backtick is the prefix key by default:
`i
givesι
`$
gives⍋
`r
givesρ
gnuapl
REPL:
- Exit repl:
)OFF
- Run like: apl -s -f <file-name.apl>
Even more
- Game of life with apl: https://www.youtube.com/watch?v=a9xAKttWgP4
- ANN with apl: https://www.youtube.com/playlist?list=PLgTqamKi1MS3p-O0QAgjv5vt4NY5OgpiM