Deutsch   English   Français   Italiano  
<vbqsmn$3cn2o$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: "B. Pym" <Nobody447095@here-nor-there.org>
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: novice: mapcan use?
Date: Wed, 11 Sep 2024 01:44:56 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <vbqsmn$3cn2o$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Wed, 11 Sep 2024 03:44:57 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="9de66f6eb852eacea8ad670706a1cf3f";
	logging-data="3562584"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/0lMhSTf4vzBIUktFQQeiT"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:gSq5lXfhl22yV9/R5x9xLkN1iw8=
Bytes: 2003

Pascal Costanza wrote:

> > I have used NRECONC.  When you are processing a list and the
> > interesting stuff is at the head of the list, you write a loop like
> > this:
> >
> >   (do ((tail      list (cdr tail))
> >        (processed '()  (cons (do-something (car tail))
> >                              processed)))
> >       ((done? ...) (nreconc processed tail)))
> >
> > The nreconc reverses the processed stuff (which is accumulated
> > backwards) and pastes it on to the remaining element of LIST.
> 
> Ah, finally a clue. Thanks for that!
> 
> Indeed, I could have used something like that before, but came up with a
> solution with LOOP that looks like this:
> 
> (loop for (car . cdr) on list
>        collect (do-something car) into processed
>        until done
>        finally (return (nconc processed cdr)))

Gauche Scheme

(use srfi-1)  ;; span

(receive (nums rest) (span number? '(2 3 4 a b c))
  (append (map square nums) rest))
  ===>
(4 9 16 a b c)


(lope dolist-by x xs cdr '(2 3 4 a b c)
  until (not (number? x))
  collect-in (sqr x) processed
  returning (append processed xs))

===>
'(4 9 16 a b c)