Multiset Problem when merging
From: Alex (capt_maddog_at_yahoo.com)
Date: 10/07/04
- Previous message: sachin786: "java and prolog problem"
- Next in thread: Paul Singleton: "Re: Multiset Problem when merging"
- Reply: Paul Singleton: "Re: Multiset Problem when merging"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 7 Oct 2004 04:02:07 -0700
Hi,
I have a problem with Prolog. Im new to Prolog and only used to code
Java and a little C.
I have a multiset: [a/2,b/5,d/7] This means a exists 2 times in the
multiset, b 5 times and d 7 times.
I have made the predicate element:
% element/3
element(E,[E/N|_],N).
element(E,[_|T],N) :-
element(E,T,N).
This works fine, it returns True when E exists N times in the multiset
Im now working on a predicate called vereniging(S1,S2,R) ,its purpose
is to return True when multiset R is S1 and S2 combined
Example:
vereniging([3/2,4/3],[2/2,3/1,4/5,1/3],R).
R = [2/2,3/3,4/8,1/3]
In words i know what my predicate should look like:
First You have to split S1 into Head1 and Tail1
Then we must split Head1 into A1/B1
Now we must check if A1 exists in S2 (as A2/B2).
If so, we must change the value of B2 into B1+B2
if not, we must add A1/B1 to S2
I tried to do this for several hours, but did not get it to work
vereniging([],S2,S2).
vereniging([H1/X|T1],[H1/Y|T2],R) :-
M is X + Y,
vereniging(T1,[H1/M|T2],R).
vereniging([H1/X|T1],[H2|T2],R) :-
element(H1,[H2|T2],X),
vereniging([H1/X|T1],T2,R).
Thank you all so much for helping me :)
- Previous message: sachin786: "java and prolog problem"
- Next in thread: Paul Singleton: "Re: Multiset Problem when merging"
- Reply: Paul Singleton: "Re: Multiset Problem when merging"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|