Search code examples
recursionerror-handlinglispfactorial

Simple LISP code won't catch input errors


I am writing a very simple lisp code for the recursive factorial function. When called using a number, it works fine. However, when I try to call it using something that is not a number (ex. a) I get the following error:

Error: Attempt to take the value of the unbound variable `A'. [condition type: UNBOUND-VARIABLE]

However, this is supposed to be caught in my code. Here is my code:

(defun FactorialRec (num)
   (cond                                                  
      ((not(numberp num))
         (princ "Argument must be a number.")
         (terpri)
         ())
      ((equal num 1) ;base case                                                       
         1)
      ((<= 1 num) (* num(FactorialRec (- num 1))))
   )
)

I do not know why the numberp is not catching this. Any ideas? Thanks.


Solution

  • Break 2 [6]> (FactorialRec "A")
    Argument must be a number.
    

    It works on my machine. So I think you were passing A but not "A"?