[plt-scheme] trying to figure out the do form

From: Sanjeev Sharma (throwit1 at hotmail.com)
Date: Mon Oct 11 23:34:15 EDT 2004

Thanks to ifconfig, I realized I asked the wrong question. I knew that a was 
being multiplied after I was decremented (in the 2nd, faulty 
implementation).  My confusion is in the way R5RS describes the form.

The piece I missed was
"The region of the binding of a <variable> consists of the entire do 
expression except for the <init>s. "

(define factorial
    (lambda (n)
    (do ((i n (- i 1)) (a 1 (* a i)))
        ((zero? i)(display "ans is  ")(display `("i, a",i",",a))(newline))
      (display `("i, a",i", ",a))(newline))))

The "i" used in the (* a i) term is the value from the previous iteration, 
NOT the new value that was just put into "i".  Do is more like let than 
letrec or let*.

But the new value will be tested in the test clause, so if i is now zero, 
"a" will never be multiplied by zero.

The other implementation was applying the new i to (* a i) immediately, thus 
my apparent "one-delayed" effect, which was really a "one-too-soon" effect.

Don’t just search. Find. Check out the new MSN Search! 

Posted on the users mailing list.