微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过使用算术运算基于谓词属性进行过滤来从其他谓词写谓词

如何解决通过使用算术运算基于谓词属性进行过滤来从其他谓词写谓词

我想从mondial的现有谓词中写一个谓词。我感兴趣的谓词是cc(国家/地区),说国家/地区是一个国家,因此该国有一个城市,其人口至少占该国总人口的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','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','Abia',500183).
city('Abengourou',cote_divoire,'Moyen-Comoe',null).
city('Abeokuta','Ogun',352735).
city('Aberdeen',united_kingdom,'Grampian',219100).
city('Aberystwyth','Ceredigion',null).
city('Abha',saudi_arabia,'Aseer',null).
city('Abidjan','Lagunes',null).
city('Abilene',united_states,'Texas',106707).
city('Aboisso','Sud-Comoe',null).
city('Abu Dhabi',united_arab_emirates,'Abu Dhabi',363432).
city('Abuja','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','Krasnoyarskiy kray',123000).
city('Adama/Nazret',ethiopia,'Oromia',127842).
city('Adamstown',pitcairn_islands,'Pitcairn Islands',null).
city('Adana',turkey,'Adana',1047300).
city('Adapazari','Sakarya',186000).
city('ad damir',sudan,'ash Shamaliyah',null).
city('Ad dammam','Ash Sharqiyah',744321).
city('Addis Ababa','Addis Ababa',2084588).
city('Adelaide',australia,'South Australia',1050000).
city('Aden',yemen,'Yemen',550744).
city('Adiyaman','Adiyaman',128000).
city('Ado-Ekiti','Ekiti',156122).

我完成这项任务的逻辑:

pop(Country,Pop) :-
    city(_,_,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,%calculate the percentage
Perc is div(Pop,sum(poplist,N)),%see if percentage is over 75%
Perc >= 75.

看起来我的代码无法正常工作,并且在创建sum(poplist,N)甚至在cc(Country)谓词定义中都中断了。

我是序幕的新手,在这里需要我们的帮助。我的算法或逻辑对我想完成的任务有意义吗?如何获得理想的结果?

解决方法

您的代码中存在多个问题:

此行仅定义了一个新事实-仅此而已。

Dim conn As ADODB.Connection
Dim comm As ADODB.Command
Set conn = New ADODB.Connection
Set comm = New ADODB.Command

conn.ConnectionString = "Provider=Microsoft.Jet.OleDB.4.0;Extended Properties=dBASE IV;Data Source=C:\Temp"
comm.CommandText = "CREATE TABLE 201008 ([FirstName] String(50),[LastName] String(50))"
With conn
    .Open
    With comm
        .ActiveConnection = conn
        .Execute
    End With
End With

conn.Close
Set conn = Nothing
Set comm = Nothing

以类似的方式,您对poplist的定义对我来说没有意义:它仅声明空列表是一个弹出列表,如果T是一个弹出列表,则列表[_ | T]是一个弹出列表。

您对总和的定义似乎很好,但是SWI-Prolog为此(总列表)提供了内置谓词。

setof(P,pop(germany,P),poplist). %intend to create a list of population from a specific country

看起来不对; sum是一个谓词;弹出列表是一个谓词。

此外,数据集中有“ null”个元素。请注意,null只是Prolog中的另一个原子,您不能对包含非算术原子的列表求和。

这是我的尚未经过测试的解决方案:

Perc is div(Pop,sum(poplist,N)),

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?