I am receiving the following error in my Lisp code:
value (PROBLEM1 (+ N 1)) is not of the expected type NUMBER.
The intent of the code is to sum all numbers up to 1000 that are divisible by 3 or 5.
(defun problem1 (n)
(if (< n 1000)
(if (or (= 0 (mod n 3)) (= 0 (mod n 5)))
(apply '+ '(n (problem1 (+ n 1))))
(apply '+ '(0 (problem1 (+ n 1)))))
0))
I realize that the error is probably because problem1 is returning a list – but when I trace the values in my head the function should work correctly. Therefore, can someone explain to me if I am misusing (or missing) an apostrophe?
Rather than missing a
', you have too many. When you writeYou are trying to apply
+to a list containing the symbolnand the unevaluated list(problem1 (+ n 1)). This is because you have quoted the argument list, and'prevents evaluation. What you actually want is to uselist, e.g.