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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 5as 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⍴1and then try the grade up function and then try the grade down function:and
They will both yield the same result: