[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: novice: mapcan use?

William James

9/3/2015 2:47:00 PM

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:

With the risk of stack-overflow:

(define items '(2 3 4 a b c))

(let go ((tail items))
(if (or (null? tail)
(not (number? (car tail))))
tail
(cons (square (car tail)) (go (cdr tail)))))

(4 9 16 a b c)


Without the risk of stack-overflow:

(let go ((accum '()) (tail items))
(if (or (null? tail)
(not (number? (car tail))))
(reverse accum tail)
(go (cons (square (car tail)) accum) (cdr tail))))

--
You have politicians saying that ... as many Africans as want to come into
Sweden should be able to come.... I think there's a billion Africans now.
They've already said that everybody from Syria can come to Sweden because they
have a civil war there.... They have a huge housing crisis; they are actually
thinking of commandeering people's vacation homes because they need more housing
for immigrants. --- Kevin MacDonald (http://lnrlive.com/tpc/tpc201...)