• Re: different LOOP results

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp on Mon Jun 23 09:09:41 2025
    From Newsgroup: comp.lang.lisp

    Dr. Edmund Weitz wrote:

    The following input

    (loop for x in '(1 2 3 4)
    for y upfrom 0
    collect x into temp
    when (oddp y)
    collect temp
    and do (print temp)
    (setq temp nil))

    results in

    (1 2)
    (3 4)
    ((1 2) (3 4))

    with CLISP and LispWorks - which is what I expected. But CMUCL shows

    (1 2)
    (1 2 3 4)
    ((1 2 3 4) (1 2 3 4))

    instead. Is this a bug or are both behaviors conforming?

    In "ANSI Common Lisp", Graham makes the following comments:

    The loop macro was originally designed to help inexperienced
    Lisp users write iterative code. Instead of writing Lisp code,
    you express your program in a form meant to resemble English,
    and this is then translated into Lisp. Unfortunately, loop is
    more like English than its designers ever intended: you can
    use it in simple cases without quite understanding how it
    works, but to understand it in the abstract is almost
    impossible.
    ....
    the ANSI standard does not really give a formal specification
    of its behavior.
    ....
    The first thing one notices about the loop macro is that it
    has syntax. A loop expression contains not subexpressions but
    clauses. The clauses are not delimited by parentheses;
    instead, each kind has a distinct syntax. In that, loop
    resembles traditional Algol-like languages. But the other
    distinctive feature of loop, which makes it as unlike Algol as
    Lisp, is that the order in which things happen is only
    loosely related to the order in which the clauses occur.
    ....
    For such reasons, the use of loop cannot be recommended.
    --- Synchronet 3.21d-Linux NewsLink 1.2