开发者

Counting Prolog Database

开发者 https://www.devze.com 2023-02-20 22:34 出处:网络
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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消