简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM