简体   繁体   中英

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?

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 ) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM