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

    From B. Pym@21:1/5 to Frode V. Fjeld on Mon Aug 19 01:50:18 2024
    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))

    newLISP

    (apply
    (fn (result a b) (push (cons a b) result -1))
    (cons '() '(f 3 g 4 h 5))
    3 ;; How many items to process at a time.
    )

    ((f 3) (g 4) (h 5))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to Frode V. Fjeld on Sat Aug 31 05:09:24 2024
    XPost: 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 ...)]))

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