Problems with prolog code
Predicates
is_a(X,Y) X is a doctor/handyman
drives(X,Y) X drives Y
We are given that a doctor drives a sportscar and a handyman drives a 4WD
is_a(john,docto开发者_高级运维r).
is_a(david,handyman).
Now i want the code decide what kind of car john/david are driving. I tried doing:
drives(X,sportscar) :- is_a(X,doctor).
drives(Y,4WD) :- is_a(Y,handyman).
What am i doing wrong?
?- drives(john,fourwd).
true .
?- drives(john,sportscar).
true .
?- drives(david,fourwd).
true .
?- drives(david,sportscar).
true .
My prolog is a bit rusty, but my interpreter doesn't like your line
drives(Y,4WD) :- is_a(Y,handyman)
It complains ERROR: c:/test.pl:4:0: Syntax error: Illegal number
I switched it to
drives(Y,fourwd) :- is_a(Y,handyman)
and it seems to work fine.
?- drives(X,Y).
X = john,
Y = sportscar ;
X = david,
Y = fourwd.
?-
精彩评论