Question

I don't know how can I achieve the following:

I want to count the number of times a certain condition (whose values are unknown) is met.

For instance, if I have the lists [A1,A2,A3] and [B1,B2,B3], how can I create a list [R1,R2,R3] where Ri is 1 if Ai=Bi and 0 if not.

This is the basis of the "program".

:- use_module(library(clpfd)).

main(A,B) :-
    length(A,3),
    domain(A,1,3),
    all_different(A),
    length(B,3),
    domain(B,1,3),
    all_different(B),

    append(A,B,L),
    labeling([],L).
Était-ce utile?

La solution

you should 'reify' your conditions, posting constraints of the form

reify(A,B,C) :- C #<==> A #= B.

between pairs of variables. maplist/3 it's an handy shortcut

:- 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).

yields

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 ....
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top