带限制的Prolog计数
我不知道如何实现以下目标: 我想计算满足某个条件(其值未知)的次数 例如,如果我有列表带限制的Prolog计数,prolog,sicstus-prolog,clpfd,Prolog,Sicstus Prolog,Clpfd,我不知道如何实现以下目标: 我想计算满足某个条件(其值未知)的次数 例如,如果我有列表[A1、A2、A3]和[B1、B2、B3],我该怎么做 创建一个列表[R1,R2,R3],其中Ri是1如果Ai=Bi如果不是0 这是“方案”的基础 您应该“具体化”您的条件,张贴表单的约束条件 具体化(A、B、C):- C#A#=B 在变量对之间。地图列表/3这是一个方便的快捷方式 :- use_module(library(clpfd)). % simulate domain/3 in SWI-prolog
[A1、A2、A3]
和[B1、B2、B3]
,我该怎么做
创建一个列表[R1,R2,R3]
,其中Ri
是1
如果Ai=Bi
如果不是0
这是“方案”的基础
您应该“具体化”您的条件,张贴表单的约束条件 具体化(A、B、C):- C#A#=B 在变量对之间。地图列表/3这是一个方便的快捷方式
:- use_module(library(clpfd)).
% simulate domain/3 in SWI-prolog
domain(Vs,L,H) :- Vs ins L..H.
reify(A,B,C) :-
C #<==> A #= B.
main(A,B,C) :-
length(A,3),
domain(A,1,3),
all_different(A),
length(B,3),
domain(B,1,3),
all_different(B),
maplist(reify, A,B,C),
labeling([],A),
labeling([],B).
:- use_module(library(clpfd)).
% simulate domain/3 in SWI-prolog
domain(Vs,L,H) :- Vs ins L..H.
reify(A,B,C) :-
C #<==> A #= B.
main(A,B,C) :-
length(A,3),
domain(A,1,3),
all_different(A),
length(B,3),
domain(B,1,3),
all_different(B),
maplist(reify, A,B,C),
labeling([],A),
labeling([],B).
1 ?- main(A,B,C).
A = B, B = [1, 2, 3],
C = [1, 1, 1] ;
A = [1, 2, 3],
B = [1, 3, 2],
C = [1, 0, 0] ;
A = [1, 2, 3],
B = [2, 1, 3],
C = [0, 0, 1]
etc ....