Multiset Problem when merging

From: Alex (capt_maddog_at_yahoo.com)
Date: 10/07/04

  • Next message: Renate Meijer: "OT Re: java and prolog problem"
    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 :)


  • Next message: Renate Meijer: "OT Re: java and prolog problem"

    Relevant Pages

    • Re: multisets
      ... of an inductive program (which includes all non-trivial definite Prolog ... and not needing any at all. ... If 0 forms the base case, you will arrive at my first program for multiset. ... but your preaching on how to teach Prolog might come across more credible ...
      (comp.lang.prolog)
    • Re: multiset ordering
      ... This is a Prolog group so I hesitate to put much Smalltalk ... He said a "multiset" was a ... use a particular language. ... languages shouldn't come with high-quality libraries to handle the ...
      (comp.lang.prolog)
    • Re: multiset ordering
      ... The code I posted was supposed to do multiset ordering, ... and the first predicate will generate infinitely many ... % This is the subtraction for multisets, represented by lists. ...
      (comp.lang.prolog)
    • Re: multisets
      ... this time I wonder how the Prolog geeks would write ... a predicate which gets multisets of cardinality C ... a multiset Set of length Len with numbers M up to N ...
      (comp.lang.prolog)
    • Re: multisets
      ... this time I wonder how the Prolog geeks would write ... a predicate which gets multisets of cardinality C ... The multiset of cardinality C filled with numbers between M+1 and N ...
      (comp.lang.prolog)