I have a quick question I've been trying to figure out in Prolog. Is there any way to check for at least X failures (or passes) in a predicate?
For example, here I could check to see if one of the scores is less than or equal to 20 but I'm trying to check all of them at the same time where at least one is less than or equal to 20, without having to specify 3 different predicates checking the first score, then second and then third separately.
scores(score1, 14, 60, 45).
# Checks to see if at least one of the scores is less than or equal to 20
at_least_one_fail(X):- scores(X, Y), Y > 20, scores(X, Z), Z > 20, scores(X, J), J =< 20.
at_least_one_fail(X):- scores(X, Y), Y > 20, scores(X, Z), Z =< 20, scores(X, J), J > 20.
at_least_one_fail(X):- scores(X, Y), Y =< 20, scores(X, Z), Z > 20, scores(X, J), J > 20.
(Really bad code example but hopefully it gets the point of my question across).
Any thoughts appreciated, thank you.
Here is a ballpark answer using facts and findall/3
score(14).
score(60).
score(45).
test(Scores) :-
findall(Score,(score(Score), Score > 20),Scores).
Example run
?- test(Scores).
Scores = [60, 45].
Another way using a list and partition/4
partition_predicate(X) :-
X > 20.
test_2(Greater,Less) :-
List = [14,60,45],
partition(partition_predicate ,List,Greater,Less).
Example run
?- test_2(Greater,Less).
Greater = [60, 45],
Less = [14].