• Re: Loop

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp on Fri Jun 27 22:27:30 2025
    From Newsgroup: comp.lang.lisp

    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)
    --- Synchronet 3.21d-Linux NewsLink 1.2