How can I check if a query gives me a speci开发者_如何转开发fic number of results.
So for example, I want to retrieve a car model of a car that has only been on three specific races.
The car predicate is : car(race#, name).
% Car has been on N races
car_with_n_races(Car, N) :-
length(Races, N),
findall(Race, car(Race, Car), Races).
Use setof
instead of findall
if car/2
may return duplicate results.
(You can swap the calls to length
and findall
, but I think this order may be more efficient.)
car_with_n_races(Car, N) :- count(race(Race,Car),N).
count(P,Count) :- findall(1,P,L), length(L,Count).
% sorry!
car_with_n_races(Car,N) :- count(car(Race,Car),N).
count(P,Count) :- findall(1,P,L), length(L,Count).
精彩评论