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: processing a sequence Date: Fri, 30 Aug 2024 22:10:07 -0000 (UTC) Organization: A noiseless patient Spider Lines: 39 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Injection-Date: Sat, 31 Aug 2024 00:10:07 +0200 (CEST) Injection-Info: dont-email.me; posting-host="c51450164ff44e4b4c9e62a2e4bb2981"; logging-data="713372"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Gk5t0O9BuZtQ9KhttRtSd" User-Agent: XanaNews/1.18.1.6 Cancel-Lock: sha1:4uA3S4l4PSvE+H//BV+V0ie2PAQ= Bytes: 1865 joswig@.... wrote: > > I am trying to process a sequence like: > > > > (0 1 2 3 0 1 2 3 0 1 2 3) > > > > and produce: > > > > ((0 1 2 3)(0 1 2 3)(0 1 2 3)) > > > > i.e. create sub-sequences beginning whenever the value decreases. > > Gauche Scheme (monotonic-slices '(0 1 2 3 0 1 2 3 0 1 2 3) + >) ===> ((0 1 2 3) (0 1 2 3) (0 1 2 3)) Given: (define (each-mono-slice func LST :optional (KEY-FUNC values) (CMP equal?)) (let ((RESULT '()) (TMP '()) (OLD-KEY 0) (NEW-KEY 0)) (dolist (X LST) (set! NEW-KEY (KEY-FUNC X)) (cond ((null? TMP) (push! TMP X)) ((CMP NEW-KEY OLD-KEY) (push! TMP X)) (#t (func (reverse TMP)) (set! TMP (list X)))) (set! OLD-KEY NEW-KEY)) (unless (null? TMP) (func (reverse TMP))) (reverse RESULT))) (define (monotonic-slices LST :optional (KEY-FUNC values) (CMP equal?)) (let ((result '())) (each-mono-slice (lambda(xs) (push! result xs)) LST KEY-FUNC CMP) (reverse result)))