Prolog - Getting Syntax Error - Operator Expected
Solution 1
Prolog doesn't use is <=
for a numeric comparison, just =<
for less-than-or-equal.
When you use the is
keyword, this is an infix predicate is/2 that evaluates the right-hand side as a numeric expression and unifies the result with the left-hand side.
Solution 2
Not sure if this will help, but try breaking it down a bit further ie. one line for the calculation, another for the comparison:
family_in_poverty(FamilyID) :-
householdSize(FamilyID, Y),
householdIncome(ID, X),
M is 38890 + (Y - 8)*3960,
X =< M,
Y > 8.

user906153
Updated on June 04, 2022Comments
-
user906153 7 months
I'm starting to learn Prolog, and I'm running into a compiler error with my code. I'm trying to write some code that will check if a family is in poverty if they meet certain conditions. The last line couple of lines are the poverty conditions, and that's where I'm getting the
Opertator Expected
error. I'm trying to say that given the family ID, if the size of that family is one, and the income is less than 11170, then that family is in poverty. And for a family of size >8 the poverty level is 38890 plus 3960 for every additional family member. How can I correct these errors?family_in_poverty
should be returningtrue
orfalse
.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.
-
user906153 over 10 yearsI'm still getting the same error message on the same lines as before, even after I get rid of 'is'
-
magus over 10 yearsSo which line of the above is it failing on ?
-
magus over 10 yearsie. suspect your symbols are the wrong way around. From swi-prolog.org/man/arith.html
-
false over 10 years@magus: I don't get it: Did you write
<=
on purpose in your answer? If not, please edit it! It's Prolog, we have=<
for comparison. To us,<=
looks like some implication and not like a comparison. -
magus over 10 years@false, I didn't spot the problem (incorrect operator) when I put in the first solution. I have corrected the code now.
-
CapelliC about 9 yearsI think the rule-of-thumb should be 'avoid arrow like shapes'. That because arrows are useful to express logical properties.