alok
alok

Reputation: 1

check/2 predicate in prolog that will have sorted list of lists

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

Answers (1)

Nicholas Carey
Nicholas Carey

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

Related Questions