I'm very new to GAP, and I'm struggling with SortBy. Starting with a list of lists, I want to sort each list by AbsoluteValue in GAP. However, I can only get Sort or SortBy to work outside of a loop. The problem exists already in this tiny example:
gap> A:=[[-5,-3,-1,2,4,6],[-6,-4,-2,1,3,5]];
[ [ -5, -3, -1, 2, 4, 6 ], [ -6, -4, -2, 1, 3, 5 ] ]
gap> sorter:=function(X)
> for i in [1..Length(X)] do
> X[i]:=SortBy(X[i],AbsoluteValue);
> od;
> end;;
gap> sorter(A);
Error, Function Calls: <func> must return a value in
X[i] := SortBy( X[i], AbsoluteValue ); at *stdin*:24 called from
<function "sorter">( <arguments> )
called from read-eval loop at line 27 of *stdin*
you can supply one by 'return <value>;'
brk>
gap> A[1];A[2];
[ -1, 2, -3, 4, -5, 6 ]
[ -6, -4, -2, 1, 3, 5 ]
It sorts the first list, but apparently doesn't like the second one. I think the error message says that AbsoluteValue doesn't return a value, which is false. What am I missing?
Best Answer
Just -- for future readers -- the reason for the initial problem:
SortBy
changes a list (but does not return the sorted list), while e.g.Sorted
would leave the list unchanged and return the sorted copy. The general language convention is that verbs do something to an object, while nouns create a new object with the desired characteristics.