views:

65

answers:

1
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 .
+4  A: 

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.

?-
Ax
if i do that i get an error saying ERROR: toplevel: Undefined procedure:
John
My prolog is more than a bit rusty, but I think this is the correct answer.
NealB
This answer is correct - SWI-PROLOG is trying to parse the `4WD` bit as a number (because it starts with `4`), but fails because of the `WD` suffix. Using the atom `fourwd` fixes the problem. You could also have used the atom `'4WD'` instead (i.e., put single quotes around `4WD` and SWI-PROLOG would treat it as an atom :-)
sharky