1

I want to sort a deep list by the depth of each list. But my code only separates the elements, not sorting them. By far, I don't have knowledge about difference lists to sort them in other way, so please let me see what is wrong with my code.

insert_ord(X, [H|T], [H|R]):- 
     X > H, !, 
     insert_ord(X, T, R).
insert_ord(X, T, [X|T]).

ins_sort([H|T], R):- 
     ins_sort(T, R1), 
     insert_ord(H, R1, R).
ins_sort([], []).

ord([H|L], [R1|R]):-
     is_list(H),
     ord(H, R1),
ord(L,R).

ord(L,R):-
     ins_sort(L, R).
ord([],[]).

So, this predicate should produce the following result:

ord([[[[1]]], 2, [5,[4],7], [[5],4], [5,[0,9]]], R).

R = [2, [5,[0,9]], [[5],4], [5,[4],7], [[[1]]]] ;
false
Nico
  • 125
  • 1
  • 10

0 Answers0