Prolog: eliminate repetitions in query

前端 未结 3 809
遇见更好的自我
遇见更好的自我 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条回答
  •  臣服心动
    2021-01-26 04:28

    I think you just made your predicate more complex than it needs to be, probably just overthinking it. A given solution may succeed in multiple paths through the logic.

    You can do this without append/3 by aligning the front end of the lists and keep the original list to "reset" on repeats:

    % Empty list base cases
    dups_list([], []).
    dups_list([_|_], []).
    
    % Main predicate, calling aux predicate
    dups_list(L, Ls) :-
        dups_list(L, L, Ls).
    
    % Recursive auxiliary predicate
    dups_list([], [_|_], []).
    dups_list([], [X|Xs], [X|Ls]) :-
        dups_list(Xs, [X|Xs], Ls).
    dups_list([X|Xs], L, [X|Ls]) :-
        dups_list(Xs, L, Ls).
    

    Here are some results:

    | ?- dups_list(X,[a,b,a,b,a,b,a,b]).
    
    X = [a,b] ? a
    
    X = [a,b,a,b]
    
    X = [a,b,a,b,a,b,a,b]
    
    no
    | ?- dups_list([a,b,a,b,a,b,a,b], X).
    
    X = [] ? ;
    
    X = [a,b,a,b,a,b,a,b] ? ;
    
    X = [a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b] ? ;
    
    X = [a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b] ?
    ...
    | ?- dups_list(A, B).
    
    A = []
    B = [] ? ;
    
    A = [_|_]
    B = [] ? ;
    
    A = [C]
    B = [C] ? ;
    
    A = [C]
    B = [C,C] ? ;
    
    A = [C,D]
    B = [C,D] ? ;
    
    A = [C]
    B = [C,C,C] ? ;
    
    A = [C,D,E]
    B = [C,D,E] ? ;
    ...
    

    There may be a way to simplify the solution just a bit more, but I haven't played with it enough to determine if that's the case.

提交回复
热议问题