I am a total novice to Prolog and still am getting used to its logic.
I have a task to create functions that turn a natural number into an "S-number" and vice versa.
So Number 0 would be 0
.
Number 1 would be s(0)
.
Number 2 would be s(s(0))
.
And so on.
nat(0).
nat(s(0)):- nat(X).
The function for finding a natural number for a given S-number I already have:
s2nat(0, 0) :- !.
s2nat(s(S),Number) :- s2nat(S,NewNumber), Number is NewNumber + 1.
So ?- s2nat(s(s(s(0))), X)
would give X=3
as an output.
Now I need to write a function that does the opposite: returns an S-number for a given natural number.
And here I am stuck. I don't quite understand how to write a condition so that Prolog understands that I need s(S)
to be returned. The condition Number is NewNumber + 1
doesn't work anymore to go back and get to a "0".
Do you have any advice?