문제

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.

도움이 되었습니까?

해결책

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' returns 1 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' returns 5 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 to 1 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:

⍋10⍴1

and

⍒10⍴1

They will both yield the same result:

1 2 3 4 5 6 7 8 9 10

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top