Prolog: eliminate repetitions in query

前端 未结 3 804
遇见更好的自我
遇见更好的自我 2021-01-26 03:55

I\'ve been trying to write a simple code, that would behave in this manner:

| ?- hasCoppiesOf(X,[a,b,a,b,a,b,a,b]).
X = [a,b] ? ;
X = [a,b,a,b] ? ;
X = [a,b,a,b         


        
3条回答
  •  -上瘾入骨i
    2021-01-26 04:46

    I think this is what you're trying for...

    coppies(Z,Z,[]).
    coppies(X,Z,[Y|Ys]):- \+member(Y,Z),coppies(X,[Y|Z],Ys).
    coppies(X,Z,[Y|Ys]):- member(Y,Z),coppies(X,Z,Ys).
    
    
    copies(M,[Y|Ys]):-coppies(M,[],[Y|Ys]).
    

    Input:

    copies(X,[1,2,1,2,1,2]).

    Output:

    X = [2, 1].

    BTW I've used some different names instead..

提交回复
热议问题