开发者

Prolog, using expressions

I'm trying to learn SWI prolog, but my simple program fails when I believe it should succeed.

%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)).

I press compile Buffer in the pseudoemacs window and the output is:

% [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))


You have used (==)/2 in place of (=:=)/2 which evaluates its arguments as arithmetic expressions.

You can use (X,Y,Z), but it isn't a triple as in e.g. Haskell. To see this:

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

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


Expressions in Prolog simply represent syntactic term trees. To evaluate an expression you need to X is Y which evaluates Y as an arithmetic expression and unifies the result with X. Alternatively you can use X =:= Y which evaluates both X and Y as arithmetic expressions, then unifies the results.

Cheers!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜