How to know if a Peano number is even

717 Views Asked by At

So I'm having a hard time trying with Peano and I need some help. I want to know if a Peano number is even and if yes then add:

0 + s(s(0)) = s(s(0))
0 + s(0) = No because one of the numbers odd

The code I have so far:

s(0).
s(X):- 
    X.

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

There are 1 best solutions below

1
On BEST ANSWER

Do not think about Peano numbers as numbers but as symbols.

Realize that the even Paeno numbers are 0 and a repeat of the pattern s(s(X)) where X can be 0 or the pattern s(s(X))

Also I look at 0 and s(0) etc. as data, and you are using s as a predicate name. I am not saying it will not work this way, but that is not how I think about this.

The name of the predicate is paeno_even and it takes one argument.

The base case is

paeno_even(0).

next for recursive case

paeno_even(P)

and the processing on P just removes s(s(X)) so do that in the head as

paeno_even(s(s(X)))

and then just do the recursive call

paeno_even(s(s(X))) :-
    paeno_even(X).

A few test to demonstrate:

?- paeno_even(0).
true.

?- paeno_even(s(0)).
false.

?- paeno_even(s(s(0))).
true.

?- paeno_even(s(s(s(0)))).
false.

The entire code as one snippet:

paeno_even(0).
paeno_even(s(s(X))) :-
    paeno_even(X).