Re: Sorting a list of lists in the order of ascending length
Carlo Capelli <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <CABty9wz9bkuXM_W2Y-9TXN=XBjHmTjD0MwnOcFWKU61i6acRGA@mail.gmail.com> |
Not sure about the relative efficiency, maybe non backtrackable assignment
could help:
:- use_module(library(rbtrees)).
:- use_module(library(nb_rbtrees)).
ordkey :- rb_empty(R),
forall(member(W, [ls,mkdir,cd,ftp]),
( atom_length(W, K),
( nb_rb_get_node(R, K, N)
-> nb_rb_node_value(N, Ws),
nb_rb_set_node_value(N, [W|Ws])
; nb_rb_insert(R, K, [W])
)
)), rb_visit(R, L), writeln(L).
yields
ordkey.
[2-[cd,ls],3-[ftp],5-[mkdir]]
bye Carlo
2013/9/24 Norbert E. Fuchs <[email protected]>
>
> 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.
>
> Regards.
>
> --- nef
> _______________________________________________
> SWI-Prolog mailing list
> [email protected]
> https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
>
-------------- next part --------------
HTML attachment scrubbed and removed