谓词是否在列表中一式三份?

时间:2019-10-10 09:27:37

标签: prolog swi-prolog

我正在写一个谓词,如果一个列表包含一个元素的3个副本,则打印true并打印该元素。此外,谓词还应打印备用解决方案(如果存在)。

我写的谓词-

hasTriplicate(List):-hasTriplicateAcc(List,Element,0).

%hasTriplicateAcc is wrapped by predicate hasTriplicate since I wanted the arity of hasTriplicate to be 1.

hasTriplicateAcc(_,Element,3):-write(Element).
hasTriplicateAcc([H|T],H,Ct):-hasTriplicateAcc(T,H,Ct1),Ct1 is Ct+1.
hasTriplicateAcc([Z|T],H,Ct):-hasTriplicateAcc(T,H,Ct),Z=\=H.

输出

hasTriplicate([1,1,1]).
11
ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR:   [11] 3 is _10188+1
ERROR:   [10] hasTriplicateAcc([1,1],1,_10218) at c:/users/user/desktop/code/hastriplicate.pl:3
ERROR:    [9] hasTriplicateAcc([1,1|...],1,0) at c:/users/user/desktop/code/hastriplicate.pl:3
ERROR:    [7] <user>
ERROR: 
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
Exception: (10) hasTriplicateAcc([1, 1], 1, _10332)

我不明白该错误。如果有人告诉我如何更正谓词代码,那就太好了。

1 个答案:

答案 0 :(得分:0)

我认为此解决方案将有效。

Auto