我开始学习 Prolog,我的代码遇到了编译器错误。我正在尝试编写一些代码来检查满足某些条件的家庭是否处于贫困之中。最后一行几行是贫困条件,这就是我得到Opertator Expected
错误的地方。我想说的是,给定家庭ID,如果那个家庭的人数是一个,并且收入低于11170,那么这个家庭是贫困的。对于一个人数大于 8 的家庭,贫困水平为 38890 加上每增加一个家庭成员 3960。我该如何纠正这些错误?family_in_poverty
应该返回true
或false
。
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.