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

102 views Asked by At
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

1

There are 1 answers

0
rajashekar On

Your are confusing predicate calls with compound terms. You have not defined s/1 predicate. So the compiler throws an error. is is not a generic assignment, it is a arithmetic evaluator. You should use = instead.

The following should work. Here when I write s(X), this is not a predicate call but a compound term.

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