Sujet : Re: Recursion or interation...??
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.schemeDate : 04. Jul 2025, 02:28:42
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <1047ao8$f73b$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
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))