Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

disjoint in prolog 1

Status
Not open for further replies.

12071982

IS-IT--Management
Nov 10, 2008
6
MT
Can anyone help me do such routine in prolog
disjoint([1,2,3],[2,3,4],Ans) binds Ans to [1,4]

thnx
 
disjoint(X,Y,R).

If I have understood your question, you just have to write in English what you find in Ans, and translate it in Prolog eg in R, you find elements of X which are not in Y and elements of Y which are not in X.

So, begin by writing a rule that takes 2 lists A and B, and give a list C containing elements of A which are not in B.


 
Can anyone write a prolog routine for me for
disjoint([1,2,3],[2,3,4],Ans) binds Ans to [1,4]
coz though i managed to understand the concept, i dont know how to implement it by programming.
I cant use reserved words found in prolog for my assignment.

thnx
lor
 
Here is the code for "a rule that takes 2 lists A and B, and give a list C containing elements of A which are not in B."
Code:
% in_A_but_not_in_B(+LstA, +LstB, +Lst_in_Construction, -Lst_Final)
% when A is finished, we unify Lst_in_Construction and Lst_Final
in_A_but_not_in_B([], _, L, L).


% we check if the first element of A is in B
in_A_but_not_in_B([H|T], B, LC, LF) :-
	member(H, B),
	% yes, it's in, we don't memorize it
	% and we carry on with the rest of liste A
	in_A_but_not_in_B(T, B, LC, LF).

% we check if the first element of A is in B
in_A_but_not_in_B([H|T], B, LC, LF) :-
	\+member(H, B),
	% no, it's not in it, we memorize it
	% and we carry on with the rest of liste A
	in_A_but_not_in_B(T, B, [H| LC], LF).

test(R) :-
	% when we start, List_in_Construction is empty
	in_A_but_not_in_B([1,2,3, 5], [2,3,4], [], R).
 
thanks a lot for your help ..i think i can manage now :)
 
I managed to do my prolog problem thanks to your help:)

Thanks once again...much obliged :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top