Re: checking implementation defined and implementation dependent behaviour



Joerg Hoehle <hoehle@xxxxxxxxxxxxxxxxxxxxx> wrote:
;; Whether the iteration constructs establish a new binding of var on
;; each iteration or whether it establishes a binding for var once at
;; the beginning and then assigns it on any subsequent iterations

;; Actually, CLISP does not guarantee the (3 3 3) or (2 2 2) result,
;; it just guarantees that it won't be (2 1 0), but rather (x x x),
;; because a single binding is assigned on each iteration.

(let (a)
(dotimes (i 3) (push (lambda () i) a))
(loop for x in a collect (funcall x)))
#+(or clisp cmu sbcl cormanlisp) (3 3 3)
#+(or) (2 1 0)

CMUCL 19d was changed to return (2 1 0) for this. I think it was due
to pedantically reading the following part of the spec:

dotimes evaluates count-form, which should produce an integer. If
count-form is zero or negative, the body is not executed. dotimes
then executes the body once for each integer from 0 up to but not
including the value of count-form

as requiring the iteration count to always be constant, whether or not
the iteration variable is changed. That is, the interpretation is that
the following should return 5, not 1:

(let ((x 0))
(dotimes (i 5 x)
(incf x)
(setf i 5)))

For which the most reasonable, though not only, implementation is to
rebind the variable on every iteration.

--
Juho Snellman
.



Relevant Pages

  • Re: Stupid error on blocks
    ... iteration variable in a "for" loop which is different than when ... because the block opens a new scope. ... that same value because they fetch it from the binding. ... invocations and so you get two independent counters. ...
    (comp.lang.ruby)
  • Re: read columns from a file - how to use read-from-string?
    ... That binding is discarded before the next round in the iteration. ... I guess the multiple-value-bind evaluates it during ...
    (comp.lang.lisp)
  • Re: beginner closures
    ... > DOLIST dictionary entry: ... > binding of var on each iteration or whether it establishes a binding ...
    (comp.lang.lisp)
  • Re: URI Escape/Unescape Library?
    ... >> I also replaced the loop and length with DOTIMES. ... > binding of var on each iteration or whether it establishes a ... characters instead of iterating over the string. ...
    (comp.lang.lisp)