• Re: Recursion or interation...??

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Fri Jul 4 01:28:42 2025
    From Newsgroup: comp.lang.lisp

    Pascal Costanza wrote:

    (defun collect-blobs (methods)
    (let ((complete-runs nil)
    (current-run nil))
    (flet ((complete-run ()
    (when current-run
    (push (nreverse current-run) complete-runs)
    (setf current-run nil))))
    (loop for method in methods do
    (when (betap method) (complete-run))
    (push method current-run))
    (complete-run))
    complete-runs))

    I like this version, and it indeed does what it should. Now I also agree
    that it is better understandable than the recursive version. May I use
    that code when I decide to make the original code publicly available?

    Testing:

    (defun betap (x) (oddp x))

    (collect-blobs '(0 0 2 3 3 4 6 8 5 5 22 24 25 27))
    ===>
    ((27) (25) (5 22 24) (5) (3 4 6 8) (3) (0 0 2))

    Begin a new group when an element satisfies betap.


    ;; Start a new sublist when "pred" yields true.
    (define (subdivide lst pred)
    (map reverse
    (reverse
    (fold
    (lambda (x accum)
    (if (or (null? accum) (pred x))
    (cons (list x) accum)
    (cons (cons x (car accum)) (cdr accum))))
    '()
    lst))))

    (subdivide '(9 0 0 2 3 3 4 6 8 5 5 22 24 25 27) odd?)
    ===>
    ((9 0 0 2) (3) (3 4 6 8) (5) (5 22 24) (25) (27))
    --- Synchronet 3.21d-Linux NewsLink 1.2