【发布时间】:2020-10-07 20:35:30
【问题描述】:
我想从 mondial 的现有谓词中编写一个谓词。我感兴趣的谓词是 cc(Country) 表示 Country 是这样一个国家,即该国家有一个城市,该城市的人口至少占该国总人口的 75%。有些国家的人口未知,因此该值为空。
给定谓词的示例:
% city(N,C,R,Pop) is a city by name N in country C in region R with
% population Pop
city('Aachen',germany,'Nordrhein Westfalen',247113).
city('Aalborg',denmark,'Nordjylland',113865).
city('Aarau',switzerland,'Aargau',null).
city('Aarhus',denmark,'Midtjylland',194345).
city('Abaetetuba',brazil,'Para',106753).
city('Abakaliki',nigeria,'Ebonyi',null).
city('Abakan',russia,'Rep. of Khakassiya',161000).
city('Abancay',peru,'Apurimac',null).
city('Aba',nigeria,'Abia',500183).
city('Abengourou',cote_divoire,'Moyen-Comoe',null).
city('Abeokuta',nigeria,'Ogun',352735).
city('Aberdeen',united_kingdom,'Grampian',219100).
city('Aberystwyth',united_kingdom,'Ceredigion',null).
city('Abha',saudi_arabia,'Aseer',null).
city('Abidjan',cote_divoire,'Lagunes',null).
city('Abilene',united_states,'Texas',106707).
city('Aboisso',cote_divoire,'Sud-Comoe',null).
city('Abu Dhabi',united_arab_emirates,'Abu Dhabi',363432).
city('Abuja',nigeria,'Abuja',107069).
city('Acapulco',mexico,'Guerrero',515374).
city('Acarigua',venezuela,'Portuguesa',116551).
city('Accra',ghana,'Greater Accra',867459).
city('Acheng',china,'Heilongjiang',197595).
city('Achinsk',russia,'Krasnoyarskiy kray',123000).
city('Adama/Nazret',ethiopia,'Oromia',127842).
city('Adamstown',pitcairn_islands,'Pitcairn Islands',null).
city('Adana',turkey,'Adana',1047300).
city('Adapazari',turkey,'Sakarya',186000).
city('ad Damir',sudan,'ash Shamaliyah',null).
city('Ad Dammam',saudi_arabia,'Ash Sharqiyah',744321).
city('Addis Ababa',ethiopia,'Addis Ababa',2084588).
city('Adelaide',australia,'South Australia',1050000).
city('Aden',yemen,'Yemen',550744).
city('Adiyaman',turkey,'Adiyaman',128000).
city('Ado-Ekiti',nigeria,'Ekiti',156122).
我完成这个任务的逻辑:
pop(Country,Pop) :-
city(_,C,_,Pop),
Country = C.
setof(P,pop(germany,P), poplist). %intend to create a list of population from a specific country
poplist([]).
poplist([_|T]) :- poplist(T). %poplist should be the list with population from each city of germany
%I wanted to sum the entire list, so i get the total population of the germany
sum([], 0).
sum([H|T], N):-
sum(T, X),
N is X + H.
%once I get the total population I wanted to iteratively execute and check with the population for a specific city in the country is > 75% so to get the country listed in the output
cc(Country):-
%get the city for specific country
city(_, Country, _, Pop),
%calculate the percentage
Perc is div(Pop, sum(poplist, N)),
%see if percentage is over 75%
Perc >= 75.
看起来我的代码不起作用,它在创建 sum(poplist, N) 甚至在 cc(Country) 谓词定义中都中断了。
我是 prolog 的新手,在这里需要我们的帮助。我的算法或逻辑对我想要完成的任务有意义吗?我怎样才能得到想要的结果?
【问题讨论】:
标签: prolog predicate logic-programming