I wrote a function for recursive subtraction in lisp, but when i load it and run the function with numbers for example ( subtract 4 3 )
the output is -3
which I do not understand why.
(defun subtract (x y)
(if (eq y 0)
x
(- 1 (subtract x (- y 1) ) ) ) )
Here's a fixed version of your code:
Notice that in the recursive call we subtract 1 from both
x
(to actually decrement the number) andy
(to get it closer to the base case, 0).