gpt4 book ai didi

Prolog - 获取语法错误 - 预期的运算符(operator)

转载 作者:行者123 更新时间:2023-12-01 11:52:14 24 4
gpt4 key购买 nike

我开始学习 Prolog,但我的代码遇到了编译器错误。我正在尝试编写一些代码来检查满足特定条件的家庭是否处于贫困状态。最后一行是贫困状况,这就是我收到 Opertator Expected 错误的地方。我想说的是,给定家庭ID,如果那个家庭的人数是一个,并且收入低于11170,那么这个家庭就处于贫困状态。对于一个大于 8 人的家庭,贫困线是 38890,每增加一个家庭成员就增加 3960。我该如何纠正这些错误? family_in_poverty 应该返回 truefalse

family(10392,
person(tom, fox, born(7, may, 1960), works(cnn, 152000)),
person(ann, fox, born(19, april, 1961), works(nyu, 65000)),
% here are the children...
[person(pat, fox, born(5, october, 1983), unemployed),
person(jim, fox, born(1, june, 1986), unemployed),
person(amy, fox, born(17, december, 1990), unemployed)]).

family(38463,
person(susan, rothchild, born(13, september, 1972), works(osu, 75000)),
person(jess, rothchild, born(20, july, 1975), works(nationwide, 123500)),
% here are the children...
[person(ace, rothchild, born(2, january, 2010), unemployed)]).

married(FirstName1, LastName1, FirstName2, LastName2) :-
family(_, person(FirstName1, LastName1, _, _),
person(FirstName2, LastName2, _, _), _).

married(FirstName1, LastName1, FirstName2, LastName2) :-
family(_, person(FirstName2, LastName2, _, _),
person(FirstName1, LastName1, _, _), _).

householdIncome(ID, Income) :-
family(ID, person(_, _, _, works(_, Income1)),
person(_, _, _, works(_, Income2)), _),
Income is Income1 + Income2.

exists(Person) :- family(_, Person, _, _).
exists(Person) :- family(_, _, Person, _).
exists(Person) :- family(_, _, _, Children), member(Person, Children).

householdSize(ID, Size) :-
family(ID, _, _, Children),
length(Children, ChildrenCount),
Size is 2 + ChildrenCount.

:- use_module(library(lists)). % load lists library for sumlist predicate

average(List, Avg) :-
sumlist(List, Sum),
length(List, N),
Avg is Sum / N.

family_in_poverty(FamilyID) :- householdSize(FamilyID, 1), householdIncome(ID, X), X <= 11170.
family_in_poverty(FamilyID) :- householdSize(FamilyID, 2), householdIncome(ID, X), X <= 15130.
........
family_in_poverty(FamilyID) :- householdSize(FamilyID, Y), householdIncome(ID, X), X <= 38890 + (Y - 8)*3960, Y > 8.

最佳答案

Prolog 不使用 is <=对于数字比较,只需 =<对于小于或等于。

当您使用 is关键字,这是一个中缀谓词 is/2,它将右侧计算为数值表达式并将结果与​​左侧统一。

关于Prolog - 获取语法错误 - 预期的运算符(operator),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10375621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com