Issue
I need to write a recursive function that can add two numbers (x, y), assuming y is not negative. I need to do it using two functions which return x-1 and x+1, and I can’t use + or – anywhere in the code. I have no idea how to start, any hints?
Solution
Lets say that
succ(x)=x+1
pre(x)=x-1
Then, (in pseudocode)
add(x,y) = {
If(y==0) Return x;
Return add(succ(x),pre(y))
}
Observe, this only works for non-negative y.
Answered By – Per Alexandersson
Answer Checked By – Robin (BugsFixing Admin)