John
John

Reputation: 4543

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,doctor).
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 .

Upvotes: 0

Views: 808

Answers (1)

Ax.
Ax.

Reputation: 687

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.

?-

Upvotes: 5

Related Questions