I want to rearrange a list according to the occurrence of length of sublists from small to large.For example,the expected answer is rearranged by the number of the length of sub-list,there is one length of 4,two length of 1, three length of 3 and four length of 2.
Example query and its result:
lists_ascending([[a,b],[c],[a,b,c],[d,d,d],[d,s],[s],[d,s,s,a], [s,a],[s,t],[a,b,w]],Ls).
Ls = [[d,s,s,a],[c],[s],[a,b,c],[d,d,d],[a,b,w],[a,b],[d,s],[s,a],[s,t]]
My idea is that to calculate each length first and then do rearrangement. What I have done so far is to collect the number of sublist whose length is equal to the first sublist using the pattern [length-number].
count([],[0-0]).
count([A|B],[L-N]):-
length(A,L),
same_length(B,L,M),
N is M+1.
same_length([],_,0).
same_length([A|B],L,N) :-
( length(A,L)->
same_length(B,L,M),
N=M+1
; same_length(B,L,N)
).
The count(LIST,X) output is as followed:
21 ?- count_slot([[2],[3],[4],[2,3,4]],X).
X = [1-3].
But the expected output is [1-3,3-1], I don't know how to deal with the rest sublist(remove one by one??) and rearrange them according to the pattern [1-3,3-1].
Can somebody help? Thanks in advanced.