Prolog列表差异例程
我正在尝试在序言中实现列表差异例程。 由于某种原因,以下失败:
difference(Xs,Ys,D) :- difference(Xs,Ys,[],D).
difference([],_,A,D) :- D is A, !.
difference([X|Xs],Ys,A,D) :-
not(member(X,Ys)),
A1 is [X|A],
difference(Xs,Ys,A1,D).
尝试时:
?- difference([1,2],[],D).
我收到此错误:
ERROR: '.'/2: Type error: `[]' expected, found `1' ("x" must hold one character)
^ Exception: (10) _L161 is [2|1] ?
I am trying to implement a list difference routine in prolog.
For some reason the following fails:
difference(Xs,Ys,D) :- difference(Xs,Ys,[],D).
difference([],_,A,D) :- D is A, !.
difference([X|Xs],Ys,A,D) :-
not(member(X,Ys)),
A1 is [X|A],
difference(Xs,Ys,A1,D).
When trying:
?- difference([1,2],[],D).
I get this error:
ERROR: '.'/2: Type error: `[]' expected, found `1' ("x" must hold one character)
^ Exception: (10) _L161 is [2|1] ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的用法 A1 是 [X|A] 不正确。谓词is仅用于算术。
顺便说一句,SWI-Prolog 有内置的减法谓词:
这是您需要的吗?
Your usage A1 is [X|A] is incorrect. Predicate is is used only for arithmetics.
Btw, SWI-Prolog has built-in subtract predicate:
Is this what you need?
使用 find all 解决方案变得显而易见:
Using find all the solution becomes obvious: