升级/降级 APL 订单
怎么会
⌽(⍒'Hello')
是
1 2 4 3 5
时候
⍋'Hello'
这个
1 2 3 4 5
?
我是 APL 的新手,偶然发现它。我只是想知道为什么第二个 l 出现在第一个之前。
How come that
⌽(⍒'Hello')
is
1 2 4 3 5
when
⍋'Hello'
is
1 2 3 4 5
?
I'm new to APL and stumbled on it by accident. I just wonderes why the second l comes before the first.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用升级
⍋
和降级⍒
作为单子基元。根据定义,升级返回一个索引整数数组,该数组指定其后面的表达式的排序顺序(按升序排列)。如果任何元素相等(在您的示例中是两个字母 l),它们将按照它们在输入表达式中出现的顺序出现在结果中。
因此,
⍋'Hello'
返回1 2 3 4 5
。两个l 的顺序相同,即第3 个字符(第1 个字母l)在第4 个字符(第2 个字母l)之前。根据定义,降级还返回一个索引整数数组,该数组指定其后面的表达式的排序顺序(按降序顺序)。如果任何元素相等(在您的示例中是两个字母 l),它们也将以与表达式中出现的顺序相同的顺序出现在结果中。
因此,
⍒'Hello'
返回5 3 4 2 1
。两个 l 保持相同的顺序,因为它们相等。当您应用旋转
⌽
时,整数数组将反转为1 2 4 3 5
,如您所见。鉴于函数的定义方式以及它们如何处理相等值,您看到的结果正是预期的结果。
如果您想查看更极端的示例,请比较以下两个数组的输出。创建一个包含 10 个元素的数组,每个元素具有相同的值 1。
10⍴1
,然后尝试升级函数,然后尝试降级函数:它们
都会产生相同的结果:
You are using both the grade up
⍋
and grade down⍒
as monadic primitives.By definition grade up returns an integer array of indices which specify the sorted order of the expression following it, in ascending order. If any elements are equal (in your example the two letter l's) , they will appear in the result in the same order that they appeared in the input expression.
So,
⍋'Hello'
returns1 2 3 4 5
. The two l's are in the same order, i.e., the 3rd character (1st letter l) precedes the 4th character (2nd letter l).By definition grade down also returns an integer array of indices which specify the sorted order of the expression following it, in descending order. If any elements are equal (in your example the two letter l's) , they will also appear in the result in the same order that they appeared in the expression.
So,
⍒'Hello'
returns5 3 4 2 1
. The two l's remain in the same order because they are equal.When you apply rotate
⌽
the integer array gets reversed to1 2 4 3 5
as you witnessed.The outcome you are seeing is precisely what is expected given the way the functions are defined and how they deal with equal values.
If you want to see a more extreme example compare the output for the following two arrays. Create an array with 10 elements each having the same value of 1.
10⍴1
and then try the grade up function and then try the grade down function:and
They will both yield the same result:
Grade up ⍋ 和 Grade Down ⍒ 原语保留相等元素的顺序。正如其他人所说,必须有平等争论的规则。但这条规则的优点是它允许多键排序。
也就是说,如果您有一个包含多个关联键的数组,则通过对每个键从最不重要到最重要的顺序进行排序,您将获得按最重要的键排序的结果,其中 equals 按第二个最重要的项排序,第一个项相等两个按第三个排序,依此类推。为此,必须捕获索引向量并将其用于更新所有密钥和数据以保持它们同步。或者它们可以存储在嵌套结构中,在这种情况下,它们将自动保持正确的相对顺序。
The grade up ⍋ and grade down ⍒ primitives preserve the order of equal elements. As others have said, there must be a rule for equal arguments. But this rule has the virtue that it allows multi-key sorts.
That is, if you have an array with several associated keys, by sorting on each key from least significant to most significant, you obtain a result sorted by the most significant key, with equals sorted by the 2nd mot significant, items equal on the 1st two sorted by the 3rd, and so on. For this to work the index vector must be captured and used to update all keys and the data to keep them in sync. Or they could be stored in a nested structure, in which case they would automatically be kept in proper relative order.