Re: Sorting atoms in a case-insensitive manner?
From: Jan Wielemaker (jan_at_ct.xs4all.nl)
Date: 11/29/03
- Previous message: Jan Wielemaker: "Re: how to redefine call/2 ?"
- In reply to: seguso: "Sorting atoms in a case-insensitive manner?"
- Next in thread: Bart Demoen: "Re: Sorting atoms in a case-insensitive manner?"
- Reply: Bart Demoen: "Re: Sorting atoms in a case-insensitive manner?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 29 Nov 2003 14:26:59 GMT
In article <pan.2003.11.29.11.28.11.885516@in.signature>, seguso wrote:
>
> Is there any easy way to sort a list of atoms in a case-insensitive manner?
>
> predsort(compare, List, SortedList).
>
> sorts it like that:
>
> Aaaa
> BBbbb
> aaaa
> bbbb
>
> whereas I need
>
> Aaaa
> aaaa
> BBbbb
> bbbb
>
> Thanks for your kind help.
Not really. As you are using SWI-Prolog anyway, the fastest definition
is:
case_insensitive_sort(Atoms, Sorted) :-
key_case(Atoms, Keyed),
keysort(Keyed, Sorted0),
unkey(Sorted0, Sorted).
key_case([], []).
key_case([H|T0], [L-H|T]) :-
downcase_atom(H, L),
key_case(T0, T).
unkey([], []).
unkey([_-T0], T) :-
unkey(T0, T).
(not tested).
I've considered adding a built-in as this operation is quite common in
some application domains and a built-in avoids the creation of a large
number of garbage atoms.
--- Jan
- Previous message: Jan Wielemaker: "Re: how to redefine call/2 ?"
- In reply to: seguso: "Sorting atoms in a case-insensitive manner?"
- Next in thread: Bart Demoen: "Re: Sorting atoms in a case-insensitive manner?"
- Reply: Bart Demoen: "Re: Sorting atoms in a case-insensitive manner?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|