Sujet : Re: Loop
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lispDate : 27. Jun 2025, 23:27:30
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <103n5sf$di1b$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
Gareth McCaughan wrote:
It's not a better algorithm. The point is that because it's
more concise you are less likely to get lost in details, and
therefore more likely to be able to spot algorithmic improvements.
If you write
(loop for x from a to b by d collect x)
rather than
(do ((j a (+ j d))
(result nil))
((>= j b) (nreverse result))
(push j result))
That's a rather poor do loop.
(do ((x 30 (- x 2))
(r '() (cons x r)))
((< x 20) r))
===>
(20 22 24 26 28 30)
Gauche Scheme:
(lrange 20 31 2)
===>
(20 22 24 26 28 30)
How about using "unfold" from SRFI-1?
(use srfi-1)
(unfold (cut > <> 30) identity (cut + <> 2) 20)
===>
(20 22 24 26 28 30)
(use srfi-42) ;; list-ec
(list-ec (: x 20 31 2) x)
===>
(20 22 24 26 28 30)
Less condensed:
(list-ec (:range x 20 31 2) x)