B. Pym
2024-09-21 13:29:23 UTC
(defmacro sum (expression index initial condition)
;; Sum $expression$ for $index$ = $initial$ and successive integers,
;; as long as $condition$ holds.
(let ((temp (gensym)))
`(do ((,temp 0 (+ ,temp ,expression))
(,index ,initial (1+ ,index)))
((not ,condition) ,temp))))
The main error with this macro is that it does something you could do;; Sum $expression$ for $index$ = $initial$ and successive integers,
;; as long as $condition$ holds.
(let ((temp (gensym)))
`(do ((,temp 0 (+ ,temp ,expression))
(,index ,initial (1+ ,index)))
((not ,condition) ,temp))))
with the standard LOOP. I.e. instead of debugging this macro, you could
(loop for x from 1 while (< x 6) summing x)
-> 15
(use srfi-42) ;; sum-ec
(sum-ec (:range x 1 6) x)
===>
15