prolog代码的问题

Joh*_*ohn 0 prolog

Predicates
is_a(X,Y)      X is a doctor/handyman
drives(X,Y)    X drives Y
Run Code Online (Sandbox Code Playgroud)

我们得到医生驾驶跑车和勤杂工驾驶4WD

is_a(john,doctor).
is_a(david,handyman).
Run Code Online (Sandbox Code Playgroud)

现在我想要代码决定约翰/大卫驾驶什么样的车.我试过做:

drives(X,sportscar) :- is_a(X,doctor).
drives(Y,4WD) :- is_a(Y,handyman).
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

?- drives(john,fourwd).
true .

?- drives(john,sportscar).
true .

?- drives(david,fourwd).
true .

?- drives(david,sportscar).
true .
Run Code Online (Sandbox Code Playgroud)

Ax.*_*Ax. 5

我的序言有点生疏,但我的口译员不喜欢你的路线

drives(Y,4WD) :- is_a(Y,handyman)
Run Code Online (Sandbox Code Playgroud)

它抱怨 ERROR: c:/test.pl:4:0: Syntax error: Illegal number

我把它换成了

drives(Y,fourwd) :- is_a(Y,handyman)
Run Code Online (Sandbox Code Playgroud)

它似乎工作正常.

?- drives(X,Y).
X = john,
Y = sportscar ;
X = david,
Y = fourwd.

?-
Run Code Online (Sandbox Code Playgroud)

  • 这个答案是正确的 - SWI-PROLOG试图将`4WD'位解析为一个数字(因为它以`4`开头),但由于`WD`后缀而失败.使用原子`fourwd`解决了这个问题.您也可以使用原子''4WD'来代替(即,将单引号放在`4WD`周围,SWI-PROLOG将其视为原子:-) (3认同)