• Re: What's the error in this macro?

    From B. Pym@21:1/5 to All on Sat Sep 21 13:29:23 2024
    XPost: comp.lang.scheme

    KK wrote:

    (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
    with the standard LOOP. I.e. instead of debugging this macro, you could
    be working on the program where this macro is intended to be used:


    (loop for x from 1 while (< x 6) summing x)
    15

    Gauche Scheme

    (use srfi-42) ;; sum-ec

    (sum-ec (:range x 1 6) x)
    ===>
    15

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From HenHanna@21:1/5 to B. Pym on Tue Sep 24 00:49:20 2024
    XPost: comp.lang.scheme

    On Sat, 21 Sep 2024 13:29:23 +0000, B. Pym wrote:

    KK wrote:

    (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))))


    This looks good to me... Was this the corrected version?


    The main error with this macro is that it does something you could do
    with the standard LOOP. I.e. instead of debugging this macro, you could
    be working on the program where this macro is intended to be used:


    (loop for x from 1 while (< x 6) summing x) --> 15

    Gauche Scheme

    (use srfi-42) ;; sum-ec
    (sum-ec (:range x 1 6) x) ===> 15


    In Scheme and Gauche Scheme, do ppl use Gensym?


    (define-macro (Sum expression index initial condition)
    `(do ((%Sum 0 (+ %Sum ,expression))
    (,index ,initial (+ 1 ,index)))
    ((not ,condition) %Sum)))

    (print (macroexpand '(Sum x x 1 (< x 6))))

    (print (Sum x x 1 (< x 6)))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)