Prolog Beginner: How to make unique values for each Variable in a predicate

前端 未结 3 1415
一个人的身影
一个人的身影 2020-12-21 15:32

I have a prolog predicate:

Add( [A|B] , Answer ) :-
    ...
    ~ Add everything in the list to come up with answer
    ...

I would now lik

3条回答
  •  無奈伤痛
    2020-12-21 16:00

    Khm... You should understand that doStuff(A,B,C,D) and doStuff(A,A,B,B) means. First is going to unify values A .. D with appropriate values which makes doStuff/4 reachable goal. And second is equal to A=B, C=D, doStuff(A,B,C,D) and doStuff(A,B,C,D), A=B, C=D (but last variant probably will cause backtracking). So I hope you understand that unique/1 shouldn't be done inside doStuff/4, because it's outside restriction. So you shoulad use doStuff(A,B,C,D), unique([A,B,C,D]) and doStuff(A,A,B,B), unique([A,B]).

    I wonder how you read A is not B... Anyway you can define unique/1 as

    not_unique([H|T]):- member(H, T) ; not_unique(T).
    unique(L):- not(not_unique(L)).
    

提交回复
热议问题