Reputation: 1
When I was trying to check the number is equal to any element in list, I get the following error:
'NIL' is not of the expected type 'NUMBER'
(defun check-num (li n)
(cond
((= n (car li)) t)
((not (= n (car li))) (check-num (cdr li) n))
((not (= n (car li))) nil)))
(check-num '(1 2 3 4 5) 6)
I found that the problem occur when it try to excute the function 'check-num' with the empty list with the number entered after perform all checking.
The error come out when the compiler try to perform the following function:
(check-num nil 6)
Upvotes: 0
Views: 1286
Reputation: 51521
The immediate problem is that you never check for the end of the list. The car
of the empty list is nil
again. =
compares numbers. Nil
is not a number.
Check for the end of the list first. Some minor stylistic issues: when using conses as a list, use first
and rest
instead of car
and cdr
, name things as they are (list
), use proper formatting.
(defun check-num (list n)
(cond ((endp list) nil) ; <-
((= (first list) n) t)
(t (check-num (rest list) n))))
However, it should be noted that there are several ways already in the standard to achieve this check, e. g.:
(member n list)
(find n list)
Upvotes: 2