Reputation: 1
I am new to prolog and I am trying to create a simple predicate which will have sorted list of lists from a unsorted list of lists. check(A,B).
check( [ [], [1], [1,1] ], [ [], [1,1], [1] ] ). returns true
check( [ [], [1], [1,1] ], [ [1,1], [1] ] ). returns false.
Note that even if A is sorted it should only contain elements from B and not anything more or less.
How do I implement this without any built in prolog predicates?
Upvotes: 0
Views: 171
Reputation: 74197
A list of lists is essentially a tree structure: you just walk the tree. Something like this:
validate( [] , _ ) .
validate( [X|Xs] , Valids ) :-
exists_in( X , Valids ) ,
validate( Xs , Valids )
.
exists_in( X , [X|Xs] ) :- !.
exists_in( X , [_|Xs] ) :- exists_in( X , Xs ) .
Upvotes: 1