1

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?

  • This is math, vanilla stackexchange is that way -> https://stackexchange.com/ –  Jun 03 '20 at 20:54
  • Ah okay, I thought that GAP was a math-programming thing, but I can try to ask it on the main one. – Ethan Kowalenko Jun 03 '20 at 20:56
  • @EthanKowalenko please see the GAP tag description for other GAP support channels. The number of GAP questions on the main stackexchange site is lower and they get less attention due to the lack of critical mass of GAP developers and users there. – Olexandr Konovalov Jun 03 '20 at 21:10
  • 2
    As for the question, it is SortBy that does not return anything. Enter ?SortBy in GAP to see its documentation and examples. – Olexandr Konovalov Jun 03 '20 at 21:14
  • 1
    @EthanKowalenko great! See also https://www.gap-system.org/Manuals/doc/ref/chap12.html#X84545F3985C60F5B about identical objects - there are some subtle things there which can be easily encountered with your example. – Olexandr Konovalov Jun 04 '20 at 08:36

1 Answers1

3

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.

ahulpke
  • 18,416
  • 1
  • 21
  • 38