Predicate for subset

16 views
Skip to first unread message

Langstra

unread,
Apr 9, 2016, 8:35:05 AM4/9/16
to SWI-Prolog
I am looking to create a predicate subset that checks for two sets S1 and S2 is S1 is a subset of S2. That is subset([1, 2], [1, 2, 3]) is true but subset([1, 2], [7, 1, 5]) is false.


I have been able to create predicates that can check if S1 is a subset of S2, but then when I want to find all the subset for S2, using subset(X, [1,2,3]) I get that X = [1, 1, 1, 1, 1,...].

member( X, [ X | T ] ).
member
( X, [ _ | T ] ) :-
    member
( X, T ).

subset
([], _).
   
subset
([H|T], [H2|T2]) :-
    member
(H, [H2|T2]),
    subset
(T, [H2|T2]).


I also created some other predicate subset which is able to find all the subsets for some given set, but then it also says that subset([2,1], [1,2,3]). is false.
subset([],V).
subset
([H|T1],[H|T2]) :-
    subset
(T1,T2).
   
subset
([H1|T1],[H2|T2]) :-
    subset
([H1|T1],T2).


Is there anyone who can help me with this problem? I would like to avoid using the functions var and nonvar.
Reply all
Reply to author
Forward
0 new messages