I have written the following predicate exactly/2
that succeeds if exactly N
of the numbers in the list L
are equal to 1:
:- pred exactly(int, list(int)).
:- mode exactly(in, in) is semidet.
:- mode exactly(out, in) is det.
exactly(N, L) :-
length(filter(pred(X::in) is semidet :- (X = 1), L), N).
For example, the call exactly(X, [1, 0, 0, 1, 0, 1])
will bind X
to 3
.
I want to create something similar, but for predicates: I want to write a predicate that succeeds if exactly N
goals from the list L
are successful.
For example, the call exactly(X, [true, false, member(1, [1, 2, 3]), member(0, [1, 2, 3])])
should bind X
to 2
.
Actually, this can be done, as shown by this code:
The predicate list_member tells the compiler which mode of list.member you want to call. As for true and false, you would need to define predicates of those names as well. (By default, they exist as goals only.)