Search code examples
prologreturnpermutation

Prolog return certain permutations


i have a predicate schedule(A,B,C) that returns possible permutations at lists A,B,C with backtracking

?- schedule(A,B,C).

A = [im204,212,217]
B = [im209,214,218]
C = [im210,216] ? ;

A = [im204,212,218]
B = [im209,214,217]
C = [im210,216] ? ;

A = [im204,212,216]
B = [im209,214,218]
C = [im210,217] ?

I also have the predicate schedule_errors(A,B,C,E) which returns errors (dont mind what the erros are) from lists A,B,C to E.

?- schedule_errors([im204, im209, im210], [im212, im214, im217], [im216, im218]).
E = 4 ?

In my new predicate

schedule_all_errors(A,B,C,E):-
   schedule(A,B,C),
   schedule_errors(A,B,C,E).

it returns possible permutations along with the error number

?- schedule_all_errors(A,B,C,E).

A = [im204,212,217]
B = [im209,214,218]
C = [im210,216]
E = 14 ? ;

A = [im204,212,218]
B = [im209,214,217]
C = [im210,216]
E = 6 ? ;

A = [im204,212,216]
B = [im209,214,218]
C = [im210,217]
E = 12 ?

I was wondering if there was a way i can return only the permutations with zero errors. (or not return any permutations whose errors are different than 0)


Solution

  • Just add this condition in the end

    schedule_all_errors_1(A,B,C):-
       schedule(A,B,C),
       schedule_errors(A,B,C,E),
       E = 0.
    

    or

    schedule_all_errors_1(A,B,C):-
       schedule(A,B,C),
       schedule_errors(A,B,C,0).
    

    Whether the second one will work depends on the definition of schedule_errors.