"Learn APL" Notes

https://news.ycombinator.com/rss Hits: 5
Summary

Some functions in APL can be used to select portions of an array. When associated with assignment, they can be used to assign values to portions of such array. Bracket indexing is the easiest example. Let's assign the first three elements of a vector by using the Take (dyadic ↑) function. VEC←⍳5 (3↑VEC)←'ABC' 8⎕CR VEC ┌→──────┐ │ABC 4 5│ └───────┘ Let's use the Ravel (monadic ,) on a matrix to assign a new vector value to it: MAT←3 4⍴'ABCDEFGHIJKL' (,MAT)←'NEW DATAHERE' ⍝ Ravelled matrix appears as a vector 8⎕CR MAT ⍝ Assignment occurs in matrix itself ┌→───┐ ↓NEW │ │DATA│ │HERE│ └────┘ Now let's combine Compression (dyadic /) and Ravel (monadic ,) to select all A's on the matrix, and replace them by asterisk: (('A'=,MAT)/,MAT)←'*' 8⎕CR MAT ┌→───┐ ↓NEW │ │D*T*│ │HERE│ └────┘ We can also combine Take (dyadic ↑) and Ravel (monadic ,) to replace elements at the top-left 2×2 submatrix of MAT: (,2 2↑MAT)←'⎕⎕⎕⎕' 8⎕CR MAT ┌→───┐ ↓⎕⎕W │ │⎕⎕T*│ │HERE│ └────┘ We can also use the Compression (/) function for selection. TABLE←3 4⍴⍳12 8⎕CR TABLE (1 0 1 0/TABLE)←3 2⍴100 8⎕CR TABLE ┌→─────────┐ ↓1 2 3 4│ │5 6 7 8│ │9 10 11 12│ └──────────┘ ┌→────────────┐ ↓100 2 100 4│ │100 6 100 8│ │100 10 100 12│ └─────────────┘ In the next example, we have a vector X. We want to replace the first ⍴X elements of DATA with the contents of X. DATA←⍳13 X←10×⍳3 8⎕CR DATA ((⍴X)↑DATA)←X 8⎕CR DATA ┌→────────────────────────────┐ │1 2 3 4 5 6 7 8 9 10 11 12 13│ └─────────────────────────────┘ ┌→───────────────────────────────┐ │10 20 30 4 5 6 7 8 9 10 11 12 13│ └────────────────────────────────┘ Replace the first X+2 elements of Y with the reverse of a vector containing numbers 1 up to X+2: Y←⍳10 X←3 8⎕CR Y ((2+X)↑Y)←⌽⍳X+2 8⎕CR Y ┌→───────────────────┐ │1 2 3 4 5 6 7 8 9 10│ └────────────────────┘ ┌→───────────────────┐ │5 4 3 2 1 6 7 8 9 10│ └────────────────────┘ We can use Enlist (monadic ∊) to remove nesting from an array. 8⎕CR NEST←(2 2⍴⍳4) 'TEXT' (3 1⍴⍳3) (∊NEST)←0 8⎕CR NEST ⍝ Set specific positio...

First seen: 2025-10-25 21:38

Last seen: 2025-10-26 01:44