Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "B. Pym" Newsgroups: comp.lang.lisp,comp.lang.scheme Subject: Re: CL: Processing more than one element of a sequence at a time? Date: Sat, 31 Aug 2024 05:09:24 -0000 (UTC) Organization: A noiseless patient Spider Lines: 83 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Injection-Date: Sat, 31 Aug 2024 07:09:24 +0200 (CEST) Injection-Info: dont-email.me; posting-host="cd5bad8e55b4aa82d833cd99ecb8f893"; logging-data="946643"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19SbSxjgVXbs5vYGGmHsRlO" User-Agent: XanaNews/1.18.1.6 Cancel-Lock: sha1:4ZvQ7m+ksDjTc0OJIx5Uzp1zc/w= Bytes: 3144 Frode V. Fjeld wrote: > Raffaele Ricciardi 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 ...)]))