简体   繁体   English

prolog 谓词以连续表示法添加 2 个数字并在 Z 中也以连续表示法获得结果

[英]prolog predicate that adds 2 numbers in successive notation and getting the result in Z also in successive notation

add(0,Y,Y).
add(s(X),Y,Z):-
     add(X,Y,Z1),
    Z is s(Z1).

why always resulting in false why is the compiler telling me Arithmetic: 's(_2676)' is not a function为什么总是导致错误为什么编译器告诉我算术:'s(_2676)' is not a function

Your are confusing predicate calls with compound terms.您将谓词调用与复合术语混淆了。 You have not defined s/1 predicate.您尚未定义s/1谓词。 So the compiler throws an error.所以编译器会抛出一个错误。 is is not a generic assignment, it is a arithmetic evaluator. is不是通用赋值,它是一个算术评估器。 You should use = instead.你应该使用=代替。

The following should work.以下应该工作。 Here when I write s(X) , this is not a predicate call but a compound term.在这里,当我写s(X)时,这不是谓词调用,而是复合词。

add(0, X, X).
add(s(X), Y, Z) :- Z = s(Z1), add(X, Y, Z1).

or this或这个

add(0, X, X).
add(s(X), Y, s(Z)) :- add(X, Y, Z).
?- add(s(s(s(0))), s(s(0)), X).
X = s(s(s(s(s(0))))).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM