This is supposed to be a definition of a function which returns t if the entered value is an even natural number and returns nil if the entered value is an odd natural number:
(defun evec (n)
(if (zp n)
(if (zerop n)
t
nil)
(evec (- n 2))))
It correctly returns t when I enter an even number and It's supposed to return nil literally for anything else but it doesn't! Actually I guess it does not halt and I don't understand why. When I enter a number like -1 which is obviously not an even natural number, I expect from this code to go through first 'if' because (zp -1) equals t and since -1 is not equal to 0 then (zerop -1) should be nil which means that the output should be nil and the program should terminate.
In fact I know how to implement this function better in way that it actually work and all I need to know is why this code doesn't work, in order to understand this language better because I've just started learning ACL2!...
Thank you for your consideration.
(evec (- n 2))
is called when(zp n)
isnil
. I take it thatzp
returnst
for integers, andnil
otherwise. Whenn
is an integer, if it is zero, thent
is returned, otherwisenil
is returned. So any even number other than zero should returnnil
, as should any other integer. This does not match your description, so this must not be the right code. That said, calling(evec (- n 2))
recursively will skip over 0 for positive odd numbers, and all negative numbers will grow in a negative sense, never terminating.evenp
, but for natural numbers only. Well, what is it supposed to do when it gets something other than a natural number? Does(evec -1)
produce an error, or does it just report false? What are the exact test cases?