学习APL笔记
"Learn APL" Notes

原始链接: https://luksamuk.codes/pages/learn-apl.html

## APL 数组操作总结 APL 提供了强大的函数来选择和修改数组的部分内容。基本的方括号索引(例如 `TAB[2;1]←8`)允许直接元素赋值。更复杂的选择使用诸如 **Take (↑)**、**Ravel (,)** 和 **Compression (/),** 等函数,通常结合使用。 **Take** 提取数组的一部分,而 **Ravel** 将多维数组展平成向量,从而可以一次性地赋值给整个结构。**Compression** 根据布尔条件选择元素。例如,`(('A'=,MAT)/,MAT)←'*'` 将矩阵中的所有 'A' 替换为星号。 这些函数可以组合起来进行复杂的操作——替换子矩阵,或根据其值或位置有选择地更新元素。数组的形状 (`⍴X`) 也可以用来动态确定选择大小。 **Enlist (∊)** 移除嵌套,允许赋值给嵌套数组中的特定位置。最后,**First (↑)** 选择第一个元素,从而可以替换整个子数组。APL 的灵活性允许进行高度简洁和富有表现力的数组操作。

一个黑客新闻的讨论集中在学习和使用APL,一种20世纪60年代开发的编程语言。用户分享了使用GNU APL的经验,并指出它可以通过C语言进行扩展,以及输入APL独特符号的方法——包括自定义XCompose定义和专门的键盘布局,如USAPLSETI。 对话深入探讨了APL符号编码的历史,以及Unicode之前的状况。在Unicode(1991/1993年推出)之前,需要自定义编码,Dyalog APL仍然支持其原始的、不兼容ASCII的编码。早期的实现甚至使用IBM Selectric打字机上的字符重叠来创建符号。 APLWiki等资源和键盘布局配置链接被分享,为有兴趣探索该语言的人们提供实用建议。该帖子还包含一首以APL为主题的歌曲,以及关于Y Combinator申请的提醒。
相关文章

原文

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.

TAB←2 3⍴⍳6
TAB[2;1]←8

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 2100
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 position to number
(6⌷∊NEST)←999
8⎕CR NEST

Set specific position to character vector (text).
For that, introduce extra nesting to the new text.
(7⌷∊NEST)←⊂'TEXT'
8⎕CR NEST
┌→───────────────┐
│┌→──┐ ┌→───┐ ┌→┐│
│↓1 2│ │TEXT│ ↓1││
││3 4│ └────┘ │2││
│└───┘        │3││
│             └─┘│
└∊───────────────┘
┌→──────────────────┐
│┌→──┐ ┌→──────┐ ┌→┐│
│↓0 0│ │0 0 0 0│ ↓0││
││0 0│ └───────┘ │0││
│└───┘           │0││
│                └─┘│
└∊──────────────────┘
┌→────────────────────┐
│┌→──┐ ┌→────────┐ ┌→┐│
│↓0 0│ │0 999 0 0│ ↓0││
││0 0│ └─────────┘ │0││
│└───┘             │0││
│                  └─┘│
└∊────────────────────┘
┌→─────────────────────────┐
│┌→──┐ ┌→─────────────┐ ┌→┐│
│↓0 0│ │0 999 ┌→───┐ 0│ ↓0││
││0 0│ │      │TEXT│  │ │0││
│└───┘ │      └────┘  │ │0││
│      └∊─────────────┘ └─┘│
└∊∊────────────────────────┘

The function First (monadic ) selects the first element of an array. Here, we shall replace the first 2×2 matrix of NEST by a character vector.

(↑NEST)←'ABC'
8⎕CR NEST

So, the sky is the limit. For more info, see this page.

联系我们 contact @ memedata.com