Prolog sum & sub using successors?

Int2) True if Int2 = Int1+ 1 and Int1>=0. At least one of the arguments must be instantiated to a natural number. This predicate raises the domain-error not_less_than_zero if called with a negative integer.

E.g. Succ(X, 0) fails silently and succ(X, -1) raises a domain-error Given that, we can define add like this: add(0, Y, Y). Add(X, Y, Z) :- succ(PredX, X), add(PredX, Y, PredZ), succ(PredZ, Z) And subtract like this: subtract(X, 0, X).

Subtract(X, Y, Z) :- succ(PredY, Y), succ(PredX, X), subtract(PredX, PredY, Z) Note that neither one of these will handle negative numbers (because succ doesn't), and therefore I haven't bothered to make subtract function when Y > X EDIT: Here's a version of add and subtract that work on any instantiation pattern. I still didn't bother with type-checking (as mentioned by Kaarel in the comments), or negative numbers add(0, 0, 0). Add(0, Y, Y).

Add(X, 0, X). Add(X, Y, Z) :- nonvar(X), succ(PredX, X), (nonvar(Z) -> succ(PredZ, Z), add(PredX, Y, PredZ) ; add(PredX, Y, PredZ), succ(PredZ, Z) ). Add(X, Y, Z) :- nonvar(Y), add(Y, X, Z).

Subtract(0, 0, 0). Subtract(X, 0, X). Subtract(X, X, 0).

Subtract(X, Y, Z) :- add(Y, Z, X).

First, you need to have a predicate succ. This is how SWI-Prolog defines it: succ(?Int1,? Int2) True if Int2 = Int1+ 1 and Int1>=0.At least one of the arguments must be instantiated to a natural number.

This predicate raises the domain-error not_less_than_zero if called with a negative integer. E.g. Succ(X, 0) fails silently and succ(X, -1) raises a domain-error.

Given that, we can define add like this: add(0, Y, Y). Add(X, Y, Z) :- succ(PredX, X), add(PredX, Y, PredZ), succ(PredZ, Z). And subtract like this: subtract(X, 0, X).

Subtract(X, Y, Z) :- succ(PredY, Y), succ(PredX, X), subtract(PredX, PredY, Z). Note that neither one of these will handle negative numbers (because succ doesn't), and therefore I haven't bothered to make subtract function when Y > X. EDIT: Here's a version of add and subtract that work on any instantiation pattern.

I still didn't bother with type-checking (as mentioned by Kaarel in the comments), or negative numbers. Add(0, 0, 0). Add(0, Y, Y).

Add(X, 0, X). Add(X, Y, Z) :- nonvar(X), succ(PredX, X), (nonvar(Z) -> succ(PredZ, Z), add(PredX, Y, PredZ) ; add(PredX, Y, PredZ), succ(PredZ, Z) ). Add(X, Y, Z) :- nonvar(Y), add(Y, X, Z).

Subtract(0, 0, 0). Subtract(X, 0, X). Subtract(X, X, 0).

Subtract(X, Y, Z) :- add(Y, Z, X).

Note that the instantiation patterns add(-, +, +) and add(+, -, +) do not work under this definition. Also, you could cut after "add(0, Y, Y)", and check the input type, currently:? - add(0, blah, R).

R = blah ; false. – Kaarel Apr 29 '09 at 15:33 Actually, add(+, -, +) does work because succ is never called on Y. However, see my edit.

– Pesto Apr 29 '09 at 17:35? - add(2, X, 3). ERROR: succ/2: Arguments are not sufficiently instantiated – Kaarel Apr 29 '09 at 18:01.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions