From: Matthew Danish
Subject: LOOP discrepancy
Date: 
Message-ID: <20020823173216.N334@meddle.res.cmu.edu>
While testing out a loop on various implementations I came across the
following discrepancy (boiled down):

(loop repeat 3 for x from 0 finally (return x))

=> 2 in CMUCL, SBCL, and Allegro CL
but
=> 3 in CLISP

For the first 3 implementations, x is no longer incremented once the
termination test of repeat has been reached, but for CLISP it is still
incremented (whether before or despite the termination-test I do not
know).

If you change the order of the repeat and the for-from clauses in the
loop, then CMUCL, SBCL, and Allegro give 3 as a result as well.  So my
guess is that those are applying the repeat termination-test in the
order that it appears with the iteration-control clauses.

The Hyperspec, section 6.1.1.6 "Order of Execution", states that:

``* Iteration control clauses implicitly perform the following actions:

-- initialize variables;

-- step variables, generally between each execution of the loop body;

-- perform termination tests, generally just before the execution of the
loop body.''

Section 6.1.4 "Termination-test Clauses" states:

`` Termination-test control constructs can be used anywhere within the
loop body. The termination tests are used in the order in which they
appear.''


Does this mean that all stepping of variables should occur before any
termination-tests are performed--regardless of lexical position--or only
before termination-tests associated with the iteration-control clauses?

-- 
; Matthew Danish <·······@andrew.cmu.edu>
; OpenPGP public key: C24B6010 on keyring.debian.org
; Signed or encrypted mail welcome.
; "There is no dark side of the moon really; matter of fact, it's all dark."

From: Erik Naggum
Subject: Re: LOOP discrepancy
Date: 
Message-ID: <3239155029893398@naggum.no>
* Matthew Danish
| Does this mean that all stepping of variables should occur before any
| termination-tests are performed--regardless of lexical position--or only
| before termination-tests associated with the iteration-control clauses?

  My understanding is that termination tests should be performed as early as
  possible.  Otherwise, if you loop over a vector and use either "for index
  from start below end" or some equivalent thereof, you would not want to
  access the endth element, not even in the finally clause.

-- 
Erik Naggum, Oslo, Norway

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.
From: Wolfhard Buß
Subject: Re: LOOP discrepancy
Date: 
Message-ID: <m3elcojwmn.fsf@buss-14250.user.cis.dfn.de>
Matthew Danish <·······@andrew.cmu.edu> writes:

> While testing out a loop on various implementations I came across the
> following discrepancy (boiled down):
> 
> (loop repeat 3 for x from 0 finally (return x))
> 
> => 2 in CMUCL, SBCL, and Allegro CL
> but
> => 3 in CLISP

Conforming implementations should complain about illegal forms,
especially about a misplaced repeat termination test clause (ttc).

 (loop for x from 0
       repeat 3
       finally (return x))  is legal Common Lisp.

The repeat iteration control clause (icc) "must precede any other loop
clauses, except initially, with, and named...", [CLHS 6.1.2.1
Iteration Control]. However, [CLHS 6.1.4 Termination Test Clauses]
states, that "Termination-test control constructs can be used anywhere
within the loop body".  Seems a bit inconsistent, for the repeat construct
is both, ttc and icc. 

-- 
"I believe in the horse. The automobile is a passing phenomenon."
                              --  Kaiser Wilhelm II. (1859-1941)