Re: Sorting a list of lists in the order of ascending length

Paulo Moura <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
On 24/09/2013, at 17:39, "Norbert E. Fuchs" <[email protected]> wrote:

> Hi
> 
> I need all subsets of a list in the order of ascending length of the subsets. 
> 
> Thus I wrote a predicate 
> 
>  generate_subset(+List, -SubList) that generates a SubList of List 
> 
> and that is also used for other purposes, and a variant of insertion sort
> 
>  sort_list_of_lists_in_ascending_length(+ListOfLists, -SortedListOfList) that sorts ListOfLists into SortedListOfList 
> 
> and then called 
> 
>  findall(SubList, generate_subset(List, SubList), SubLists), sort_list_of_lists_in_ascending_length(SubLists, SortedSubLists)
> 
> This works quite nicely and efficiently, but nevertheless I wonder whether there isn't a simpler way to perform this operation. 


An alternative would be to modify an implementation of a power set predicate to return each subset in the form Length-Subset and then to use keysort/2.

Cheers,

Paulo


-----------------------------------------------------------------
Paulo Moura
Logtalk developer

Email: <mailto:[email protected]>
Web:   <http://logtalk.org/>
-----------------------------------------------------------------
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.