This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Why aren't these equivalent?

3 posts · 3547 views

Hello, noob question here. I'm coding up some DO loop examples to help me understand it. I eventually got my example working, but I'm curious why a previous version did NOT work.

The working one:
(defun add-inputs ()
  "Add a series of numbers from input"
  (do ((input (get-integer-from-input) (get-integer-from-input))
       (sum 0 (+ sum input)))
      ((= input 0) sum)))
The non-working one, that I thought would be equivalent:
(defun add-inputs-broken ()
  "Add a series of numbers from input"
  (do ((input (get-integer-from-input))
       (sum 0 (+ sum input)))
      ((= input 0) sum)
    (setf input (get-integer-from-input))))

Re: Why aren't these equivalent?

(do ((i 0 (+ i 1)) (list nil (cons list i)) ((= i 10) list))
(do ((i 0) (list nil (cons list i)) ((= i 10) list) (setf i (+ i 1))
Compare those. the 'do body is done before the 'increment' operations.

Re: Why aren't these equivalent?

Ah, I see your point, I was clobbering my initial value before making use of it with the other variable's increment statement.

Thanks!