In your favorite programming language, how do you sort a list of the strings "a", "bcd", "ef", and "ghij" in descending order of length?
One proposed solution was:
tag_negative_len(Str, NegLen-Str) :-
atom_length(Str, StrLen),
NegLen is -1*StrLen.
rem_tag(_-Val, Val).
sort_desc_len(StrLs, Sorted) :-
maplist(tag_negative_len, StrLs, TaggedLs),
keysort(TaggedLs, SortedTaggedLs),
maplist(rem_tag, SortedTaggedLs, Sorted).
I assume that the above code was written for ISO Prolog, because it doesn't make use of implementation-specific features. Example usage:
?- sort_desc_len(["a", "bcd", "ef", "ghij"], L).
L = ["ghij", "bcd", "ef", "a"].
I would have solved it in a similar way. However, the solution is extremely verbose (as compared to the one or two-liners of other programming languages), and pollutes the program with helper/auxiliary predicates. Is there a better way to solve the problem in ISO Prolog?