im trying to define a predicate in prolog that would return the union of two sets but im facing the following error.
set_operations:-
write('write 1 for union:'), nl,
read(X),
(X=1->
write('enter club 1:'),nl,
read(Y),
write('Enter club 2:'),nl,
read(Z),
union(Y,Z)).
club(football, [john, mary, peter, jane]).
club(basketball, [peter, jane, tom, kim]).
club(dance,[emily, andrew, john, jacob]).
union(Club1,Club2,R):-Club(Club1,L1), Club(Club2,L2),
concatenate(L1,L2,X), sort(X,R).
concatenate( [ ] , [ ] , [ ] ).
concatenate( [ ] , X , X ).
concatenate( X ,[], X ).
concatenate( [H1|T1] , L2, [H1|X] ):-concatenate(T1,L2,X).
Once you had corrected both the problems pinpointed by @brebs and @PauloMora, you still need to add the following directive:
:- redefine_system_predicate(union/3).
or rename your predicate to something like myunion
, to avoid the problem altogheter. Here is a possible implementation, simpler than your, based on append/3 followed by sort/2:
myunion(L1,L2,U) :- append(L1,L2,T),sort(T,U).