Sujet : Re: CL: Processing more than one element of a sequence at a time?
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.schemeDate : 31. Aug 2024, 07:09:24
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vau8hv$ssej$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
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 ...)]))