2011-05-30 51 views
5

我想學習SWI序言, 但我簡單的程序失敗,當我相信它應該成功。序言,使用表達式

%My code: 
orthogonal((X1,Y1,Z1),(X2,Y2,Z2)) :- (X1*X2)+(Y1*Y2)+(Z1*Z2)==0. 
integerVector((X,Y,Z)) :- integer(X),integer(Y),integer(Z). 

?-orthogonal((1,0,0),(0,0,1)). 

我按在pseudoemacs窗口編譯緩衝器和輸出是:

% [PATH].pl compiled 0.00 sec, 136 bytes 
ERROR: emacs_prolog_mode ->error_at_location: Argument 1 (int): `int' expected, found `@11470948?start' 
ERROR: emacs_prolog_mode ->error_at_location: Argument 1 (int): `int' expected, found `@11470948?start' 
Warning: [PATH]s.pl:5: 
     Goal (directive) failed: user:orthogonal((1,0,0), (0,0,1)) 

回答

6

您已經到位的(=:=)/2進行參數賦值的算術表達式中使用(==)/2

您可以使用(X,Y,Z),但它不是三倍於例如。哈斯克爾。要看到這個:

?- write_canonical((1,2,3)). 
','(1,','(2,3)) 

?- (1,2,3) = (X,Y). 
X = 1, 
Y = (2,3). 
+1

我應該用什麼來代替三元組? – 2011-05-30 04:53:17

+2

@Oxinabox:如果你所有的矢量都是3d,可以使用列表或者函數v/3。 – 2011-05-30 06:20:03

5

Prolog中的表達式只是表示句法術語樹。要評估一個表達式,您需要使用X is Y來計算Y作爲算術表達式,並將結果與​​X進行統一。或者,您可以使用X =:= Y,它將X和Y都計算爲算術表達式,然後統一結果。

乾杯!