我開始學習Prolog,並且我的代碼遇到編譯器錯誤。我試圖編寫一些代碼來檢查一個家庭是否處於貧困狀態,如果他們符合某些條件。最後一行的幾行是貧窮條件,這就是我得到Opertator Expected
錯誤的地方。我試圖說,鑑於家庭身份證,如果這個家庭的規模是一個,而收入低於11170,那麼這個家庭就處於貧困狀態。而對於規模大於8歲的家庭來說,每增加一個家庭成員,貧困程度爲38890加上3960。我怎樣才能糾正這些錯誤? family_in_poverty
應返回true
或false
。Prolog - 獲取語法錯誤 - 運算符期望
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.
即使我擺脫'is' – user906153