Page 1 of 1

Why aren't these equivalent?

Posted: Wed Oct 22, 2008 1:56 pm
by jordan
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:

Code: Select all

(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:

Code: Select all

(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?

Posted: Wed Oct 22, 2008 6:26 pm
by Jasper

Code: Select all

(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?

Posted: Wed Oct 22, 2008 10:00 pm
by jordan
Ah, I see your point, I was clobbering my initial value before making use of it with the other variable's increment statement.

Thanks!