Invalid function mod within lisp to recursively add sum positive integers that are multiples of certain numbers

StackOverflow https://stackoverflow.com/questions/20537039

Question

I am trying to write function that sums all positive integers less than or equal to 200 and are multiples of 6 and 7.

What I have is the following:

(defun sumFunction(current sum)
    (if (/= current 200)
        (if ((eq (mod current 6) 0) or (eq (mod current 7) 0))
            (setf sum (+ sum current))
            (sumFunction (+ current 1) sum)
        )
        (sumFunction ((+ current 1) sum)
    )
)

It is giving me the following error:

Error handler called recursively (:INVALID-FUNCTION NIL IF "" "~S is invalid as a function." (EQ (MOD CURRENT 3) 0))

I am unsure of why there is any errors.

If I follow the logic it should return the result I need.

Any help is much appreciated! Thanks

Was it helpful?

Solution

There are two syntax errors in your code, and there are also some other issues which do not match Lisp style. Please refer to the corrected code below.

(defun sumFunction(current sum)
  (if (/= current 200)
      (if (or (eq (mod current 6) 0) (eq (mod current 7) 0))
          (sumFunction (+ current 1) (+ current sum))
        (sumFunction (+ current 1) sum))
    sum))

Here is the result.

(sumFunction 20 0)
;=> 5731
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top