• re: DOLIST as DO

    From B. Pym@21:1/5 to Barry Margolin on Wed Jul 3 08:08:14 2024
    Barry Margolin wrote:

    In article <[email protected]> [email protected] (Jerry Jackson) writes: >(defmacro dolist ((var list &optional result-form) &rest body)
    (let ((listvar (gensym)))
    `(do* ((,listvar ,list (cdr ,listvar))
    (,var (car ,list) (car ,listvar)))
    ((null ,listvar) ,result-form)
    ,@body)))

    Both versions of your macro have a bug: they have ",list" in two
    places in the expansion. This will cause the list expression to be
    evaluated twice, which could cause problems if it has side effects.
    In the above case, both the second line of the DO* form should be

    (,var (car ,listvar) (car ,listvar))

    Fixing the DO version is slightly more complicated.

    Scheme

    (define-syntax dolist
    (syntax-rules ()
    [(dolist (var list result-form ...) expr ...)
    (do ((listvar list (cdr listvar))
    (var #f))
    ((null? listvar) result-form ...)
    (set! var (car listvar))
    expr ...)]))

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