Reputation: 862
I am working on writing a subset function and I have succeeded in doing so. Here's my function which implements member function:
member( X, [ X | T ] ).
member( X, [ _ | T ] ) :- member( X, T ).
subset([], _).
subset([H|T1], T2) :-
member(H, T2),
subset(T1, T2).
subset([H1|T1], [H2|T2]) :-
\+ member(H1, T2),
subset([H1|T1], T2).
My question is, is there a better way to write this function using the member function of course.
Upvotes: 1
Views: 1334
Reputation: 22585
The third clause of subset/3
does not make sense and i think that it should be removed.
If H1
is not a member of T2
, then the recursive call subset([H1|T1], T2)
will obviously don't succeed either.
Aside from that, the first two clauses seem the way to go.
Upvotes: 2