p = perms([0:2])
p =
2 1 0
2 0 1
1 2 0
1 0 2
0 1 2
0 2 1
This function is supposed to display the permutations of a vector in reverse lexicographical order. Hence, I would expect the last row of this output to contain the elements 0 1 2; however, it contains 0 2 1. The other rows are displayed correctly.
In short, the order of the last two rows are interchanged. What is going on here?
Yes, this seems to be a bug. Good catch! But probably a bug in the documentation, rather than in the function.
If you type
open permsto see the source code, you'll see the following description in the first lines:in which no reference is made to reverse lexicographical order.
The actual job is done by the recursive, local function
permsr. If you look at its code, it's not obvious at first how it works (as usual with recursion), but the linegives a clue that no particular order is sought in the result.
If you try a larger vector you'll see discrepancies from reverse lexicographical order in more rows:
In summary, the function seems to have been designed without regard to any order. It is the documentation which is probably wrong in claiming that order.