• Re: CL: Processing more than one element of a sequence at a time?

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Sat Aug 31 05:09:24 2024
    From Newsgroup: comp.lang.scheme

    Frode V. Fjeld wrote:

    Raffaele Ricciardi <rfflrccrd@gmail.com> writes:

    in CL, is there an idiomatic way to process more than one element of a sequence at a time?

    Yes: (loop for (a b) on list by #'cddr collect (cons a b))

    Or without LOOP, I'd do:

    (do ((a (pop list) (pop list))
    (b (pop list) (pop list))
    (result nil))
    ((null list) (reverse result))
    (push (cons a b) result))

    Testing:

    (setq lst '(p 2 q 3 r 4 s 5))

    (do ((a (pop lst) (pop lst))
    (b (pop lst) (pop lst))
    (result nil))
    ((null lst) (reverse result))
    (push (cons a b) result))

    ((P . 2) (Q . 3) (R . 4))

    The last pair is missing.


    Gauche Scheme

    (define lst '(p 2 q 3 r 4 s 5))

    (do@ ((:for a (pop! lst))
    (:for b (pop! lst))
    (result '()))
    ((begin (push! result (cons a b)) (null? lst))
    (reverse result)))

    ((p . 2) (q . 3) (r . 4) (s . 5))


    Given:

    Use ":for" when the same expression is to be assigned
    to the variable every time.

    (define-syntax do@-aux
    (syntax-rules ()
    [(do* (inits ...) ((var update) ...) (test expr ...) stuff ...)
    (let* (inits ...)
    (if test
    (begin expr ...)
    (begin
    (begin stuff ...)
    (let loop ()
    (begin (set! var update) ...)
    (if test
    (begin expr ...)
    (begin stuff ...
    (loop)))))))]))

    (define-syntax do@
    (syntax-rules (:for !!!)
    [(do@ !!! (inits ...) (updates ...)
    ((:for var expr) more ...) until body ...)
    (do@ !!! (inits ... (var expr)) (updates ... (var expr))
    (more ...) until body ...)]
    [(do@ !!! (inits ...) (updates ...)
    ((var init update) more ...) until body ...)
    (do@ !!! (inits ... (var init)) (updates ... (var update))
    (more ...) until body ...)]
    [(do@ !!! (inits ...) (updates ...)
    ((var init) more ...) until body ...)
    (do@ !!! (inits ... (var init)) (updates ... )
    (more ...) until body ...)]
    [(do@ !!! inits updates () until body ...)
    (do@-aux inits updates until body ...)]
    [(do@ inits-updates until stuff ...)
    (do@ !!! () () inits-updates until stuff ...)]))
    --- Synchronet 3.22a-Linux NewsLink 1.2