While learning Raku, I arrive to the point of higher order function and the sort function.
I have this example:
> sort <4 6 2 9 1 5 11>
(1 2 4 5 6 9 11)
Then the doc for the routine says this:
Sorts the list, smallest element first. By default infix:<cmp> is used for
comparing list elements.
And the book that I'm following "Piensa en raku", in section 9.2, makes a comparative between numeric sort and lexicographic sort.
I tried the following:
> sort &le, <4 6 2 9 1 5 11>;
===SORRY!=== Error while compiling:
Undeclared routine:
le used at line 1. Did you mean 'lc'?
But getting this problem, instead of the sorted list lexicographically ordered. So may be is too early for me to understand this, but should be possible to pass an operator in Raku like a function, since also the documentation says that it is using an infix: or I need to do something like this, maybe I'm confusing operators and subroutines:
sub my-le($a,$b) {
$a le $b;
}
sort &my-le, <4 6 2 9 1 5 11>;
or this:
sort { $^b le $^a }, <4 6 2 9 1 5 11>;
So I have the question about different use the infix operator and a subroutine, maybe the problem about this difference is that the order of the operands affect the result of some operations. so you can not use so lightly passing it as a function or a parameter or variable
> sort { $^a le $^b }, <4 6 2 9 1 5 11>
(9 6 5 4 2 11 1)
> sort { $^b le $^a }, <4 6 2 9 1 5 11>
(1 11 2 4 5 6 9)
I hope that I can explained my doubts on this problem.